nodeca / pako

high speed zlib port to javascript, works in browser & node.js

Home Page:http://nodeca.github.io/pako/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GZIP pako compressed string results in "Not in GZIP format" in JAVA on decompressing

anbaran opened this issue · comments

Hello

I am trying to pack a string with gzip in Angular and send it to server (JAVA), decompress and process.

However, If I compress with pako gzip then I cannot decompress it in JAVA due to

java.util.zip.ZipException: Not in GZIP format at java.base/java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:166)

In JAVA the line where it fails is (package java.util.zip, JAVA 11)

 private int readHeader(InputStream this_in) throws IOException {
        CheckedInputStream in = new CheckedInputStream(this_in, this.crc);
        this.crc.reset();
        if (this.readUShort(in) != 35615) {
            throw new ZipException("Not in GZIP format");
        } else if (this.readUByte(in) != 8) {
            throw new ZipException("Unsupported compression method");

Also decompressing JAVA gziped string in pako results in error: Error incorrect header check

I noticed that the output of gzip from pako is different than the output of compressing with gzip in java.

I managed to reproduce it, here is the working example in Stackblitz (Angular)

https://stackblitz.com/edit/stackblitz-starters-7ypnzq?file=src%2Fmain.ts

Can you tell me what is wrong with pako.gzip or Am I doing something wrong?

I use pako v2.1.0 and JAVA 11

I figured it out.. It is because of JAVA using signed bytes (-127, 128) whereas pako javascript using unsinged bytes (1 to 256), in case someone is also wondering why.