The ZIP file format has become extremely popular for the distribution and storage of files... 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... 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.
public static void main(String[] args) { System.out.println("Example of ZIP file creation.");
// Specify files to be zipped String[] filesToZip = new String[3]; filesToZip[0] = "firstfile.txt"; filesToZip[1] = "secondfile.txt"; filesToZip[2] = "temp\thirdfile.txt";
byte[] buffer = new byte[18024];
// Specify zip file name String zipFileName = "c:\example.zip";
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
// Set the compression ratio out.setLevel(Deflater.DEFAULT_COMPRESSION);
// iterate through the array of files, adding each to the zip file for (int i = 0; i < filesToZip.length; i++) { System.out.println(i); // Associate a file input stream for the current file FileInputStream in = new FileInputStream(filesToZip[i]);
// Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(filesToZip[i]));
// Transfer bytes from the current file to the ZIP file //out.write(buffer, 0, in.read(buffer));
int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); }
// Close the current entry out.closeEntry();
// Close the current file input stream in.close();
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.