Java examples for File Path IO:ZipOutputStream
Create Zip File From Directory using ZipOutputStream
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 main(String args[]) { try {// w w w . j a v a2 s . c o m String zipFile = "C:/Folder/zipdemo.zip"; String sourceDirectory = "C:/examples"; // create byte buffer byte[] buffer = new byte[1024]; FileOutputStream fout = new FileOutputStream(zipFile); ZipOutputStream zout = new ZipOutputStream(fout); File dir = new File(sourceDirectory); if (!dir.isDirectory()) { System.out.println(sourceDirectory + " is not a directory"); } else { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { System.out.println("Adding " + files[i].getName()); // create object of FileInputStream for source file FileInputStream fin = new FileInputStream(files[i]); zout.putNextEntry(new ZipEntry(files[i].getName())); int length; while ((length = fin.read(buffer)) > 0) { zout.write(buffer, 0, length); } zout.closeEntry(); // close the InputStream fin.close(); } } // close the ZipOutputStream zout.close(); System.out.println("Zip file has been created!"); } catch (IOException ioe) { System.out.println("IOException :" + ioe); } } }