从ResourceStream提取压缩文件会引发错误“无效的存储块长度". [英] Extracting zipped file from ResourceStream throws error "Invalid stored block lengths"
问题描述
我正在尝试使用以下方法从当前的JAR中提取ZIP文件:
I am trying to extract a ZIP file from my current JAR using:
InputStream resource = getClass().getClassLoader().getResourceAsStream(name);
这将获得正确的InputStream
,但是当我尝试使用以下代码将其解压缩时,它给出了一个错误(我将每个文件存储到Hashmap<file, filename>
中):
This get the correct InputStream
, but it gives an error when I try to unzip it using the following code (I'm storing each file into a 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
上)
However when I try to do this, I get this exception (on 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)
我做错了吗?我已经对错误进行了大量的搜索,但没有发现与我的问题有关的任何信息.
Am I doing this wrong? I've done plenty of googling of the error, and I didn't see anything related to my issue.
推荐答案
感谢@PaulBGD的上述回答.它节省了我几个小时来弄清楚我的系统发生了什么,我在我的pom.xml
文件中添加了以下新代码片段(在阅读Paul的答案之前,我没有意识到这是根本原因):
Thanks to @PaulBGD's answer above. It saved me hours to figure out what happened to my system I add the following new snippets into my pom.xml
file (which I didn't realize is the root cause before reading Paul's answer):
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.version</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.csv</include>
<include>**/*.txt</include>
<include>**/*.gif</include>
<include>**/*.json</include>
<include>**/*.xlsx</include>
<include>rythm/**</include>
</includes>
</resource>
</resources>
但是我没有直接接受Paul的回答,相反,我认为这些xlsx
文件不应该通过资源插件的过滤管道,因此我在pom中添加了另一个resource
:
However I didn't take Paul's answer directly, instead I don't think those xlsx
files should go through the resource plugin's filtering pipeline, thus I added another resource
into pom:
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xlsx</include>
</includes>
</resource>
它解决了我的问题,而没有将源编码设置从UTF-8
更改为ISO-8859-1
And it fixed my problem without changing the source encoding setting from UTF-8
to ISO-8859-1
这篇关于从ResourceStream提取压缩文件会引发错误“无效的存储块长度".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!