Hello everyone
I've tried to create zip file from multiple text file.
This is code snippet.
// These are the files to include in the ZIP file
String[] filenames = new String[]{"c:\\temp\\text1.txt", "c:\\temp\\text2.txt"};
// Create a buffer for reading the files
byte[] buf = new byte[1024];
try {
// Create the ZIP file
String outFilename = "c:\\temp\\zipOut.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
// Compress the files
for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames[i].replace(File.separatorChar,'/')));
// 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) {
e.printStackTrace();
}
There is zip file after I 've run this code. However, there is nothing in zip file no text files that I expected.
Please could you help to solve this problem. It's take me an hour since I tried to do it myself.
Thanks,