1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
//These are the files to include in the ZIP file
String[] filenames = new String[]{"C:\\sysiclog.txt", "C:\\Montreal_Twilight_Panorama_2006.jpg"};
// Create a buffer for reading the files
byte[] buf = new byte[1024];
ZipOutputStream out = null ;
FileInputStream in = null;
try {
// Create the ZIP file
String outFilename = "outfile.zip";
out = new ZipOutputStream(new FileOutputStream(outFilename));
// Compress the files
for (int i=0; i<filenames.length; i++)
{
in = new FileInputStream(filenames[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames[i]));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
} // Complete the ZIP file
out.close();
}
catch (IOException e) {
System.out.println( e.toString());
}
catch (Exception e) {
System.out.println( e.toString());
}
finally{
out.close();
in.close();
}
} |
Partager