How do I read contents of a ZipEntry from a zip file?

T

teresni

I've got code that is opening a zip file, and then writes data for each
zip file entry to a
database. So I need to be able to read in the contents, and it's not
just simple character
data. How do I read this in? The zip entry is part of a larger
ZipInputStream. I can
read data from that stream, but it's not working properly, most likely
I'm guessing
because I'm not reading the proper positions in the file for the
ZipEntry being processed.
So how do I know the offset for reading from the ZipInputStream and/or
how can I
directly read just the contents of a specific ZipEntry??
 
N

NullBock

Get the InputStream from the ZipFile object :

ZipFile file = new ZipFile("/my/zip/file.zip");
for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) e.nextElement();
InputStream in = file.getInputStream(entry);
...
}

Is this what you want?

Walter
 
T

teresni

Not exactly. That is what I am doing to read the 'main' zip file. But
the zip file contains other zip files. So when I read in an entry as
your code shows, I am still looking at a zip file. I need to be able
to then read the entries of THAT zip file. It is not a file on the
file system, so I cannot set it up as a file object. I just cannot
find a way to do this. I may have to just create new files on the file
system for the nested zip files and then process those...
 
C

Chris Uppal

Not exactly. That is what I am doing to read the 'main' zip file. But
the zip file contains other zip files. So when I read in an entry as
your code shows, I am still looking at a zip file. I need to be able
to then read the entries of THAT zip file.

I think you can do it by wrapping a ZipInputStream around the InputStream for
the nested zip file entry. I haven't tried it myself, so there may be issues,
but it /ought/ to work.

-- chris
 
T

teresni

I did try that, and while it lets me do things like getEntry, that
still doesn't let me cleanly read in just the contents of that 1
embedded file. The methods for reading want to read an entire file
(which I don't have, since this is a piece of a larger file) or they
want an offset at which to start reading, which I don't know how to
calculate for a zip file...
 
C

Chris Uppal

I did try that, and while it lets me do things like getEntry, that
still doesn't let me cleanly read in just the contents of that 1
embedded file.

I'm not sure if I'm understanding you correctly, but if what you want to do is
find just 1 specific entry in a "zipfile" which itself is embedded in another
zip file, then you can't do it.

Only the outermost (real) zip file can be opened as a java.util.zip.ZipFile
(and so allow random access). Embedded zip-format files can be only be treated
sequentially by using a ZipInputStream. That makes sense, if you think about
it, because it's not possible to do random access in compressed data (which the
embedded zip file is), so all you can do is iterate over it.

In theory the API could allow you to extract the bytes of the embedded zipfile
(thus decompressing them) and wrap a ZipFile object around that byte[] array,
but in fact the API does not support it.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top