Beruflich Dokumente
Kultur Dokumente
Article Index: The ZIP file format has become extremely popular for the distribution and storage of files.
ZIP files allow for compression, making transporting a set of files from one location to
another (e.g., across a network) faster. When a person is downloading files via a dial up
connection, the size of the file being downloaded can make the difference between sanity
and being fitted for a straight jacket. Depending on the source file types being compressed,
ZIP files can provide significant space savings. ZIP archives can also maintain the directory
structure of files that are compressed, making the ZIP format a formidable file transport
mechanism.
The java.util.zip package allows for the programmatic reading and writing of the ZIP
and GZIP formats. As you’ll see in this article, the API for reading and writing ZIP files is
pretty straightforward to use. Learning how to use the offerings of the java.util.zip package
might best be facilitated via example. In this article, I’ll take you through a Java program that
creates of a ZIP file and also walk you through another Java program that unzips the file we
created. First, let’s ZIP up a set of files. In the code in Listing 1, we first define a set of files
to be added to our ZIP file. Notice that we state the location of the files on our hard drive.
One of our files is located in a subdirectory. You will see later that the directory structure of
our files will be kept within the ZIP file we construct. The ZIP file we are creating is called
example.zip and will be stored in the root of the C drive. We also create a buffer to aid us as
we write our ZIP file. Note that the buffer size must be big enough to house the files you
want added to the ZIP file.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
1/6
Devshed 03/06/2004 08:08:07 AM
try {
ZipOutputStream out =
new ZipOutputStream(new
FileOutputStream(zipFileName));
int len;
while ((len = in.read(buffer)) > 0)
{
out.write(buffer, 0, len);
}
2/6
Devshed 03/06/2004 08:08:07 AM
}
// Close the ZipOutPutStream
out.close();
}
catch (IllegalArgumentException iae) {
iae.printStackTrace();
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
The core of our ZIP file construction is performed in our try−catch clause. First we create a
new ZipOutputStream object. On this object, we use the setLevel method to specify the
compression ratio we’d like to use when we create our ZIP file. The Deflater object contains
static member variables for specifying such compression options as best compression (i.e.,
BEST_COMPRESSION), best speed (i.e., BEST_SPEED), no compression (i.e.,
NO_COMPRESSION). In our code, we specify the option of BEST_COMPRESSION.
To add files to our zip file, we iterate through our filesToZip file name array, each time
associating a FileInputStream to the file we are currently working with. On all iterations, we
also use the putNextEntry method of our ZipOutputStream object, to add the current file to
our target Zip file, followed by a writing of the bytes of the current file to the
ZipOutputStream. After each file is written, we must use the closeEntry method. Finally,
after iterating is complete, we use the close method to close off our ZipOutPutStream,
thereby sealing our ZIP file. So we can create zip files with Java. Can we extract them?
Listing 2 shows an example Java class that can do just that. Let’s cover what’s going on in
the class, as it might not be that obvious to the java.util.zip API newcomer.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipUncompressExample
{
3/6
Devshed 03/06/2004 08:08:07 AM
// specify buffer size for extraction
static final int BUFFER = 2048;
File destFile =
new File(unzipDestinationDirectory, currentEntry);
4/6
Devshed 03/06/2004 08:08:07 AM
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
5/6
Devshed 03/06/2004 08:08:07 AM
advantages of compression and ease of storage, being able to
programmatically ZIP and UNZIP files presents a valuable
facility to the Java developer. One can do such things as
build applications that dynamically create ZIP files on an as
needed basis from a specified set of files.
6/6