Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZIP file created using Java showing empty when open with Windows Explorer

Tags:

java

zip

The below code is used to zip normal text file. When I extract using WinRaR its showing the content properly, but when I open with Windows Explorer its empty, no file listed. I am using Windows 7 Enterprise (64 bit) operating system. Any idea why its not listing in Windows explorer? Thanks in advance.

File file = new File("F:\\sample.txt");
    byte[] buf = new byte[1024];
    String outFilename = "F:\\zipped_sample.zip";
    try {
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
      FileInputStream in = new FileInputStream(file);
      out.putNextEntry(new ZipEntry(file.toString()));
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
        out.flush();
      }
      out.closeEntry();
      out.close();
      in.close();
    } catch (Exception e) {
      // log exception here
    }
like image 765
Sridhar Avatar asked Oct 24 '25 14:10

Sridhar


1 Answers

ZipEntry constructor takes name but you are providing it a path by doing file.toString(); Try:

New ZipEntry(file.getName());

This will pass the file name.

like image 54
webDeveloper Avatar answered Oct 26 '25 05:10

webDeveloper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!