|
Search this site:
|
Reading a ZIP file in JavaJava provides the facility to read raw data compressed using the DEFLATE algorithm using a Deflater or DeflaterInputStream. For many applications, an even more useful facility is that Java provides an API for reading from (and writing to) ZIP files. (Such support is inevitable, since the jar (Java archive) file is essentially a ZIP file.) The ZIP file format packages a number of files together into a single archive; individual subfiles within the archive are compressed using the DEFLATE algorithm. Thus before reading data from the archive, we need to specify which subfile we want to read. Firstly, let's consider the case where we want to read from a single file within the ZIP archive1. The basic pattern for doing so is as follows (for clarity, we'll ignore exception handling code):
ZipFile zf = new ZipFile(file);
try {
InputStream in = zf.getInputStream("file.txt");
// ... read from 'in' as normal
} finally {
zf.close();
}
We can successively call getInputStream() on any number of subfiles in the archive and read the corresponding data. If you really desire, you can also hold open and call read methods concurrently on different InputStreams from the same ZipFile, but the actual reads are synchronized on the ZipFile object, so there'll only be one actual read per zip file in progress at any one time. Given the nature of what ZipFile does, that kind of makes sense. Buffering and stream closingIn general, the flavour of InputStream returned by ZipFile.getInputStream() can be treated as any old InputStream. A couple of subtleties are:
Of course, you should generally avoid unbuffered single-byte reads and writes; I make the point simply because you might have expected the single-byte read to be reading straight from a buffer, given the decompression process2. Enumerating entries and metadataThe example above assumes that you want to read from a known file in the zip archive. But what happens if you want to read from 'all' of the files, or files matching a certain filter etc? On the next page, we look at how to enumerate zip entries and read their metadata via the ZipFile class. 1. A slight anomaly is that the zip file data must physically be in
a file; we can't open a zip file from an arbitrary input stream. Written by Neil Coffey. Copyright © Javamex UK 2008. All rights reserved. |