Documentation ¶
Overview ¶
Package compress contains some useful tools to compress/decompress data or files
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFileToZip ¶
AddFileToZip add file tp zip.Writer
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).
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 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
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.