George
George

Reputation: 789

java: Zip file size in target folder is bigger than original size in resources, won't unzip

I have a zip file of 672 bytes in the main/resources of my java project.

However when the project builds, the same file in target/classes in now a zip file of 1.10 KB.

When my code tried to unzip it, I get the error:

java.util.zip.ZipException: invalid code lengths set
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:194)
at java.io.FilterInputStream.read(FilterInputStream.java:107)

I think that the problem comes from the fact that the file is bigger in the target problem, because I have had other projects where I used my unZip function without any problem. I have no idea what is making this and how to solve it? Also not sure what info you need to help me? I am using Spring and Maven, and the JDK 1.8.0_45

Upvotes: 2

Views: 1924

Answers (3)

Rajeev Rathor
Rajeev Rathor

Reputation: 1922

When i building the jar using command "mvn clean package or install". I encountered this issue. "invalid code lengths set" and compilation error I struggled with weird issue. I tried all the suggested solution in POM.xml.

But Finally I removed the .m2 directory which size was more than 3 GB.[Its tedious and weird solution]. But It is perfectly working for me.

Upvotes: 0

SubOptimal
SubOptimal

Reputation: 22963

The problem came from a combination of following settings in your pom.xml

<filtering>true</filtering>

this instructs maven to replace variables in the resources files (see: Maven Resources Filtering).

As you also instruct Maven to write the resources files encoded in UTF-8 (see: Maven Resources encoding).

<encoding>UTF-8</encoding>

the ZIP file (which is a binary file) will be writen in UTF-8 encoding, which garbles the content.

If you would have used LATIN-1 as encoding it would work

<encoding>ISO-8859-1</encoding>

Depending on your own answer. As it seems that you neither use the encoding as UTF-8 in the resources files nor the filtering of variables, removing both the resources plugin and the filtering instruction was the solution for you.

edit If for some reason you cannot use this encoding you can exclude files based on their extension from filtering.

<artifactId>maven-resources-plugin</artifactId>
...
<configuration>
    <encoding>UTF-8</encoding>
    <escapeString>\</escapeString>
    ...
    <nonFilteredFileExtensions>
        <nonFilteredFileExtension>zip</nonFilteredFileExtension>
    </nonFilteredFileExtensions>
</configuration>

Upvotes: 3

George
George

Reputation: 789

Deleting this piece of code in my pom.xml file did the trick:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                specify UTF-8, ISO-8859-1 or any other file encoding
                <encoding>UTF-8</encoding>
                <escapeString>\</escapeString>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

Now the zip files has the same size in the target folder and can be unzip.

Problem solved, but I don't really understand what happened there...!

Upvotes: 1

Related Questions