gzip
Node.js
const zlib = require('zlib')
const data = Buffer.from('hello world', 'utf-8')
zlib.gzip(data, (err, compressed) => {
if (err) {
console.error(err)
}
console.log(compressed)
zlib.unzip(compressed, (err, decompressed) => {
if (err) {
console.error(err)
}
console.log(decompressed.toString())
})
})
Output
<Buffer 1f 8b 08 00 00 00 00 00 00 13 cb 48 cd c9 c9 57 28 cf 2f ca 49 01 00 85 11 4a 0d 0b 00 00 00>
hello world
Go
package main
import (
"bytes"
"compress/gzip"
"fmt"
)
func main() {
data := []byte("hello world")
compressed := new(bytes.Buffer)
w := gzip.NewWriter(compressed)
if _, err := w.Write(data); err != nil {
panic(err)
}
if err := w.Close(); err != nil {
panic(err)
}
fmt.Println(compressed.Bytes())
decompressed := new(bytes.Buffer)
r, err := gzip.NewReader(compressed)
if err != nil {
panic(err)
}
_, err = decompressed.ReadFrom(r)
if err != nil {
panic(err)
}
fmt.Println(string(decompressed.Bytes()))
}
Output
[31 139 8 0 0 0 0 0 0 255 202 72 205 201 201 87 40 207 47 202 73 1 4 0 0 255 255 133 17 74 13 11 0 0 0]
hello world
当前内容版权归 miguelmota 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 miguelmota .