Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

java - Extracting zipped file from ResourceStream throws error "Invalid stored block lengths"

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

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>):

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)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...