Create Zip Files in Java - Example Program

File Compression in Java - Introduction


In this post, we will explain how to create a .ZIP file in Java, by compressing two files in a folder as an example. There are so many tutorials out there in the internet, that explains how to compress files in Java. Then, why another one? I would like the readers to know about Apache Common compress library, that automatically handles all the compression for you, and let you write simplistic code in your Java program, to achieve the outcome. You will appreciate the power of this library at the end of this tutorial, and see how easy it is to zip files using this open source technique. With this introduction, let us get started.

Zipper - Apache Commons Compress


To work with the examples provided in this tutorial, you have to download Apache Commons Compress 1.4.1 or equivalent. You should ideally have commons-compress-1.4.1.jar in your classpath.

Test Input Files for Zipping


A screenshot of the files we would like to zip and create a single file, is provided below:

Create Zip File in Java - Apache Commons Compress- Test Input Files
Create Zip File in Java - Apache Commons Compress- Test Input Files
The step by step code to create a zip file in Java using Apache Commons Compress, is provided below. You will see how easy it is to write few lines of code to create your zip file. Here we go.

Create FileOutputStream - Final Zip File


The first step is to create a FileOutputStream object that will map to the zip file we want to create on the disk. We have done this many times. Here is the code snippet.

                /* Create Output Stream that will have final zip files */
                OutputStream zip_output = new FileOutputStream(new File("zip_output.zip"));

Create Logical Archiver Object


We now create an Archiver object, where we specify the type of compression (i.e ZIP) and attach the output stream. Every file we add to this object, will be mapped to the physical file. (not there yet)..
                /* Create Archive Output Stream that attaches File Output Stream / and specifies type of compression */
                ArchiveOutputStream logical_zip = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zip_output);

Add Files to the Archive


You can now add all files you need into the archive. First we use "putArchiveEntry" method to create the header entry for the archive. After this, we use IOUtils.copy, method to copy the physical file to ArchiveOutputStream. Once this is done, we use "closeArchiveEntry" method and add trailer information for the file added. That is it..You have to repeat this step for every file you want to add to the zip file.
                /* Create Archieve entry - write header information*/
                logical_zip.putArchiveEntry(new ZipArchiveEntry("test_file_1.xml"));
                /* Copy input file */
                IOUtils.copy(new FileInputStream(new File("test_file_1.xml")), logical_zip);
                /* Close Archieve entry, write trailer information */
                logical_zip.closeArchiveEntry();
                /* Repeat steps for file - 2 */
                logical_zip.putArchiveEntry(new ZipArchiveEntry("test_file_2.xml"));
                IOUtils.copy(new FileInputStream(new File("test_file_2.xml")), logical_zip);
                logical_zip.closeArchiveEntry();

Finish Zipping


We are done. Invoke 'finish' method in ArchiveOutputStream, that will prepare the zip file. Finally, you can close the output stream. Easy, your zip file is ready.
                /* Finish addition of entries to the file */
                logical_zip.finish(); 
                /* Close output stream, our files are zipped */
                zip_output.close();

Complete Java Program - Zip Files using Java 


The complete Java program to create a zip file in Java using Apache Commons Compress library is provided below:

import java.io.*;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
public class ZipFiles {  
        public static void main(String[] args) throws Exception{
                /* Create Output Stream that will have final zip files */
                OutputStream zip_output = new FileOutputStream(new File("zip_output.zip"));
                /* Create Archive Output Stream that attaches File Output Stream / and specifies type of compression */
                ArchiveOutputStream logical_zip = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zip_output);
                /* Create Archieve entry - write header information*/
                logical_zip.putArchiveEntry(new ZipArchiveEntry("test_file_1.xml"));
                /* Copy input file */
                IOUtils.copy(new FileInputStream(new File("test_file_1.xml")), logical_zip);
                /* Close Archieve entry, write trailer information */
                logical_zip.closeArchiveEntry();
                /* Repeat steps for file - 2 */
                logical_zip.putArchiveEntry(new ZipArchiveEntry("test_file_2.xml"));
                IOUtils.copy(new FileInputStream(new File("test_file_2.xml")), logical_zip);
                logical_zip.closeArchiveEntry();
                /* Finish addition of entries to the file */
                logical_zip.finish(); 
                /* Close output stream, our files are zipped */
                zip_output.close();
        }
}

The program zips the files as we specified in the input which can be opened with your popular zip file editor. A screenshot of the output zip file is given below:

Zip File - Apache Commons Compress - Java Program Output - Example
Zip File - Apache Commons Compress - Java Program Output - Example

No comments:

Post a Comment