Documentation ¶
Overview ¶
Package compress contains some useful tools to compress/decompress data or files
Index ¶
- func AddFileToZip(zipWriter *zip.Writer, filename, basedir string) error
- func GzCompress(in io.Reader, out io.Writer) error
- func GzDecompress(in io.Reader, out io.Writer, opts ...GzDecompressOption) error
- func Unzip(src string, dest string, opts ...UnzipOption) (filenames []string, err error)
- func ZipFiles(output string, files []string) (err error)
- type Compressor
- type GzDecompressOption
- type Gzip
- type Option
- type PGZip
- type UnzipOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFileToZip ¶
AddFileToZip add file tp zip.Writer
func GzCompress ¶ added in v4.9.0
GzCompress compress data by gzip
func GzDecompress ¶ added in v4.9.0
GzDecompress decompress data by gzip
be careful about the decompression bomb, see https://en.wikipedia.org/wiki/Zip_bomb, default maxBytes is 1GB, it's better to set this value to avoid decompression bomb.
func Unzip ¶
func Unzip(src string, dest string, opts ...UnzipOption) (filenames []string, err error)
Unzip will decompress a zip archive, moving all files and folders within the zip file (parameter 1) to an output directory (parameter 2).
inspired by https://golangcode.com/unzip-files-in-go/
Args:
- src: is the source zip file.
- dest: is the destination directory.
- (opt) UnzipWithMaxBytes: decompressed bytes will not exceed this limit, default/0 is unlimit. it's better to set this value to avoid decompression bomb.
- (opt) UnzipWithCopyChunkBytes: copy chunk by chunk from src to dst
Returns:
- filenames: all filenames in zip file
Types ¶
type Compressor ¶
type Compressor interface { Write([]byte) (int, error) WriteString(string) (int, error) // write footer and flust to lower writer Flush() error WriteFooter() error }
Compressor interface of compressor
type GzDecompressOption ¶ added in v4.9.0
type GzDecompressOption func(*gzDecompressOption) error
GzDecompressOption optional arguments for GzDecompress
func WithGzDecompressMaxBytes ¶ added in v4.9.0
func WithGzDecompressMaxBytes(bytes int64) GzDecompressOption
WithGzDecompressMaxBytes decompressed bytes will not exceed this limit,
default is 1GB, it's better to set this value to avoid decompression bomb. set 0 to unlimit.
type Gzip ¶
type Gzip struct {
// contains filtered or unexported fields
}
GZCompressor compress by gz with buf
func NewGZip ¶
NewGZip create new GZCompressor
Example ¶
originText := testCompressraw writer := &bytes.Buffer{} var err error // writer c, err := NewGZip( writer, WithLevel(defaultGzipLevel), // default WithBufSizeByte(defaultBufSizeByte), // default ) if err != nil { log.Shared.Error("new compressor", zap.Error(err)) return } if _, err = c.WriteString(originText); err != nil { log.Shared.Error("write string to compressor", zap.Error(err)) return } if err = c.Flush(); err != nil { log.Shared.Error("flush compressor", zap.Error(err)) return } // reader var gz *gzip.Reader if gz, err = gzip.NewReader(writer); err != nil { log.Shared.Error("new compressor", zap.Error(err)) return } var bs []byte if bs, err = io.ReadAll(gz); err != nil { log.Shared.Error("read from compressor", zap.Error(err)) return } got := string(bs) if got != originText { log.Shared.Error("extract compressed text invalidate", zap.String("got", got), zap.ByteString("expect", bs)) return }
Output:
type Option ¶
type Option func(*option) error
CompressOptFunc options for compressor
func WithPGzipBlockSize ¶
WithPGzipBlockSize set compressor blocks
func WithPGzipNBlocks ¶
WithPGzipNBlocks set compressor blocks
type PGZip ¶
type PGZip struct {
// contains filtered or unexported fields
}
PGZip parallel gzip compressor
call `NewPGZip` to create new PGZip
Example ¶
originText := testCompressraw writer := &bytes.Buffer{} var err error // writer c, err := NewPGZip(writer) if err != nil { log.Shared.Error("new compressor", zap.Error(err)) return } if _, err = c.WriteString(originText); err != nil { log.Shared.Error("write string to compressor", zap.Error(err)) return } if err = c.Flush(); err != nil { log.Shared.Error("flush compressor", zap.Error(err)) return } // reader var gz *gzip.Reader if gz, err = gzip.NewReader(writer); err != nil { log.Shared.Error("new compressor", zap.Error(err)) return } var bs []byte if bs, err = io.ReadAll(gz); err != nil { log.Shared.Error("read from compressor", zap.Error(err)) return } got := string(bs) if got != originText { log.Shared.Error("extract compressed text invalidate", zap.String("got", got), zap.ByteString("expect", bs)) return }
Output:
type UnzipOption ¶
type UnzipOption func(*unzipOption) error
UnzipOption optional arguments for UnZip
func UnzipWithCopyChunkBytes ¶
func UnzipWithCopyChunkBytes(bytes int64) UnzipOption
UnzipWithCopyChunkBytes copy chunk by chunk from src to dst
func UnzipWithMaxBytes ¶
func UnzipWithMaxBytes(bytes int64) UnzipOption
UnzipWithMaxBytes decompressed bytes will not exceed this limit, default/0 is unlimit.