无法提取从Java创建的zip文件

时间:2019-03-19 16:42:02

标签: java zip gnome ziparchive zipoutputstream

我编写了一个小的Java程序,如下所示,使用提供的目录创建zip归档文件。

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();
            }
        }
    }
}

档案创建中使用的/home/kasun/Downloads/sample/resources目录包含以下层次结构

 ~/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

上面的代码正确创建了zip文件,并且可以通过在终端中执行以下命令来提取该文件,

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

但是,当从UI解压缩时,发生以下错误 enter image description here

我在java 1.8.0_171上使用Ubuntu 18.04

==== 根据注释中的要求,请找到-v输出,如下所示,

 ✘  ~/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

1 个答案:

答案 0 :(得分:0)

我能够通过删除zip文件中的目录创建逻辑来解决此问题。

请找到以下工作代码,

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();
            }
        }
    }
}

unzip -v命令的输出上方,如下所示,

 ~/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