我试图从我当前的JAR中提取ZIP文件:
InputStream resource = getClass().getClassLoader().getResourceAsStream(name);
这会得到正确的InputStream,但是当我尝试使用以下代码解压缩它时会出错(我将每个文件存储到Hashmap< file,filename>):
public static HashMap readZip(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
HashMap 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)
我做错了吗?我已经做了很多关于错误的谷歌搜索,我没有看到与我的问题有关的任何内容.