Java language comes with a build in API that enables to compress and decompress files and in this tutorial we will see how this API can be used.

Compressing files

The process of file compression is quite easy and straightforward.As you can see in the below snippet we create a stream to write the files we have to compress in zip format and then we add the files into the stream as zip entries:

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileCompression {

	public static void main(String[] args) {

		FileOutputStream stream = null;
		byte[] buffer = new byte[4096];
		int read = 0;
		try {
			// create the stream to get the contents of the uncompressed file
			File uncompressedFolder = new File("C:\\unzippedFile");
			stream = new FileOutputStream("C:\\" + uncompressedFolder.getName()+ ".zip");
			// create the stream to write data in zip format
			ZipOutputStream zipStream = new ZipOutputStream(stream);
			String[] filesList = uncompressedFolder.list();
			// get all the files in the uncompressed folder,create zipEntries
			// and put it on the zipStream
			for (int i = 0; i < filesList.length; i++) {
				String string = filesList[i];
				File file = new File("C:\\unzippedFile\\" + string);
				FileInputStream fileStream = new FileInputStream(file);
				ZipEntry zipEntry = new ZipEntry("unzippedFile\\"
						+ file.getName());
				zipStream.putNextEntry(zipEntry);
				while (fileStream.read(buffer) != -1) {
					zipStream.write(buffer, 0, read);

				}
				fileStream.close();

			}
			zipStream.close();

		} catch (IOException ex) {
			Logger.getLogger(JavaApplication23.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				stream.close();
			} catch (IOException ex) {

				Logger.getLogger(JavaApplication23.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
	}
}

Decompressing Files

The process of decompressing files is also quite easy and similar to the compression process. As you can see in the below snippet we create a stream to read the files that are in zip format â?? the zipEntries- and then add them to the decompressed folder

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FolderDecompression {

	public static void main(String args[]) {
		File zippedFile = new File("C:\\unzippedFile.zip");
		byte buffer[] = new byte[1024];

		try {

			FileInputStream inputStream = new FileInputStream(zippedFile);
		
			// Create the folder to store the decompressed files
			File uncompressedFile = new File("C:\\" + zippedFile.getName());
			uncompressedFile.mkdir();
			// Create a stream to get the contents of the compress folder
			ZipInputStream zipInputStream = new ZipInputStream(inputStream);
			ZipEntry zipEntry = null;
			while ((zipEntry = zipInputStream.getNextEntry()) != null) {

				FileOutputStream outputStream = new FileOutputStream("C:\\"
						+ zipEntry.getName());

				while (zipInputStream.read(buffer, 0, 1024) != -1) {
					outputStream.write(buffer, 0, 1024);
				}
				outputStream.flush();
				outputStream.close();
			}
			inputStream.close();
			zipInputStream.close();
		} catch (IOException ex) {
			Logger.getLogger(FolderDecompression.class.getName()).log(
					Level.SEVERE, null, ex);
		}
	}
}