我有这段C#
代码:
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;
}
我想重现这段代码,JavaScript
所以我尝试了pako和node.js zlib。以下是它们的输出与彼此
略有不同的方式:GZipStream
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...
我还尝试了 and 的一些不同选项pako
,zlib
虽然使用某些选项结果不同,但它从未匹配C#
结果:
// 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]
所以我该怎么做?
为什么会有这些细微的差别?
我怎样才能达到准确的GZipStream.Write()
输出?
修复(感谢@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]