dw做的静态网站怎么分享链接,短视频app成品搭建源码免费,家在深圳论坛,如何一键建淘宝客网站有时我们可能会遇到需要把多个 gzip 文件合并成单个 gzip 文件的场景#xff0c;最简单最容易的方式是把每个gzip文件都先解压#xff0c;然后合并成一个文件后再次进行压缩#xff0c;最终得到我们想要的结果#xff0c;但这种先解压后压缩的方式显然效率不高#xff0c;…有时我们可能会遇到需要把多个 gzip 文件合并成单个 gzip 文件的场景最简单最容易的方式是把每个gzip文件都先解压然后合并成一个文件后再次进行压缩最终得到我们想要的结果但这种先解压后压缩的方式显然效率不高有没有更好的实现方式呢答案是肯定的Linux下常用的压缩库 Zlib 的两位主要作者之一 Mark Adler 就给我们提供了一个这样的示例程序
https://github.com/madler/zlib/blob/develop/examples/gzjoin.c 从说明中我们可以看出这种方式只需要解压一遍所有文件用于找到特定的比特位并修改它但不需要做任何额外的压缩操作而且合并后的 gzip 文件末尾的 crc32 校验和也不需要从头计算根据源 gzip 文件的校验和用函数 crc32_combine 便可计算出一般来说deflate 解压操作要比压缩操作速度快很多所以这种合并 gzip 文件的方式在性能上是相当高效的如果是在 C 语言中实现我们就可以直接“借鉴” Mark Adler 大佬的代码。
针对同一个数据源用 Go 内置 compress/gzip 包压缩和解压缩简单的性能对比
goos: darwin
goarch: arm64
pkg: go-redis-demo
BenchmarkGzipgoredis_test.go:60: test input data length: 778785
BenchmarkGzip/compress
BenchmarkGzip/compress-8 115 9727792 ns/op
BenchmarkGzip/decompress
BenchmarkGzip/decompress-8 609 1957044 ns/op使用默认压缩级别解压速度是压缩速度的5倍使用 Linux 下的命令行压缩工具 gzip 对比结果也差不多
命令行工具 gzip 压缩速度
$ ll test.html
-rw-r--r-- 1 zy staff 778785 Jul 30 14:28 test.html
$
$ time gzip -kf test.htmlreal 0m0.034s
user 0m0.024s
sys 0m0.007s
$ time gzip -kf test.htmlreal 0m0.032s
user 0m0.025s
sys 0m0.004s
$ time gzip -kf test.htmlreal 0m0.030s
user 0m0.025s
sys 0m0.004s命令行工具 gzip 解压缩速度
$ ll
total 1808
-rw-r--r-- 1 zy staff 778785 Jul 30 14:28 test.html
-rw-r--r-- 1 zy staff 142127 Jul 30 14:28 test.html.gz
$
$ time gzip -dkf test.html.gzreal 0m0.013s
user 0m0.003s
sys 0m0.005s
$ time gzip -dkf test.html.gzreal 0m0.008s
user 0m0.003s
sys 0m0.004s
$ time gzip -dkf test.html.gzreal 0m0.010s
user 0m0.003s
sys 0m0.004s解压也是比压缩快 3-4 倍左右这就意味着在合并 gzip 文件时省去压缩操作会对性能产生极大提升。
Go 语言实现
用 Go 内置的 compress/gzip 或者 compress/flate 包无法实现与 gzjoin.c 相同的功能因为 gzjoin.c 的实现依赖解压时的 Z_BLOCK 刷写模式而 compress/flate 解压缩时并不支持指定 Flush 模式所以我们只能换一种思路利用 cgo 来直接调用 Zlib C 库具体实现可以参考我这里的代码 https://github.com/zhyee/deflatejoin如果对实现细节不感兴趣也可以在你的Go项目中直接调用该module 与用 解压 -- 合并文件 -- 再压缩 的方式性能对比差不多提升了10倍以上
goos: darwin
goarch: arm64
pkg: github.com/zhyee/deflatejoin
BenchmarkConcatGzip/concat-standard-go-8 9 123559972 ns/op 1257826 B/op 1261 allocs/op
BenchmarkConcatGzip/concat-deflatejoin-8 100 10784015 ns/op 30289 B/op 41 allocs/op