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 单实例实现网站多域名请求

有时候写网站,为了统一的后端,把不同业务都集中到一个后端,这时就需要处理多域名的请求,在 Go http server 里实现很简单,只需把不同域名映射到不同的 `http.Handler`。...

Golang sync.WaitGroup 的 Wait 超时处理

sync.WaitGroup 使用 `Add(1)`、`Done()`、`Wait()`组合来实现多协程等待,如果某一协程未能合理处理错误,导致无法退出,此时需要引入超时机制。下面是一种超时处理方法。...

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

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