Jan 242017

ADM

How to compress files in ZIP format using Java

  • 24 January 2017
  • ADM

 

 

 

How to compress files in ZIP format using Java - images/logos/java.jpg

 

Here is an example how to compress files in ZIP format using Java.

Example

The purpose of this tutorial is to take a file or a folder and compress into an archive all the files and sub-folders keeping the structure. For these classes from the java.util.zip package will be used.

There are two options for compression:

  • STORED: uncompressed entries.
  • DEFLATED: compressed entries.
package com.admfactory.io.archive;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZIPArchive {

    public static void main(String[] args) {

	String output = "D:\\admfactory.com\\archive.zip";
	String source = "D:\\admfactory.com\\dir1\\";

	System.out.println("Zip Archive Example");
	System.out.println();
	System.out.println("Output to Zip : " + output);
	System.out.println();
	List<String> fileList = getAllFileList(new File(source));

	zip(output, fileList);
    }

    /**
     * Zip the list received as parameter
     * 
     * @param zipFile
     *            output ZIP file location
     */
    public static void zip(String zipFile, List<String> fileList) {

	byte[] buffer = new byte[1024];

	try {
	    FileOutputStream fos = new FileOutputStream(zipFile);
	    ZipOutputStream zos = new ZipOutputStream(fos);
	    zos.setMethod(ZipOutputStream.DEFLATED);
	    
	    for (String file : fileList) {

		System.out.println("File added: " + file);
		ZipEntry ze = new ZipEntry(file);
		zos.putNextEntry(ze);

		FileInputStream in = new FileInputStream(file);

		int len;
		while ((len = in.read(buffer)) > 0) {
		    zos.write(buffer, 0, len);
		}

		in.close();
	    }

	    zos.closeEntry();

	    zos.close();

	    System.out.println();
	    System.out.println("Done!");
	} catch (IOException ex) {
	    ex.printStackTrace();
	}
    }

    /**
     * Traverse a directory and get all files, and add the file into fileList
     * 
     * @param file
     *            file or directory
     * @return the list with all files to be added
     */
    public static ArrayList<String> getAllFileList(File file) {

	ArrayList<String> result = new ArrayList<String>();
	/** add the file only */
	if (file.isFile()) {
	    result.add(file.getAbsolutePath());
	    /** add all files from the folder */
	} else if (file.isDirectory()) {
	    File[] files = file.listFiles();
	    for (File f : files) {
		result.addAll(getAllFileList(f));
	    }
	}
	return result;
    }

}

Output

Zip Archive Example

Output to Zip : D:\admfactory.com\archive.zip

File added: D:\admfactory.com\dir1\dir3\dir4\f6.txt
File added: D:\admfactory.com\dir1\dir3\dir4\f7.txt
File added: D:\admfactory.com\dir1\dir3\f4.txt
File added: D:\admfactory.com\dir1\dir3\f5.txt
File added: D:\admfactory.com\dir1\f1.txt
File added: D:\admfactory.com\dir1\f2.txt
File added: D:\admfactory.com\dir1\f3.txt

Done!

Also, you can check the archive file created.

 

 

 

References