gpt4 book ai didi

java//解压报错:MALFORMED

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:54 28 4
gpt4 key购买 nike

我想递归解压缩一些存档 .zip。我使用 java.util.zip,但无法使用其他库。

我的代码:

public static void unzip(String file) {

try {
File fSourceZip = new File(file);
String zipPath = file.substring(0, file.length() - 4);
File temp = new File(zipPath);
temp.mkdir();
System.out.println(zipPath + " created");


ZipFile zipFile = new ZipFile(fSourceZip);
Enumeration e = zipFile.entries();

while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry) e.nextElement();
File destinationFilePath = new File(zipPath, entry.getName());

destinationFilePath.getParentFile().mkdirs();

if (entry.isDirectory()) {
continue;
} else {
System.out.println("Extracting " + destinationFilePath);

BufferedInputStream bis = new BufferedInputStream(
zipFile.getInputStream(entry));

int b;
byte buffer[] = new byte[1024];


FileOutputStream fos = new FileOutputStream(
destinationFilePath);
BufferedOutputStream bos = new BufferedOutputStream(fos,
1024);

while ((b = bis.read(buffer, 0, 1024)) != -1) {
bos.write(buffer, 0, b);
}

bos.flush();
bos.close();
bis.close();
}
if (entry.getName().endsWith(".zip")) {
// found a zip file, try to open
unzip(destinationFilePath.getAbsolutePath());
}
}

} catch (IOException ioe) {
System.out.println("IOError :" + ioe);
}
}

但是我的一些存档有一些错误:

Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED
at java.util.zip.ZipCoder.toString(ZipCoder.java:58)
at java.util.zip.ZipFile.getZipEntry(ZipFile.java:567)
at java.util.zip.ZipFile.access$900(ZipFile.java:61)
at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:525)
at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:500)
at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:481)
at zip.ReadingArchive.unzip(ReadingArchive.java:36)
at zip.ReadingArchive.unzip(ReadingArchive.java:82)
at zip.ReadingArchive.unzip(ReadingArchive.java:82)
at main.Main.main(Main.java:13)

我遇到这个问题是因为我的存档中有 .odp。我怎么能说只使用 .zip,而不使用其他文件?我该如何解决这个问题?

谢谢!

最佳答案

我刚刚通过指定替代(非 UTF-8)字符集修复了它:

Charset CP866 = Charset.forName("CP866");
ZipFile zipFile = new ZipFile(zipArchive, CP866);

在您的情况下,您需要指定另一个字符集。例如,尝试 CP437

关于java//解压报错:MALFORMED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20013091/

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