Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

writing a text file directly to a ZIP

807588Mar 3 2009 — edited Mar 3 2009
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,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 31 2009
Added on Mar 3 2009
3 comments
316 views