aspose file tools*
The moose likes I/O and Streams and the fly likes zip file does not contain any text file ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "zip file does not contain any text file ??" Watch "zip file does not contain any text file ??" New topic
Author

zip file does not contain any text file ??

Theeranit Pongtongmuang
Greenhorn

Joined: May 17, 2009
Posts: 19
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,
Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
Hi Theeranit,

The good news is that the code you pasted here, working fine for me.

I didn't change any single line.

Please verify the location of text files, whether they actually exist at c:\\temp
and name of files are text1.txt and text2.txt.


Thanks,
Tanzy.


Roll with punchers, there is always tomorrow.
Techie Blog -- http://jtanzy.blogspot.com/
Theeranit Pongtongmuang
Greenhorn

Joined: May 17, 2009
Posts: 19
Thank jtanzy for your helping.

However, I can solve this problem. I think method putNextEntry(new ZipEntry()) that accept ZipEntry and for ZipEntry constructor.
I refer the file only file name. No need for full path. Then I's done.

For example

out.putNextEntry(new ZipEntry(text1.txt));
Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
Your welcome ..
 
jQuery in Action, 2nd edition
 
subject: zip file does not contain any text file ??