Creating a ZIP file in Java

Ads
 

Creating a ZIP file in Java

This section teaches you about the ZIP file with java program.

This section teaches you about the ZIP file with java program.

Creating a ZIP file in Java

     

Zip File: Zip file format is the popular method of data compression. Zip file contains many files in compressed format. You can say the files are stored in the zip format with compression. The file which is in the zip format holds the ".zip" extension.

You can make executable file in zip format. You can archive data from the zip file by extracting it. For create a zip file from other type of file, following program gives you the best way of making it possible through you code in java application.

Program Result:

This program makes a zip file from only one file which is given by you. It takes a file to make it into zip format and it takes the zip file name which has to be created from the given file. Finally, it makes the zip file. You can archive the file from the zip file by extracting, if you want.

Code Description:

Several methods and APIs are used in the following program for making a zip file from the given file. These are explained as follows:

ZipOutputStream:
ZipOutputStream is the class of java.util.zip.*; package. This class is used to compression the file whether the file is compressed on uncompressed.

ZipOutputStream.setLevel(Deflater.DEFAULT_COMPRESSION):
Above mentioned is the setLevel() method of ZipOutputStream class which sets the level of the compression process. This method takes a argument which is the built-in field of the Deflater class.

Deflater:
This is the class of java.util.zip.*; package. This class provides the compression using the ZLIB compression library.

Here is the code of the program:

import java.io.*;
import java.util.zip.*;

public class ZipCreateExample{
  public static void main(String[] argsthrows IOException{
  System.out.print("Please enter file name to zip : ");
  BufferedReader input = new BufferedReader
(
new InputStreamReader(System.in));
  String filesToZip = input.readLine();
  File f = new File(filesToZip);
  if(!f.exists()){
  System.out.println("File not found.");
  System.exit(0);
  }
  System.out.print("Please enter zip file name : ");
  String zipFileName = input.readLine();
  if (!zipFileName.endsWith(".zip"))
  zipFileName = zipFileName + ".zip";
  byte[] buffer = new byte[18024];
  try{
  ZipOutputStream out = new ZipOutputStream
(
new FileOutputStream(zipFileName));
  out.setLevel(Deflater.DEFAULT_COMPRESSION);
  FileInputStream in = new FileInputStream(filesToZip);
  out.putNextEntry(new ZipEntry(filesToZip));
  int len;
  while ((len = in.read(buffer)) 0){
  out.write(buffer, 0, len);
  }
  out.closeEntry();
  in.close();
  out.close();
  }
  catch (IllegalArgumentException iae){
  iae.printStackTrace();
  System.exit(0);
  }
  catch (FileNotFoundException fnfe){
  fnfe.printStackTrace();
  System.exit(0);
  }
  catch (IOException ioe){
  ioe.printStackTrace();
  System.exit(0);
  }
  }
}

Download this example.

Ads

Ads