Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

zip file does not contain any text file ??

 
Theeranit Pongtongmuang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Theeranit Pongtongmuang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your welcome ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic