golang zlib 压缩,解压缩


package main

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

//进行zlib压缩
func DoZlibCompress(src []byte) []byte {
    var in bytes.Buffer
    w := zlib.NewWriter(&in)
    w.Write(src)
    w.Close()
    return in.Bytes()
}

//进行zlib解压缩
func DoZlibUnCompress(compressSrc []byte) []byte {
    b := bytes.NewReader(compressSrc)
    var out bytes.Buffer
    r, _ := zlib.NewReader(b)
    io.Copy(&out, r)
    return out.Bytes()
}

func main() {
    buff := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207,
        47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
    b := bytes.NewReader(buff)
    r, err := zlib.NewReader(b)
    if err != nil {
        panic(err)
    }
    io.Copy(os.Stdout, r)
    r.Close()

    zip := DoZlibCompress([]byte("hello, world\n"))
    fmt.Println(zip)
    fmt.Println(string(DoZlibUnCompress(zip)))
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM