Here you can find the source of zip(String[] sourceFiles, String zipFile, String directory)
public static void zip(String[] sourceFiles, String zipFile, String directory)
//package com.java2s; //License from project: Open Source License 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 Main { public static void zip(String[] sourceFiles, String zipFile, String directory) { try {/*w w w . jav a 2s .c o m*/ zipFile = directory + File.separatorChar + zipFile; // create byte buffer byte[] buffer = new byte[65535]; int progress = 0; FileOutputStream fileOutputStream = new FileOutputStream(zipFile); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream); for (int i = 0; i < sourceFiles.length; i++) { sourceFiles[i] = directory + File.separatorChar + sourceFiles[i]; File srcFile = new File(sourceFiles[i]); FileInputStream fileInputStream = new FileInputStream(srcFile); // begin writing a new ZIP entry, positions the stream to the start of the entry data zipOutputStream.putNextEntry(new ZipEntry(srcFile.getName())); int length; while ((length = fileInputStream.read(buffer)) > 0) { zipOutputStream.write(buffer, 0, length); progress += length; System.out.println("Progress : " + progress); } zipOutputStream.closeEntry(); // close the InputStream fileInputStream.close(); } // close the ZipOutputStream zipOutputStream.close(); } catch (IOException e) { System.out.println("Error creating zip file: " + e); } } }