57 lines
916 B
Go
57 lines
916 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"io"
|
|
"log"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
"github.com/songgao/water/waterutil"
|
|
)
|
|
|
|
func Compress(writer io.Writer, data [][]byte) {
|
|
|
|
// encode gob
|
|
enc := gob.NewEncoder(writer)
|
|
|
|
log.Printf("send to target %s source bytes: %d", waterutil.IPv4Destination(data[0]), len(data[0]))
|
|
err := enc.Encode(data)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
// zstd compress
|
|
zenc, err := zstd.NewWriter(writer)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
err = zenc.Flush()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
}
|
|
|
|
func Decompress(data []byte) (frames [][]byte) {
|
|
var err error
|
|
|
|
log.Printf("get data bytes len: %d", len(data))
|
|
|
|
var buf = bytes.NewBuffer(data)
|
|
var zdec *zstd.Decoder
|
|
zdec, err = zstd.NewReader(buf)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
zdec.Close()
|
|
|
|
dec := gob.NewDecoder(buf)
|
|
err = dec.Decode(&frames)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
return
|
|
}
|