0

I am trying to compress my file using the ZipOutPutStream. I tried below code and got the compressed folder with the file. But when I am extracting the folder using 7Zip, the fileName extension is missing. Also I am unable to extract the folder by normal Extract option provided in Windows.

Below is the code I tried Using Java -8

public void compress(String compressed , String raw) {

    Path pCompressed = null;
    try {
        pCompressed = Files.createFile(Paths.get(compressed));

    try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(pCompressed))) {
        Path pRaw = Paths.get(raw);
        Files.walk(pRaw).filter(path -> !Files.isDirectory(path)).forEach(path -> {
              ZipEntry zipEntry = new ZipEntry(pRaw.relativize(path).toString());
              try {
                  zos.putNextEntry(zipEntry);
                  Files.copy(path, zos);
                  zos.closeEntry();
            } catch (IOException e) {
               logger.error("Exception while copying file to compressed Directory: "+e);
            }
          });
    }
   catch(IOException ioe) {
       logger.error("Exception while compressing the output file: "+ioe);
   }
    }
    catch (IOException e1) {
        logger.error("Exception While Path initialization for compressing the file");
    }
}

Expecting : After Extraction someFolder/MyFile.csv

1 Answers1

0

I've just tried this code and when I provided it with compressed = "out.zip" and raw = "testdir", it worked fine for me. It produced a zip file containing the contents of testdir. I was then able to extract this with 7Zip and the built in Windows extraction and the files were all present and correct.

My guess is that you have not specified the .zip extension for the output file or that Windows is hiding the extension in the folder view. (I think it does this by default.)

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66