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