Extracting zipped file from ResourceStream throws error “Invalid stored block lengths”

后端 未结 3 1054
醉酒成梦
醉酒成梦 2020-12-11 15:23

I am trying to extract a ZIP file from my current JAR using:

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

相关标签:
3条回答
  • 2020-12-11 16:10

    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>
    

    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>
    

    And it fixed my problem without changing the source encoding setting from UTF-8 to ISO-8859-1

    0 讨论(0)
  • 2020-12-11 16:11

    Change spring-boot-starter-parent to 2.0.4.RELEASE. It worked for me.

      <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.RELEASE</version>
            <relativePath />
        </parent>
    
    0 讨论(0)
  • 2020-12-11 16:20

    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.

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

    Now the zip file copies into the jar perfectly, no corruption at all.

    0 讨论(0)
提交回复
热议问题