Working with Zip Files in Java

2 minutes read

Table of Content

Introduction

Java_logozip files java

Java comes equipped with “java.util.zip” library to perform operations related to zip files.  In this post we would look at some operations related to zip files.

You can get the source code at  github here

How to create a zip File

We can zip a list of files

* Add a file to the zip

* @param zipfilename
* @param file
* @param zos
* @throws FileNotFoundException
* @throws IOException
*/

private void addToZip(File zipfilename, File file, ZipOutputStream zos)

throws FileNotFoundException, IOException {

FileInputStream fis = new FileInputStream(file);

// we want the zipEntry's path to be a relative path that is relative

// to the directory being zipped, so chop off the rest of the path

String zipFilePath = file.getCanonicalPath().substring(

zipfilename.getCanonicalPath().length() + 1,

file.getCanonicalPath().length());

System.out.println("Writing '" + zipFilePath + "' to zip file");

ZipEntry zipEntry = new ZipEntry(zipFilePath);

zos.putNextEntry(zipEntry);

byte[] bytes = new byte[1024];

int length;

while ((length = fis.read(bytes)) >= 0) {

zos.write(bytes, 0, length);

}
zos.closeEntry();

fis.close();

}

How to zip a directory

First recursively find all the files in a given directory.  Then add the found files to the desired zip file in any given directory .

Recursively get all the files in a given Directory

 


* Read all the files recursively from the directory
* 
* @param dir
* @param fileList
*/

private void getAllFiles(File dir, List fileList) {

try {

	File[] files = dir.listFiles();

	for (File file : files) {

		fileList.add(file);

		if (file.isDirectory()) {

			System.out.println("directory:" + file.getCanonicalPath());

			getAllFiles(file, fileList);

		} else {
			System.out.println("     file:" + file.getCanonicalPath());
		}
	}

} catch (IOException e) {
	e.printStackTrace();
}
}

Adding a directory to a zip file


	 * Compress a given directory recursively and store the zip in the provided
	 * directory name

	 * @param directoryToZip
	 */

	public void compressDirectory(String fileDirectory,

			String savedZipFileDirectory) {

		File directoryToZip = new File(fileDirectory);

		List fileList = new ArrayList();

		try {

			System.out.println("---Getting references to all directory in: "

					+ directoryToZip.getCanonicalPath());

		} catch (IOException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

		getAllFiles(directoryToZip, fileList);

		System.out.println("---Creating zip file");

		String folder = savedZipFileDirectory + File.separator

				+ directoryToZip.getName();

		writeZipFile(folder, directoryToZip, fileList);

		System.out.println("---Done");

	}

How to Decompress a Zip File



	 * Uncompress a zip file
	 * @param zipFile
	 */

public void unCompressZipFile(String zipFileName) {

	try {

		ZipFile zipFile = new ZipFile(zipFileName);

		Enumeration<?> enu = zipFile.entries();

		while (enu.hasMoreElements()) {

			ZipEntry zipEntry = (ZipEntry) enu.nextElement();



			String name = zipEntry.getName();

			long size = zipEntry.getSize();

			long compressedSize = zipEntry.getCompressedSize();

			System.out.printf(

					"name: %-20s | size: %6d | compressed size: %6dn",

					name, size, compressedSize);



			File file = new File(name);

			if (name.endsWith("/")) {

				file.mkdirs();

				continue;

			}



			File parent = file.getParentFile();

			if (parent != null) {

				parent.mkdirs();

			}



			InputStream is = zipFile.getInputStream(zipEntry);

			FileOutputStream fos = new FileOutputStream(file);

			byte[] bytes = new byte[1024];

			int length;

			while ((length = is.read(bytes)) >= 0) {

				fos.write(bytes, 0, length);

			}

			is.close();

			fos.close();



		}

		zipFile.close();

	} catch (IOException e) {

		e.printStackTrace();

	}

	}

See content of a zip file


	 * See the contents of a zip file

	 */

	public void seeContentOfZipFile(String zipfile) {

		try {

			ZipFile zipFile = new ZipFile(zipfile);

			Enumeration<?> enu = zipFile.entries();

			while (enu.hasMoreElements()) {

				ZipEntry zipEntry = (ZipEntry) enu.nextElement();

				String name = zipEntry.getName();

				long size = zipEntry.getSize();

				long compressedSize = zipEntry.getCompressedSize();

				System.out.printf(

						"name: %-20s | size: %6d | compressed size: %6dn",

						name, size, compressedSize);

			}

		} catch (IOException e) {

			e.printStackTrace();

		}

	}

You can get the source code at  github here

Updated:

Leave a Comment