Learn How to handle ZIP files in Java



In any software application, be it client-server or distributed, data transfer is always there. So, the architecture of an application is designed to ease the data transfer process and improve the performance. One simple solution to manage this huge volume of data is to add more storage and improve communication between the two ends (namely client and server or storage system). But the problem is, additional storage and communication infrastructure needs more investment, which is not always possible. The alternate solution is to use data compression technology.
In this article, we will focus on data compression and decompression and how it can be handled by Java applications.

How data compression works?
Data compression can be achieved in different ways. Here we will discuss some basic concepts and compression techniques. Compression mainly removes the redundancy in a data set by applying intelligent techniques to represent it. The simplest form of redundancy is repetition of similar characters in a string of data. And, compression techniques usually compact the string and remove redundant characters.

For example, check the following string; it has lot of redundant characters which can be represented in a different way.

Let’s compact the string as shown below. It represents the same string with a different technique.

There are different other ways also for data compression. But the basic concept is same as discussed above.

What is the difference between ZIP and GZIP?
We must have used ZIP and GZIP utilities in many cases for compressing or decompressing files. But there is a difference between the usages of these utilities. In Windows environment, WinZip tool is used for file compression and decompression. But in UNIX environment, it is performed in a different way. First, ‘tar ‘utility is used to create an archive and then GZIP is used to compress the archived files. So, in UNIX it is a two-step process whereas in Windows both archiving and compression is done in a single step.

How Java works with ZIP files?
Java as a language provides lot of packages to work with data compression and decompression. The main utility package is java.util.zip, which is used to compress zip compatible files. These packages provide different classes to read, write. create, modify GZIP and ZIP file formats. They also have checksums to validate the size before and after compression. Different Java utilities related to data compression and decompression can be checked in Oracle documentation.

Let’s try some examples
In this section we will try two examples, one with file compression and the other one with file decompression.

File compression example:
In this example, we will check how Java application can be used to compress a file and make an archive. Here we have used a single file named ‘firstfile.txt’ to compress. The file will be compressed in a zip file named as ‘FileArchive.zip’. The example program can be modified to compress multiple numbers of files using a loop. The files can be created at any location and the path has to specify in the application. We have kept both the files in a single location where the Java application is kept (for simplicity).

Listing1: Example showing file compression using Java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileCompress 
    public static void main( String[] args )
    	byte[] buffersz = new byte[1024];    	
       		//Create different input and output streams
		FileOutputStream floutstr = new FileOutputStream("FileArchive.zip");
    		ZipOutputStream zpoutstr = new ZipOutputStream(floutstr);			
		//Add the files to be compressed
    		ZipEntry zpentry= new ZipEntry("firstfile.txt");
		//Create file input stream
    		FileInputStream finstrm = new FileInputStream("firstfile.txt");   	   
    		//Write to the zip output stream
		int lenstrm;
    		while ((lenstrm = finstrm.read(buffersz)) &gt; 0) {
    			zpoutstr.write(buffersz, 0, lenstrm);
		//Close file input stream
    		//Close zip output stream and entry
    		System.out.println("File compression done successfully");
    	}catch(IOException ex){

After the Java program is compiled and executed, a zip file will be created at the same location. Now, extract the zip file and you will find the file ‘firstfile.txt’ kept inside it.

Following screen shot shows the output and other details as described above.

Image1: Shows file compression output

File decompression example:
In this example, we will check how to decompress a file from a zip file. We will use the same zip file above ‘FileArchive.zip’ and extract the content inside it. It will extract the file ‘FileArchive.zip’ in an output directory.

Listing 2: File decompression using Java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class FileDecompress
    List&lt;String&gt; listOfFiles;
    private static final String ZIP_FILE_INPUT = "FileArchive.zip";
    private static final String ZIP_OUTPT_FLDR = "Zipoutput";		
    public static void main( String[] args )
    	FileDecompress unZipFile = new FileDecompress();
    public void unZipFile(String zipFileName, String outpfFldr){
     byte[] bufferlen = new byte[1024];    	
    	//Create output folder/directory (if not exist)
    	File foldernm = new File(ZIP_OUTPT_FLDR);
    	//Getting zip file content
    	ZipInputStream zipinstrm = 	new ZipInputStream(new FileInputStream(zipFileName));
    	//Get the list entry
    	ZipEntry zipentry = zipinstrm.getNextEntry();    		
    	   String fileNm = zipentry.getName();
                     File newFileName = new File(outpfFldr + File.separator + fileNm);                
                     System.out.println("Unzipped file name is : "+ newFileName.getAbsoluteFile());
                     FileOutputStream foutstrm = new FileOutputStream(newFileName);           
                     int lenstrm;
                   while ((lenstrm = zipinstrm.read(bufferlen)) &gt; 0) {
       		foutstrm.write(bufferlen, 0, lenstrm);
            zipentry = zipinstrm.getNextEntry();
    	System.out.println("Files decompressed successfully");    		
    }catch(IOException ex){

Now compile and execute the Java file as shown below. It will create an output directory and put the extracted file there. Following screen shot shows the details.


Image2: Shows file decompression output

Data compression and decompression is very important in any software application. Here, we have discussed about Java APIs which can be used for the same purpose. We have also discussed some basic concepts of data compression and how it is achieved internally. Java examples are also explained with file compression and decompression. There are also different other ways to perform the same task, but these simple examples will help you understand the basics and implement in your application development.


Please enter your comment!
Please enter your name here