简体   繁体   中英

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

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
    }

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.

i had the same problem zip file were not extracted and was shown as empty the problem was in the folder names in zip file. If folder name consist of > or < symbols i saw the failure . SO in the code new ZipEntry(file.toString()) i try to clean up any folder names with those particular symbols

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM