zip

Create zip file from directory with ZipOutputStream

In this example we shall show you how to create a zip file from a directory with ZipOutputStream class, that is an output stream filter for writing files in the ZIP file format. To create a zip file from a directory with ZipOutputStream one should perform the following steps:

  • Create a FileOutputStream to write to the file with the specified name, that is the zipFile.
  • Create a new ZipOutputStream from the FileOutputStream, that is an output stream filter for writing files in the ZIP file format.
  • Create a new File instance by the given pathname of the file.
  • Use listFiles() API method of File in order to get the array of Files of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
  • For each one of the Files in the array, create a FileInputStream by opening a connection to the file and a new ZipEntry with the name of the specified file, that begins writing it to the ZipOutputStream. The default compression method will be used if no compression method was specified for the entry.
  • Read up to 1024 bytes of data from the file into an array of bytes, using the read(byte[] b) API method of FileInputStream and write the data to the current ZipEntry , using write(byte[] b, int off, int len) method of ZipOutputStream.
  • Close the ZipEntry, the ZipOutputStream and the FileInputStream, using their closeEntry() and close() API methods,

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 CreateZipFileFromDirectoryWithZipOutputStream {
	
	public static void main(String[] args) {
		
		String zipFile = "C:/archive.zip";
		String srcDir = "C:/foldertocompress";
		
		try {
			
			// create byte buffer
			byte[] buffer = new byte[1024];

			FileOutputStream fos = new FileOutputStream(zipFile);

			ZipOutputStream zos = new ZipOutputStream(fos);

			File dir = new File(srcDir);

			File[] files = dir.listFiles();

			for (int i = 0; i < files.length; i++) {
				
				System.out.println("Adding file: " + files[i].getName());

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

				// begin writing a new ZIP entry, positions the stream to the start of the entry data
				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();
			}

			// close the ZipOutputStream
			zos.close();
			
		}
		catch (IOException ioe) {
			System.out.println("Error creating zip file" + ioe);
		}
		
	}

}

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

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron 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.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
slave
slave
3 years ago

thanks

Back to top button