GolangNote

Golang笔记

golang flate/zlib 解压缩

Permalink

golang flate/zlib 解压缩

Go: flate/zlib 解压缩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import (
	"bytes"
	"compress/flate"
	"compress/zlib"
	"fmt"
	"io/ioutil"
)

func main() {
	content := []byte{120, 156, 181, 80, 65, 78, 195, 48, 16, 252, 10, 242, 57, 69, 118, 226, 166, 38, 247, 156, 64, 42, 42, 130, 107, 100, 156, 165, 88, 196, 118, 149, 93, 35, 160, 234, 223, 89, 183, 61, 112, 42, 226, 192, 109, 118, 118, 102, 103, 180, 123, 65, 62, 0, 146, 13, 59, 209, 237, 5, 189, 15, 8, 78, 116, 74, 215, 70, 27, 211, 174, 100, 85, 184, 124, 34, 111, 86, 82, 171, 67, 37, 144, 102, 31, 183, 195, 15, 167, 168, 165, 90, 46, 164, 94, 72, 115, 165, 100, 87, 235, 174, 145, 215, 39, 189, 168, 68, 72, 209, 83, 154, 7, 22, 83, 70, 86, 67, 180, 207, 19, 140, 188, 114, 41, 4, 27, 71, 44, 225, 155, 254, 169, 223, 60, 244, 195, 221, 122, 125, 251, 120, 95, 24, 103, 221, 43, 20, 144, 50, 161, 31, 143, 16, 179, 115, 128, 8, 108, 225, 114, 47, 214, 79, 121, 62, 15, 232, 191, 224, 8, 74, 51, 6, 92, 213, 71, 130, 57, 218, 233, 175, 78, 182, 142, 30, 223, 254, 35, 91, 53, 77, 219, 94, 118, 47, 165, 50, 210, 148, 18, 148, 232, 124, 128, 31, 104, 183, 151, 91, 176, 126, 55, 167, 143, 207, 95, 3, 15, 229, 180, 155, 60, 68, 42, 159, 231, 241, 27, 47, 165, 167, 25}
	fmt.Println(string(content))

	enflated, err := ioutil.ReadAll(flate.NewReader(bytes.NewReader(content[2:])))
	if err != nil {
		panic(err)
	}
	fmt.Println(string(enflated))
	
	
	// zlib
	r, err := zlib.NewReader(bytes.NewReader(content))
	if err != nil {
		panic(err)
	}
	enflated, err = ioutil.ReadAll(r)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(enflated))
}

文件压缩:

Go: 文件压缩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import (
	"compress/flate"
	"io"
	"os"
)

func decompress(inputFile, outputFile string) {
	i, _ := os.Open(inputFile)
	defer i.Close()
	f := flate.NewReader(i)
	defer f.Close()
	o, _ := os.Create(outputFile)
	defer o.Close()
	io.Copy(o, f)
}

func compress(inputFile, outputFile string) {
	i, _ := os.Open(inputFile)
	defer i.Close()
	o, _ := os.Create(outputFile)
	defer o.Close()
	f, _ := flate.NewWriter(o, flate.BestCompression)
	defer f.Close()
	io.Copy(f, i)
}

func main() {
	compress("compress.go", "compress.min")
	decompress("compress.min", "compress_1.go")
}

用zlib 压缩、解压字符串

Go: zlib 压缩、解压字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
	"bytes"
	"compress/zlib"
	"fmt"
	"io"
)

func main() {
	var in bytes.Buffer
	b := []byte(`hello golang`)
	w := zlib.NewWriter(&in)
	w.Write(b)
	w.Close()

	var out bytes.Buffer
	r, _ := zlib.NewReader(&in)
	io.Copy(&out, r)
	fmt.Println(out.String())
}

本文网址: https://golangnote.com/topic/167.html 转摘请注明来源

Related articles

Golang http IPv4/IPv6 服务

Golang 的网络服务,如果不指定IPv4 或 IPv6,如果VPS 同时支持 IPv4 和 IPv6,`net.Listen()` 只会监听 IPv6 地址。但这不影响客户端使用 IPv4 地址来访问。...

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

Write a Comment to "golang flate/zlib 解压缩"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 1ms