Retrieving an encrypted file from ResourceStream causes an "Invalid saved block length" error

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

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

Get the correct InputStream , but it gives an error when I try to unzip it using the following code (I store each file in 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 it wrong? I made a lot of mistakes in the error, and I did not see anything related to my problem.

+7
java maven zip embedded-resource
source share
2 answers

Thanks @PaulBGD answer above. It saved me time to figure out what happened to my system. I am adding the following new snippets to my pom.xml file (which I did not understand is the main reason 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 immediately answer Paul's answer, instead I don’t think that these xlsx files should go through the resource plugin filtering pipeline, so I added another resource to pom:

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

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

+5
source share

After hours of searching, I decompiled the maven-resources plugin and noticed that it uses UTF-8 encoding by default. I quickly searched for the required encoding (ISO-8859-1) and put it in my pom.

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

Now the zip file is completely copied to the jar, without any damage.

+6
source share

All Articles