ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Computer Programming Tutorials

How to create a Zip File in Java ?.

Updated on September 17, 2014

Introduction

In this tutorial, you will learn how to create a zip file in Java. Java has very vast API to deal with various such problems. Java API contains various classes and streams that help us in creating a zip file. A zip file is nothing but a compressed file saving memory as well as a handy format in storing multiple files together. In this simple tutorial we will create a zip file by name samples.zip through Java code. This zip file will contain two already created text files such as sample1.txt and sample2.txt. You can create zip with any of the file format files into it. This Java utility is really a good tool in creating a zip file. You can use this source code in creation of various tools which outputs multiple files. The tutorial contains a video tutorial and highly commented Java code below. Just browse and watch video tutorial to understand zip file creation concept through Java.

Java video tutorial - How to create a zip file in Java.

Java source code - How to create a zip file in Java.

// Step 1 - Import classes present in Java API such as File, FileInputStream,
// FileOutputStream, ZipEntry and ZipOutputStream.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

// Step 2 - Create a Java class CreateZip having a main method. This method
// is the starting point of execution.
public class CreateZip {

   public static void main(String[] args) {

	try {
	// Step 3 - In order to create a zip file, we have to create a FileOutputStream
	// which will refer zip files as samples.zip. 
	FileOutputStream fos = new FileOutputStream("samples.zip");
		
	// Step 4 - In order to write contents/files in sample.zip, we have to create a
	// ZipOutputStream. This input stream helps in writing files to samples.zip 
	// with the help of FileOutputStream created above. 
	ZipOutputStream zos = new ZipOutputStream(fos);

	// Step 5 - Create two Strings pointing to files with actual location appended to it. 
	// Lets say we want to create a zip file holding sample1.txt and sample2.txt.  
	String file1 = "sample1.txt";
	String file2 = "sample2.txt";

	// Step 6 - In order to add these two text files to samples.zip file, we call a static method
	// that takes in a ZipOutputStream and file to add into it. We call this method twice to add
	// one file at a time to zip. Initially, there are only two files sample1.txt and sample2.txt as 
	// shown in first fig. below
	add( zos , file1 );
	add( zos , file2 );

	// Step 14 - Close ZipOutputStream and FileOutputStream after creating zip file.
	zos.close();
	fos.close(); 
	} catch (Exception e) {
	}  	}

        // Step 7 - Create a static method by name add. It takes ZipOutputStream and file location + file name.
	public  static void add(ZipOutputStream zos, String fileName) throws Exception {

	// Step 8 - Create a File from the fileName String passed to it.
	File file = new File(fileName);
		
	// Step 9 - Create a FileInputStream with the file created above. 
	FileInputStream fis = new FileInputStream(file);
		
	// Step 10 - In order to create a zip file with multiple files, we have to create ZipEntry for each file.
	// ZipEntry is created for each and every file to be added. This ZipEntry instance takes 
	// in a fileName passed to method. ZipOutputStream has a method putNextEntry which 
	// takes in a ZipEntry instance for each and every file we want to put in zip.
	ZipEntry zipEntry = new ZipEntry(fileName);
	zos.putNextEntry(zipEntry);

	// Step 11 - After placing a ZipEntry in ZipOutputStream, now its time to put actual contents
	// of file to zip. Create a byte array taking 1024 size of array. 
	byte[] bytes = new byte[1024];
	int length;
		
	// Step 12 - Using ZipOutputStream's write method write contents of file to ZipEntry file. We use while 
	// loop to write contents of file to zip till file is completely written.
	while ((length = fis.read(bytes)) >= 0) {
		zos.write(bytes, 0, length);
	}
		
	// Step 13 - Close ZipEntry instance and FileInputStream.
	zos.closeEntry();
	fis.close();
	}
}

Let say we want to create a zip file by name samples.zip. This zip file must contain two files such as sample1.txt and sample2.txt. Before running the above Java program there are only two sample txt files as shown below.

Before creation of zip file

After running CreateZip Java class, it creates a zip file by name samples.zip. This zip file has two files such as sample1.txt and sample2.txt. After running above Java program structure of file system changes and it creates samples.zip as shown below.

After creation of zip files

Output of the program -

After running CreateZip class, it creates a zip file by name samples.zip. Let's open this zip file as shown in below image. It contains two text files such as sample1.txt and sample2.txt. Thus, it conforms creation of zip file through Java code demonstrated above.

Contents of zip file "samples.zip"

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.