Java 从 ResourceStream 中提取压缩文件会引发错误“存储的块长度无效”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25235229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting zipped file from ResourceStream throws error "Invalid stored block lengths"
提问by PaulBGD
I am trying to extract a ZIP file from my current JAR using:
我正在尝试使用以下方法从我当前的 JAR 中提取 ZIP 文件:
InputStream resource = getClass().getClassLoader().getResourceAsStream(name);
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>
):
这得到了正确的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;
}
However when I try to do this, I get this exception (on IOUtils.read
)
但是,当我尝试这样做时,我得到了这个异常(上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.
我这样做错了吗?我已经对错误进行了大量的谷歌搜索,但没有看到与我的问题相关的任何内容。
采纳答案by Gelin Luo
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):
感谢@PaulBGD 上面的回答。它节省了我几个小时来弄清楚我的系统发生了什么我将以下新片段添加到我的pom.xml
文件中(在阅读 Paul 的答案之前我没有意识到这是根本原因):
<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>
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:
但是我没有直接接受 Paul 的回答,相反我认为这些xlsx
文件不应该通过资源插件的过滤管道,因此我resource
在 pom 中添加了另一个:
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xlsx</include>
</includes>
</resource>
And it fixed my problem without changing the source encoding setting from UTF-8
to ISO-8859-1
它解决了我的问题,而无需将源编码设置从 更改UTF-8
为ISO-8859-1
回答by PaulBGD
After a few more hours of searching, I decompiled the maven-resources-plugin and noticed it used UTF-8 encoding by default. I quickly looked up the encoding I would need (ISO-8859-1) and put it in my pom.
经过几个小时的搜索,我反编译了 maven-resources-plugin 并注意到它默认使用 UTF-8 编码。我迅速查找了我需要的编码(ISO-8859-1)并将其放入我的 pom.xml 文件中。
<properties>
<project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>
</properties>
Now the zip file copies into the jar perfectly, no corruption at all.
现在 zip 文件完美地复制到 jar 中,完全没有损坏。
回答by Narendra Kekane
Change spring-boot-starter-parent to 2.0.4.RELEASE. It worked for me.
将 spring-boot-starter-parent 更改为 2.0.4.RELEASE。它对我有用。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath />
</parent>