Hello,
I would like to write a new text file DIRECTLY to a new zip file. Right now, I write the text file to disk, transfer it to the ZIP file, then delete the original text file. Here is the code:
String[] names = {"Tom", "Dick", "Harry"};
String tempDir = System.getProperty("java.io.tmpdir");
File actionFile = new File(tempDir,"temp.txt");
byte[] buf = new byte[1024];
int len;
// Write the text file
BufferedWriter bw = new BufferedWriter(new FileWriter(actionFile));
for (int i = 0; i < names.length; i++) {
bw.append(names);
bw.newLine();
}
bw.close();
// Create a zip file containing temp.txt
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("/home/myZipFile.zip"));
zos.putNextEntry(new ZipEntry("temp.txt"));
// Transfer bytes from the text file to the ZIP file.
FileInputStream in = new FileInputStream(actionFile);
while ((len = in.read(buf)) > 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
zos.close();
// Delete the original text file.
actionFile.delete();
I would like to know if their is a more direct way of writing the text file directly to zip file without having to create a temporary file on disk? Thanks,