I have this piece of C#
code:
public static byte[] TestGzip(string text)
{
byte[] bytes = Encoding.UTF8.GetBytes(text);
MemoryStream memoryStream1 = new MemoryStream();
using (GZipStream gzipStream = new GZipStream(memoryStream1, CompressionMode.Compress, true))
gzipStream.Write(bytes, 0, bytes.Length);
memoryStream1.Position = 0L;
byte[] buffer = new byte[memoryStream1.Length];
memoryStream1.Read(buffer, 0, buffer.Length);
return buffer;
}
and I wanted to reproduce this code in JavaScript
so I tried pako and node.js zlib.
Here's how their output is slightly different than the GZipStream
and each other:
const zlib = require('zlib');
const pako = require('pako');
const cc = str => [...str].map(c => c.charCodeAt(0) & 255);
// C# (this is what I want)
Program.TestGZip("a") // [31, 139, 8, 0, 0, 0, 0, 0, 4, 0, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0]
// JS
pako.gzip("a") // [31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0] Uint8Array(21)
pako.gzip([97]) // same...
pako.gzip(new Uint8Array([97])) // same...
pako.gzip(cc("a")) // same...
zlib.gzipSync("a") // [31, 139, 8, 0, 0, 0, 0, 0, 0, 10, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0] Buffer(21)
zlib.gzipSync(new Uint8Array([97])) // same...
I also tried some different options of pako
and zlib
, and while with some options the result was different, it never matched the C#
result:
// different options
zlib.gzipSync("a", {level: 1}) // [31, 139, 8, 0, 0, 0, 0, 0, 4, 10, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0]
zlib.gzipSync("a", {level: 9}) // [31, 139, 8, 0, 0, 0, 0, 0, 2, 10, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0]
zlib.gzipSync("a", {strategy: 2|3}) // [31, 139, 8, 0, 0, 0, 0, 0, 4, 10, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0]
pako.gzip("a", {level: 1}) // [31, 139, 8, 0, 0, 0, 0, 0, 4, 3, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0]
pako.gzip("a", {level: 9}) // [31, 139, 8, 0, 0, 0, 0, 0, 2, 3, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0]
pako.gzip("a", {strategy: 2|3}) // [31, 139, 8, 0, 0, 0, 0, 0, 4, 3, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0]
So what should I do?
Why there are these slight differences?
How can I achieve the exact GZipStream.Write()
output?
fix (thanks to @Sebastian):
pako.gzip("a", {strategy: 2, header:{os: 0}})
pako.gzip("a", {strategy: 3, header:{os: 0}})
// weirdly enough, just passing an empty header object works as well:
pako.gzip("a", {strategy: 2, header:{}})
pako.gzip("a", {strategy: 3, header:{}})
// all outputs are exactly like GZipStream.Write():
// [31, 139, 8, 0, 0, 0, 0, 0, 4, 0, 75, 4, 0, 67, 190, 183, 232, 1, 0, 0, 0]