Source Code : Zip a list of file into one zip file.

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Zip a list of file into one zip file.

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

public class Utils {
  /**
   * Zip a list of file into one zip file.
   * 
   * @param files
   *          files to zip
   * @param targetZipFile
   *          target zip file
   * @throws IOException
   *           IO error exception can be thrown when copying ...
   */
  public static void zipFile(final File[] files, final File targetZipFile) throws IOException {
    try {
      FileOutputStream   fos = new FileOutputStream(targetZipFile);
      ZipOutputStream zos = new ZipOutputStream(fos);
      byte[] buffer = new byte[128];
      for (int i = 0; i < files.length; i++) {
        File currentFile = files[i];
        if (!currentFile.isDirectory()) {
          ZipEntry entry = new ZipEntry(currentFile.getName());
          FileInputStream fis = new FileInputStream(currentFile);
          zos.putNextEntry(entry);
          int read = 0;
          while ((read = fis.read(buffer)) != -1) {
            zos.write(buffer, 0, read);
          }
          zos.closeEntry();
          fis.close();
        }
      }
      zos.close();
      fos.close();
    } catch (FileNotFoundException e) {
      System.out.println("File not found : " + e);
    }

  }
}

   
    
    
  

Thank with us