seaweedfs/weed-fs/src/pkg/storage/compress.go

50 lines
974 B
Go
Raw Normal View History

package storage
import (
"bytes"
"compress/flate"
"compress/gzip"
"io/ioutil"
"strings"
)
2012-10-23 17:59:40 +00:00
func IsGzippable(ext, mtype string) bool {
if ext == ".zip" {
2012-10-23 17:59:40 +00:00
return false
}
if ext == ".rar" {
2012-10-23 17:59:40 +00:00
return false
}
if ext == ".gz" {
return false
}
if strings.Index(mtype,"text/")==0 {
return true
}
if strings.Index(mtype,"application/")==0 {
return true
}
return false
}
func GzipData(input []byte) []byte {
buf := new(bytes.Buffer)
w, _ := gzip.NewWriterLevel(buf, flate.BestCompression)
if _, err := w.Write(input); err!=nil {
2012-08-24 08:15:27 +00:00
println("error compressing data:", err)
}
if err := w.Close(); err!=nil {
2012-08-24 08:15:27 +00:00
println("error closing compressed data:", err)
}
return buf.Bytes()
}
func UnGzipData(input []byte) []byte {
buf := bytes.NewBuffer(input)
r, _ := gzip.NewReader(buf)
defer r.Close()
output, err := ioutil.ReadAll(r)
if err!=nil {
2012-08-24 08:15:27 +00:00
println("error uncompressing data:", err)
}
return output
}