gpt4 book ai didi

java - 解压缩(提取 zip)文件显示错误,存档意外结束

转载 作者:行者123 更新时间:2023-11-29 03:20:23 25 4
gpt4 key购买 nike

我在这几个文件夹中有 Bookfolder(英语、印地语、日语)。将英语、印地语、日语转换为英语.zip、印地语.zip 和 japanese.zip。一切正常,我正在保存 zip 文件和 Bookfolder 中的文件夹,我正在用 java 做这件事。但是当我手动解压缩 zip 文件 ex:english.zip 时,右键单击此处的提取物,然后将错误显示为 UNEXPECTED END OF ARCHIVE。这是我的代码.

public void foldertToZip(File zipDeleteFile) {
//System.out.println(zipDeleteFile);
File directoryToZip = zipDeleteFile;
List<File> fileList = new ArrayList<>();
//System.out.println("---Getting references to all files in: " + directoryToZip.getCanonicalPath());
getAllFiles(directoryToZip, fileList);
//System.out.println("---Creating zip file");
writeZipFile(directoryToZip, fileList);
//System.out.println("---Done");
}

public static void getAllFiles(File dir, List<File> fileList) {
try {
File[] files = dir.listFiles();
for (File file : files) {
fileList.add(file);
if (file.isDirectory()) {
System.out.println("directory:" + file.getCanonicalPath());
getAllFiles(file, fileList);
} else {
System.out.println("file:" + file.getCanonicalPath());
}
}
} catch (IOException e) {
}
}

public static void writeZipFile(File directoryToZip, List<File> fileList) {
try {
//try (FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip"); ZipOutputStream zos = new ZipOutputStream(fos)) {
File path = directoryToZip.getParentFile();
File zipFile = new File(path, directoryToZip.getName() + ".zip");
try (FileOutputStream fos = new FileOutputStream(zipFile)) {
ZipOutputStream zos = new ZipOutputStream(fos);
for (File file : fileList) {
if (!file.isDirectory()) { // we only zip files, not directories
addToZip(directoryToZip, file,zos);
}
}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

public static void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException,
IOException {
try (FileInputStream fis = new FileInputStream(file)) {
String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
file.getCanonicalPath().length());
System.out.println("Writing '" + zipFilePath + "' to zip file");
ZipEntry zipEntry = new ZipEntry(zipFilePath);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
}
}`

当我提取新的 zip 文件(例如:english.zip)时,它显示错误为 archieve 的意外结束(我认为没有完全压缩)'

最佳答案

需要在writeZipFile()方法中关闭ZipOutputStream

for (File file : fileList) {
if (!file.isDirectory()) { // we only zip files, not directories
addToZip(directoryToZip, file,zos);
}
}
//here close zos
zos.close();

关于java - 解压缩(提取 zip)文件显示错误,存档意外结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24011735/

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