import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class createZip { public static void main(String args[]) { try { //Providing input files with full path String zipFile = "C:/Documents and Settings/468947/Desktop/a.zip"; String[] sourceFiles = {"C:/Documents and Settings/468947/Desktop/a.txt", "C:/Documents and Settings/468947/Desktop/b.txt"}; byte[] buffer = new byte[1024]; FileOutputStream fout = new FileOutputStream(zipFile); ZipOutputStream zout = new ZipOutputStream(fout); for(int i=0; i < sourceFiles.length; i++) { System.out.println("File given " + sourceFiles[i]); FileInputStream fin = new FileInputStream(sourceFiles[i]); //passing the files to ZipEntry zout.putNextEntry(new ZipEntry(sourceFiles[i])); int length; while((length = fin.read(buffer)) > 0) { zout.write(buffer, 0, length); } zout.closeEntry(); fin.close(); } zout.close(); System.out.println("Zip file is created successfully"); } catch(IOException ioe) { System.out.println("IOException :" + ioe); } } }