tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Zip Utilities > Create Zip File

Create Zip File 

The following example shows creating a ZIP file with user given list of files. The method zipOut.putNextEntry() adds a new file entry to the ZIP file. Remove the drive prefix before making an entry to build the ZIP file in proper format.

File Name  :  
com/bethecoder/tutorials/utils/zip/CreateZipFile.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.zip;

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 CreateZipFile {

  /**
   @param args
   */
  public static void main(String[] args) {

    String zipFilePath = "C:\\abcd.zip"
    String [] filePaths = {
        "C:\\abc.txt",
        "C:\\ONE\\test.txt",
        "C:\\NEW\\file_ops.txt",
        "C:\\Test\\Test.java"
    };
    
    createZipFile(zipFilePath, filePaths);
    System.out.println("Successfully created zip file.");
  }

  public static void createZipFile(String zipFilePath, String [] filePaths) {
      
      try {
          /**
           * Create ZIP file
           */
          ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
          FileInputStream fileInput = null;
          byte [] buf = new byte[1024];
          String entryName = null;
          int index = 0;
          
          /**
           * Add each file as ZIP entry
           */
          for (int i = ; i < filePaths.length; i++) {
            fileInput = new FileInputStream(filePaths[i]);
              entryName = filePaths[i];

              /**
               * Remove the drive prefix,
               
               * C:\NEW2\file_ops.txt   to NEW2\file_ops.txt
               * C:\NEW2\CCCC\AAA\IPC.LOG to NEW2\CCCC\AAA\IPC.LOG
               
               * If we don't remove the prefix, a directory
               * named C: is getting created in the ZIP file.
               * To avoid this remove the directory prefix.
               */
              index = entryName.indexOf(File.separator);
              
              if (index > 0) {
                entryName = entryName.substring(index + 1);
              }
              
              /**
               * Add ZIP entry
               */
              System.out.println("Adding file : " + filePaths[i]);
              zipOut.putNextEntry(new ZipEntry(entryName));
      
              /**
               * Copy file content to ZIP stream
               */
              int len;
              while ((len = fileInput.read(buf)) 0) {
                zipOut.write(buf, 0, len);
              }
      
              /**
               * Close ZIP entry 
               */
              zipOut.closeEntry();
              fileInput.close();
          }
      
          /**
           * Close ZIP stream
           */
          zipOut.close();
          
      catch (IOException e) {
        e.printStackTrace();
      }

  }
}
   

It gives the following output,
Adding file : C:\abc.txt
Adding file : C:\ONE\test.txt
Adding file : C:\NEW\file_ops.txt
Adding file : C:\Test\Test.java
Successfully created zip file.



 
  


  
bl  br