gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:06:58 25 4
gpt4 key购买 nike

我编写了一个如下所示的小 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

我在 Ubuntu 18.04 上使用 java 1.8.0_171

====根据评论中的要求,请找到 -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

最佳答案

我能够通过删除 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

关于java - 无法提取从 Java 创建的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55246018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com