【问题标题】:Extracting zipped file from ResourceStream throws error "Invalid stored block lengths"从 ResourceStream 中提取压缩文件会引发错误“无效的存储块长度”
【发布时间】:2014-10-03 19:21:16
【问题描述】:

我正在尝试使用以下方法从我当前的 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)

我做错了吗?我已经对错误进行了大量的谷歌搜索,但没有看到任何与我的问题相关的内容。

【问题讨论】:

  • 所以您的 JAR 包含一个压缩的条目?您想读取此条目的字节并通过ZipInputStream 读取它?
  • 是的,我想就是这样
  • 这表明文件已损坏。尝试用普通的解压工具解压。
  • 不管怎样,您在将数据连接到 StringBuilder 并将其保存为 String 时犯了一个严重错误。首先,您甚至没有正确执行此操作,因为您在构造每个字符串时忽略了读取计数。其次,String 不是二进制数据的容器。您应该将每个条目复制到 ByteArrayOutputStream 并返回 HashMap
  • 等一下。运行 maven 和构建 .zip 文件并将其添加到 JAR 之间存在很大的无法解释的距离,并且都不涉及编译。

标签: java maven zip embedded-resource


【解决方案1】:

将 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>

【讨论】:

    【解决方案2】:

    感谢@PaulBGD 上面的回答。它节省了我几个小时来弄清楚我的系统发生了什么我将以下新的 sn-ps 添加到我的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>
    

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

      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <includes>
          <include>**/*.xlsx</include>
        </includes>
      </resource>
    

    它解决了我的问题,而无需将源编码设置从 UTF-8 更改为 ISO-8859-1

    【讨论】:

    • 这似乎是更好的答案,将其标记为 :)
    【解决方案3】:

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

    <properties>
        <project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>
    </properties>
    

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

    【讨论】:

    • 非常感谢,为我节省了几个小时来了解发生了什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2017-02-26
    • 2020-01-15
    • 1970-01-01
    相关资源
    最近更新 更多