Add snappy compression

This commit is contained in:
Milan Nikolic 2017-12-01 16:22:06 +01:00
parent 4564e18b32
commit 2914dc5cc8
3 changed files with 27 additions and 2 deletions

View file

@ -39,7 +39,7 @@ func init() {
func main() {
base := flag.String("base", "data", "Resources file basename")
comp := flag.Int("comp", rres.CompDeflate, "Compression type, 0=NONE, 1=DEFLATE, 2=LZ4, 5=LZMA2 (XZ), 6=BZIP2")
comp := flag.Int("comp", rres.CompDeflate, "Compression type, 0=NONE, 1=DEFLATE, 2=LZ4, 5=LZMA2 (XZ), 6=BZIP2, 7=Snappy")
enc := flag.Int("enc", rres.CryptoNone, "Encryption type, 0=NONE, 1=XOR, 2=AES, 3=3DES, 4=Blowfish, 5=XTEA")
key := flag.String("key", "", "Encryption key")
header := flag.Bool("header", false, "Generate C header (.h file)")
@ -286,7 +286,7 @@ func validComp(compType int) bool {
return true
case rres.CompLZ4, rres.CompLZMA2:
return true
case rres.CompBZIP2:
case rres.CompBZIP2, rres.CompSnappy:
return true
}
return false

View file

@ -15,6 +15,7 @@ import (
"golang.org/x/crypto/xtea"
"github.com/dsnet/compress/bzip2"
"github.com/golang/snappy"
"github.com/klauspost/compress/flate"
"github.com/pierrec/lz4"
xor "github.com/rootlch/encrypt"
@ -171,6 +172,19 @@ func Compress(data []byte, compType int) ([]byte, error) {
w.Close()
return buf.Bytes(), nil
case rres.CompSnappy:
buf := new(bytes.Buffer)
w := snappy.NewWriter(buf)
_, err := w.Write(data)
if err != nil {
return nil, err
}
w.Close()
return buf.Bytes(), nil
default:
return data, nil
@ -225,6 +239,15 @@ func Decompress(data []byte, compType int) ([]byte, error) {
return nil, err
}
return u, nil
case rres.CompSnappy:
r := snappy.NewReader(bytes.NewReader(data))
u, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return u, nil
default:
return data, nil

View file

@ -82,6 +82,8 @@ const (
CompLZMA2
// BZIP2 compression
CompBZIP2
// Snappy compression
CompSnappy
)
// Encryption types