Search code examples
javazipgnomephp-ziparchivezipoutputstream

Unable to extract zip files created from Java


I have written a small Java programme as follows to create a zip archive using a provided directory.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Demo {

    public static void main(String[] args) {

        FileOutputStream destinationZipOutputStream = null;
        try {
            destinationZipOutputStream = new FileOutputStream("/home/kasun/Downloads/sample/test/compress.zip");
                    } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ZipOutputStream updateZipOutputStream = new ZipOutputStream(destinationZipOutputStream);
        updateZipOutputStream.setLevel(ZipOutputStream.STORED);
        File sourceFile = new File("/home/kasun/Downloads/sample/resources");
        try {
            zipFile(sourceFile, "parentdir", updateZipOutputStream);
            updateZipOutputStream.close();
            destinationZipOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void zipFile(File fileToZip, String fileName, ZipOutputStream updateZipOutputStream) throws
            IOException {

        if (fileToZip.isHidden()) {
            return;
        }
        if (fileToZip.isDirectory()) {
            // Creating directories with in the zip file
            if (fileName.endsWith("/")) {
                updateZipOutputStream.putNextEntry(new ZipEntry(fileName));
                updateZipOutputStream.closeEntry();
            } else {
                updateZipOutputStream.putNextEntry(new ZipEntry(fileName + "/"));
                updateZipOutputStream.closeEntry();
            }
            File[] childFiles = fileToZip.listFiles();
            if (childFiles == null) {
                throw new IOException("IOException occurred when getting list of files of directory " +
                        fileToZip.toString());
            }
            for (File file : childFiles) {
                zipFile(file, fileName + "/" + file.getName(), updateZipOutputStream);
            }
        } else {
            // Creating files with in the zip file
            try (FileInputStream sourceInputStream = new FileInputStream(fileToZip)) {
                updateZipOutputStream.putNextEntry(new ZipEntry(fileName));
                byte[] buffer = new byte[1024];
                int length;
                while ((length = sourceInputStream.read(buffer)) > 0) {
                    updateZipOutputStream.write(buffer, 0, length);
                }
                updateZipOutputStream.closeEntry();
            }
        }
    }
}

The /home/kasun/Downloads/sample/resources directory used in archive creation contains the following hierarchy

 ~/Downloads/sample/resources  pwd
/home/kasun/Downloads/sample/resources
 ~/Downloads/sample/resources  tree 
.
├── sampledir
│   └── a
│       └── b
│           └── c
│               └── sample.jar
├── sampleFile1.txt
├── sampleFile2.txt
└── sampleFile3.yaml

4 directories, 4 files

The above code creates the zip file correctly and it is able to be extracted by executing the following command in terminal,

unzip -q /home/kasun/Downloads/sample/test/compress.zip

However, when unzipping from the UI the following error occurs enter image description here

I am using java 1.8.0_171 on Ubuntu 18.04

==== As requested in comments please find the -v output as follows,

 ✘  ~/Downloads/sample/test  unzip -v compress.zip 
Archive:  compress.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Defl:N        5   0% 2019-03-20 13:55 00000000  parentdir/
    6147  Defl:N     6152  -0% 2019-03-20 13:55 a8ae3168  parentdir/sampleFile1.txt
     642  Defl:N      647  -1% 2019-03-20 13:55 bb00089f  parentdir/sampleFile3.yaml
       0  Defl:N        5   0% 2019-03-20 13:55 00000000  parentdir/sampledir/
       0  Defl:N        5   0% 2019-03-20 13:55 00000000  parentdir/sampledir/a/
       0  Defl:N        5   0% 2019-03-20 13:55 00000000  parentdir/sampledir/a/b/
       0  Defl:N        5   0% 2019-03-20 13:55 00000000  parentdir/sampledir/a/b/c/
  275541  Defl:N   275586   0% 2019-03-20 13:55 d83c1ab3  parentdir/sampledir/a/b/c/sample.jar
      97  Defl:N      102  -5% 2019-03-20 13:55 8b690c25  parentdir/sampleFile2.txt
--------          -------  ---                            -------
  282427           282512   0%                            9 files

Solution

  • I was able to resolve the issue by removing the directory creation logic inside the zip file.

    Please find the working code as follows,

    package zip.extraction.error;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    public class Demo {
    
        public static void main(String[] args) {
    
            FileOutputStream destinationZipOutputStream = null;
            try {
                destinationZipOutputStream = new FileOutputStream("/home/kasun/Downloads/sample/test/compress.zip");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            ZipOutputStream updateZipOutputStream = new ZipOutputStream(destinationZipOutputStream);
            updateZipOutputStream.setLevel(ZipOutputStream.STORED);
            File sourceFile = new File("/home/kasun/Downloads/sample/resources");
            try {
                zipFile(sourceFile, "parentdir", updateZipOutputStream);
                updateZipOutputStream.close();
                destinationZipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        private static void zipFile(File fileToZip, String fileName, ZipOutputStream updateZipOutputStream) throws
                IOException {
    
            if (fileToZip.isHidden()) {
                return;
            }
            if (fileToZip.isDirectory()) {
                // Creating directories with in the zip file
    /*          if (fileName.endsWith("/")) {
                    updateZipOutputStream.putNextEntry(new ZipEntry(fileName));
                    updateZipOutputStream.closeEntry();
                } else {
                    updateZi`pOutputStream.putNextEntry(new ZipEntry(fileName + "/"));
                    updateZipOutputStream.closeEntry();
                }*/
                File[] childFiles = fileToZip.listFiles();
                if (childFiles == null) {
                    throw new IOException("IOException occurred when getting list of files of directory " +
                            fileToZip.toString());
                }
                for (File file : childFiles) {
                    zipFile(file, fileName + "/" + file.getName(), updateZipOutputStream);
                }
            } else {
                // Creating files with in the zip file
                try (FileInputStream sourceInputStream = new FileInputStream(fileToZip)) {
                    updateZipOutputStream.putNextEntry(new ZipEntry(fileName));
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = sourceInputStream.read(buffer)) > 0) {
                        updateZipOutputStream.write(buffer, 0, length);
                    }
                    updateZipOutputStream.closeEntry();
                }
            }
        }
    }
    

    Upon above output of the unzip -v command was as follows,

     ~/Downloads/sample/test  unzip -v compress.zip 
    Archive:  compress.zip
     Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
    --------  ------  ------- ---- ---------- ----- --------  ----
        6147  Defl:N     6152  -0% 2019-03-20 14:01 a8ae3168  parentdir/sampleFile1.txt
         642  Defl:N      647  -1% 2019-03-20 14:01 bb00089f  parentdir/sampleFile3.yaml
      275541  Defl:N   275586   0% 2019-03-20 14:01 d83c1ab3  parentdir/sampledir/a/b/c/sample.jar
          97  Defl:N      102  -5% 2019-03-20 14:01 8b690c25  parentdir/sampleFile2.txt
    --------          -------  ---                            -------
      282427           282487   0%                            4 files