Go Jlibを呼び出して圧縮と解凍を実現する。

1304 ワード

http://studygolang.com/articles/2522
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
")) fmt.Println(zip) fmt.Println(string(DoZlibUnCompress(zip))) }