[
https://issues.apache.org/jira/browse/COMPRESS-508?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17082148#comment-17082148
]
Stefan Bodewig commented on COMPRESS-508:
-----------------------------------------
Currently there is no way to get the size of a STORED entry using a data
descriptior reliably without using {{ZipFile}}. {{ZipArchiveInputStream}} can
do so for certain entries but fails badly when the entry's content contains
certains sequences of bytes that look like data descriptors or other ZIP
meta-data structures, which is certainly true if the entry itself is a ZIP
archive.
The most reliable way to find out whether your archive causes problems is to
start reading it wit{{h }}{{ZipArchiveInputStream}} and see whether an
exception is thrown. If there are no data descriptors for stored entries it
will work. Likewise if you enable data descriptors for stored entries and the
exception disappears then the data descriptor could be found and its content
was consistent with the bytes read so far - otherwise you get an exception with
a different message.
The two-pass approach we've described in this issue is possible, but more
complex than it looks at first glance. The first pass needs to find and parse
the so called central directory. But if the archive contains another archive -
which is the most common cause of the problem at hand - the inner archive
certainly contains a valid central directory that will be found and parsed. So
if you simple stop if you find a valid central directory you'll end up with
reading the meta data of the (first) inner archive and the second pass would
extract the inner archive rather than the outer one.
So far you are the first one who ever talked about a situation where it was
possible to open an {{InputStream}} twice but not possible to provide random
access to its content. The "keep everything in memory" or alternatively
"capture the contents to a temporary file on disk" workarounds seem to have
been sufficient so far. This makes it look as if your case is not a very common
one.
> Provide an API that can read ZIP archives in two passes over a stream
> ---------------------------------------------------------------------
>
> Key: COMPRESS-508
> URL: https://issues.apache.org/jira/browse/COMPRESS-508
> Project: Commons Compress
> Issue Type: Improvement
> Components: Archivers
> Affects Versions: 1.20
> Environment: Android 9 and Android 10, on both emulator and real
> device .
> Reporter: AD_LB
> Priority: Major
> Labels: zip
> Attachments: 2020-03-31_20-53-36.png, 2020-04-01_18-28-19.mp4,
> ZipTest.zip, ZipTest2.zip, ZipTest3.zip, test.zip
>
>
> I'm trying to use ZipArchiveInputStream to iterate over the items of a zip
> file (which may or may not be a real file on the file-system, which is why I
> use a stream), optionally creating a stream from specific entries.
> One of the operations I need is to get the size of the files within.
> For some reason, it fails to do so. Not only that, but it throws an exception
> when I'm done with it:
> {code:java}
> Error:org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException:
> Unsupported feature data descriptor used in entry ...
> {code}
> I've attached here 3 files:sample project, the problematic zip file (remember
> that you need to put it in the correct path and grant storage permission),
> and a screenshot of the issue.
> Note that if I open the file using a third party PC app (such as
> [7-zip|https://www.7-zip.org/] ), it works fine, including showing the file
> size inside.
> Files:
> !2020-03-31_20-53-36.png![^test.zip]
> [^ZipTest.zip]
> Here's the relevant code (kotlin) :
>
> {code:java}
> thread {
> try {
> val file = File("/storage/emulated/0/test.zip")
> ZipArchiveInputStream(FileInputStream(file)).use {
> while (true) {
> val entry = it.nextEntry ?: break
> Log.d("AppLog", "entry:${entry.name} ${entry.size} ")
> }
> }
> Log.d("AppLog", "got archive ")
> } catch (e: Exception) {
> Log.d("AppLog", "Error:$e")
> e.printStackTrace()
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)