package archiver import "bytes" import "compress/gzip" import "fmt" import "github.com/foobaz/go-zopfli/zopfli" import "github.com/andybalholm/brotli" func Compress (_data []byte, _algorithm string) ([]byte, error) { switch _algorithm { case "gz", "gzip" : return CompressGzip (_data) case "zopfli" : return CompressZopfli (_data) case "br", "brotli" : return CompressBrotli (_data) case "", "none", "identity" : return _data, nil default : return nil, fmt.Errorf ("[ea23f966] invalid compression algorithm `%s`", _algorithm) } } func CompressEncoding (_algorithm string) (string, string, error) { switch _algorithm { case "gz", "gzip" : return "gzip", "gzip", nil case "zopfli" : return "zopfli", "gzip", nil case "br", "brotli" : return "brotli", "br", nil case "", "none", "identity" : return "identity", "identity", nil default : return "", "", fmt.Errorf ("[6026a403] invalid compression algorithm `%s`", _algorithm) } } func CompressGzip (_data []byte) ([]byte, error) { _buffer := & bytes.Buffer {} var _encoder *gzip.Writer if _encoder_0, _error := gzip.NewWriterLevel (_buffer, gzip.BestCompression); _error == nil { _encoder = _encoder_0 } else { return nil, _error } if _, _error := _encoder.Write (_data); _error != nil { return nil, _error } if _error := _encoder.Close (); _error != nil { return nil, _error } _data = _buffer.Bytes () return _data, nil } func CompressZopfli (_data []byte) ([]byte, error) { _buffer := & bytes.Buffer {} _options := zopfli.DefaultOptions () _options.NumIterations = 15 _options.BlockSplitting = true _options.BlockSplittingLast = true _options.BlockSplittingMax = 0 _options.BlockType = zopfli.DYNAMIC_BLOCK if _error := zopfli.GzipCompress (&_options, _data, _buffer); _error != nil { return nil, _error } _data = _buffer.Bytes () return _data, nil } func CompressBrotli (_data []byte) ([]byte, error) { _buffer := & bytes.Buffer {} _options := brotli.WriterOptions { Quality : 11, LGWin : 24} _encoder := brotli.NewWriterOptions (_buffer, _options) if _, _error := _encoder.Write (_data); _error != nil { return nil, _error } if _error := _encoder.Close (); _error != nil { return nil, _error } _data = _buffer.Bytes () return _data, nil }