Breaking News
Home / Java / Java Tutorials / How to extract a zipped file using Java?

How to extract a zipped file using Java?

Unzip File using Java

Building a Zip utility like Winzip is easy in Java, similarly building a unzip tool is easy too,  thanks to java.util.zip package.

Basically in this case we need to open a ZipInputStream to reference the zipped file that we want to extract. Then loop through all the ZipEntry defined in the file. Each ZipEntry represent a file that is packaged into the zip file. While looping, create a handle for each of the output file using FileOutputStream. Then read the byte chucks of that file from the ZipInputStream and write it to FileOutputStream and flush. Once done extracting all the files, finally close the zip output stream. As a result of this, the output folder will contain all the extracted files what were contained in the zip.

package com.livrona.snippets.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * This snippet show how write a unzip a file and extract its contents.
 * 
 * @author java4learners
 */
public class UnzipFile {

	// buffer to hold contents
	private static final int BUFFER = 1024;

	/**
	 * Unzip the file into output folder
	 * 
	 * @param inFile
	 * @param outFolder
	 * @return
	 */
	public void unzip(File inFile, File outFolder) {
		try {

			// create a zip input stream
			ZipInputStream in = new ZipInputStream(new BufferedInputStream(
					new FileInputStream(inFile)));
			ZipEntry entry;

			// loop through zipped file entries
			while ((entry = in.getNextEntry()) != null) {
				System.out.println("Extracting: " + entry);
				int count;
				// buffer to read bytes of the file
				byte data[] = new byte[BUFFER];

				// write the files to the disk
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(outFolder.getPath() + "/"
								+ entry.getName()), BUFFER);

				// read of bytes and write to file in output folder
				while ((count = in.read(data, 0, BUFFER)) != -1) {
					out.write(data, 0, count);
				}
				// clean the file output stream created
				cleanUp(out);
			}
			cleanUp(in);
		} catch (Exception e) {
			System.out.println("Failed to extract : " + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * Close the Input Stream
	 * 
	 * @param in
	 * @throws Exception
	 */
	private void cleanUp(InputStream in) throws Exception {
		in.close();
	}

	/**
	 * Close the output stream
	 * 
	 * @param out
	 * @throws Exception
	 */
	private void cleanUp(OutputStream out) throws Exception {
		out.flush();
		out.close();
	}

	/**
	 * Test Method
	 * @param args
	 */
	public static void main(String[] args) {

		if (args.length != 2) {
			System.out
					.println("Usage: java com.livrona.snippets.io.UnzipFile <Path to your zip file> <Path to output folder>");
			System.exit(0);
		}

		// store args
		String zipfile = args[0];
		String folder = args[1];
		System.out.println("Zip File : " + zipfile);
		System.out.println("Output File : " + folder);

		// invoke the unzipper
		try {
			UnzipFile unzipper = new UnzipFile();
			unzipper.unzip(new File(zipfile), new File(folder));
			System.out.println("File unzipped successfully");
		} catch (Exception e) {
			System.out.println("Failed to unzip file : " + e.getMessage());
			e.printStackTrace();
		}

	}
}

 [pb_builder]

Check Also

Have a Question

How to select a JVM Analyzing Tool?

A JVM monitoring tool also know as a Diagnostic might be needed to fine tune …

Leave a Reply