从ResourceStream中提取压缩文件会引发错误“无效的存储块长度”

我试图从我当前的JAR中提取ZIP文件:

InputStream resource = getClass().getClassLoader().getResourceAsStream(name); 

这会得到正确的InputStream ,但是当我尝试使用以下代码解压缩它时会出错(我将每个文件存储到Hashmap ):

 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) 

我做错了吗? 我已经做了很多关于错误的谷歌搜索,我没有看到与我的问题有关的任何内容。

感谢@ PaulBGD的上述答案。 它节省了我几个小时来弄清楚我的系统发生了什么我将以下新片段添加到我的pom.xml文件中(在阅读Paul的答案之前我没有意识到这是根本原因):

   src/main/resources true  **/*.version **/*.properties **/*.xml **/*.csv **/*.txt **/*.gif **/*.json **/*.xlsx rythm/**    

但是我没有直接接受Paul的回答,相反我不认为那些xlsx文件应该通过资源插件的过滤管道,因此我在pom中添加了另一个resource

   src/main/resources false  **/*.xlsx   

它修复了我的问题,而没有将源编码设置从UTF-8更改为ISO-8859-1

经过几个小时的搜索,我反编译了maven-resources-plugin并注意到它默认使用了UTF-8编码。 我很快查找了我需要的编码(ISO-8859-1)并将其放入我的pom中。

  ISO-8859-1  

现在,zip文件完美地复制到jar中,完全没有损坏。

将spring-boot-starter-parent更改为2.0.4.RELEASE。 它对我有用。

   org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE