zip

Create zip file from directory recursively with ZipOutputStream

In this example we shall show you how to create a zip file from a directory recursively, using ZipOutputStream. We have implemented a method to write a File to a ZipFile, with a ZipOutputStream. It is void addDirToArchive(ZipOutputStream zos, File srcFile). The steps of this method are described below:

  • The method gets the array of Files of abstract pathnames denoting the files in the directory denoted by this abstract pathname, using listFiles() API method of File.
  • For each one of the Files in the array, it checks if it is a directory, with isDirectory() API method of File, and if so, it begins all over for the Files in the directory.
  • When it reaches to a File that is not a directory, in order to add the file to the ZipFile, it creates a FileInputStream by opening a connection to the file. It also creates a new ZipEntry with the name of the specified file, and begins writing it to the ZipOutputStream. The default compression method will be used if no compression method was specified for the entry.
  • It reads up to 1024 bytes of data from the file, using the read(byte[] b) API method of FileInputStream and writes the data to the current ZipEntry, using write(byte[] b, int off, int len) method of ZipOutputStream.
  • At the end it closes both the ZipOutputStream and the ZipEntry.

We have created a FileOutputStream to write to the file with the specified name, that is the zipFile and a new ZipOutputStream from the FileOutputStream, that is an output stream filter for writing files in the ZIP file format. We have also created a new File instance by the pathname of the source file. Then we call the addDirToArchive(ZipOutputStream zos, File srcFile) method, with the specified ZipOutputStream and File we have created, to create the zip file. When the method returns, we close the ZipOutputStream, as described in the code snippet below.

package com.javacodegeeks.snippets.core;

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

public class CreateZipFileFromDirectoryRecursivelyWithZipOutputStream {

	public static void main(String[] args) {
		
		String zipFile = "C:/archive.zip";
		
		String srcDir = "C:/srcdir";
		
		try {

			FileOutputStream fos = new FileOutputStream(zipFile);

			ZipOutputStream zos = new ZipOutputStream(fos);
			
			File srcFile = new File(srcDir);
			
			addDirToArchive(zos, srcFile);

			// close the ZipOutputStream
			zos.close();
			
		}
		catch (IOException ioe) {
			System.out.println("Error creating zip file: " + ioe);
		}
		
	}
	
	private static void addDirToArchive(ZipOutputStream zos, File srcFile) {

		File[] files = srcFile.listFiles();

		System.out.println("Adding directory: " + srcFile.getName());

		for (int i = 0; i < files.length; i++) {
			
			// if the file is directory, use recursion
			if (files[i].isDirectory()) {
				addDirToArchive(zos, files[i]);
				continue;
			}

			try {
				
				System.out.println("tAdding file: " + files[i].getName());

				// create byte buffer
				byte[] buffer = new byte[1024];

				FileInputStream fis = new FileInputStream(files[i]);

				zos.putNextEntry(new ZipEntry(files[i].getName()));
				
				int length;

				while ((length = fis.read(buffer)) > 0) {
					zos.write(buffer, 0, length);
				}

				zos.closeEntry();

				// close the InputStream
				fis.close();

			} catch (IOException ioe) {
				System.out.println("IOException :" + ioe);
			}
			
		}

	}

}

 
This was an example of how to create a zip file directory recursively with ZipOutputStream in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button