Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
teresni@ucia.gov - 17 Nov 2005 19:39 GMT
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??
NullBock - 17 Nov 2005 21:15 GMT
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
teresni@ucia.gov - 18 Nov 2005 15:15 GMT
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...
Chris Uppal - 18 Nov 2005 16:50 GMT
> 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
teresni@ucia.gov - 18 Nov 2005 17:36 GMT
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...
Chris Uppal - 19 Nov 2005 10:46 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.