从 ResourceStream 中提取压缩文件会引发错误“存储的块长度无效”
2022-09-04 20:23:55
我正在尝试使用以下命令从当前JAR中提取ZIP文件:
InputStream resource = getClass().getClassLoader().getResourceAsStream(name);
这得到了正确的,但是当我尝试使用以下代码解压缩它时,它给出了一个错误(我将每个文件存储到一个):InputStream
Hashmap<file, filename>
public static HashMap<String, String> readZip(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
HashMap<String, String> list = new HashMap<>();
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry entry = zipInputStream.getNextEntry();
while (entry != null) {
if (!entry.isDirectory()) {
StringBuilder stringBuilder = new StringBuilder();
while (IOUtils.read(zipInputStream, buffer) > 0) {
stringBuilder.append(new String(buffer, "UTF-8"));
}
list.put(stringBuilder.toString(), entry.getName());
}
zipInputStream.closeEntry();
entry = zipInputStream.getNextEntry();
}
zipInputStream.closeEntry();
zipInputStream.close();
return list;
}
但是,当我尝试执行此操作时,我得到此异常(在IOUtils.read
)
java.util.zip.ZipException: invalid stored block lengths
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.util.zip.ZipInputStream.read(Unknown Source)
我做错了吗?我已经对错误进行了大量的谷歌搜索,但我没有看到与我的问题相关的任何内容。