2019-06-14 14:00:46 -07:00
|
|
|
package press
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
2020-01-31 19:18:19 +01:00
|
|
|
|
|
|
|
"github.com/klauspost/compress/gzip"
|
2019-06-14 14:00:46 -07:00
|
|
|
)
|
|
|
|
|
2020-01-31 19:18:19 +01:00
|
|
|
// AlgGzip represents gzip compression algorithm
|
|
|
|
type AlgGzip struct {
|
|
|
|
level int
|
|
|
|
blockSize uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitializeGzip initializes the gzip compression Algorithm
|
|
|
|
func InitializeGzip(bs uint32, level int) Algorithm {
|
|
|
|
a := new(AlgGzip)
|
|
|
|
a.blockSize = bs
|
|
|
|
a.level = level
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFileExtension returns file extension
|
|
|
|
func (a *AlgGzip) GetFileExtension() string {
|
|
|
|
return ".gz"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeader returns the Lz4 compression header
|
|
|
|
func (a *AlgGzip) GetHeader() []byte {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFooter returns
|
|
|
|
func (a *AlgGzip) GetFooter() []byte {
|
|
|
|
return []byte{}
|
|
|
|
}
|
2019-06-14 14:00:46 -07:00
|
|
|
|
2020-01-31 19:18:19 +01:00
|
|
|
// CompressBlock that compresses a block using gzip
|
|
|
|
func (a *AlgGzip) CompressBlock(in []byte, out io.Writer) (compressedSize uint32, uncompressedSize uint64, err error) {
|
2019-06-14 14:00:46 -07:00
|
|
|
// Initialize buffer
|
2020-01-31 19:18:19 +01:00
|
|
|
bufw := bufio.NewWriterSize(out, int(a.blockSize+(a.blockSize)>>4))
|
2019-06-14 14:00:46 -07:00
|
|
|
|
|
|
|
// Initialize block writer
|
2020-01-31 19:18:19 +01:00
|
|
|
outw, err := gzip.NewWriterLevel(bufw, a.level)
|
2019-06-14 14:00:46 -07:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compress block
|
|
|
|
_, err = outw.Write(in)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize gzip file, flush buffer and return
|
|
|
|
err = outw.Close()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
blockSize := uint32(bufw.Buffered())
|
|
|
|
err = bufw.Flush()
|
|
|
|
|
2020-01-31 19:18:19 +01:00
|
|
|
return blockSize, uint64(len(in)), err
|
2019-06-14 14:00:46 -07:00
|
|
|
}
|
|
|
|
|
2020-01-31 19:18:19 +01:00
|
|
|
// DecompressBlock decompresses Lz4 compressed block
|
|
|
|
func (a *AlgGzip) DecompressBlock(in io.Reader, out io.Writer, BlockSize uint32) (n int, err error) {
|
2019-06-14 14:00:46 -07:00
|
|
|
gzipReader, err := gzip.NewReader(in)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
written, err := io.Copy(out, gzipReader)
|
|
|
|
return int(written), err
|
|
|
|
}
|