preface

The comporess package in the Go standard library also provides a series of algorithms for file compression and decompression. This article will focus on the common gZIP type compression and decompression.

Header

type Header struct {
    Comment string    / / comment
    Extra   []byte    // Additional data
    ModTime time.Time // Change the time
    Name    string    / / file name
    OS      byte      // Operating system type
}
Copy the code

Each gzip file has a Header type structure that holds information about the file. Both Reader and Writer contain Header fields, which can be used directly by the caller through the dot syntax.

Decompression/Reader

type Reader struct {
	Header       // valid after NewReader or Reader.Reset
	r            flate.Reader
	decompressor io.ReadCloser
	digest       uint32 // CRC-32, IEEE polynomial (section 8)
	size         uint32 // Uncompressed size (section 2.3.1)
	buf          [512]byte
	err          error
	multistream  bool
}

func NewReader(r io.Reader) (*Reader, error)

func (z *Reader) Reset(r io.Reader) error

func (z *Reader) Read(p []byte) (n int, err error)

func (z *Reader) Close(a) error
Copy the code
  • useNewReader()To create and return a slaveio.ReaderType parameter read with decompressed input streamReaderA pointer to an object of the.
  • useResetMethod to reset a Reader object that has been created and set the next read target toio.ReaderType parameterr, so as to achieve the purpose of reuse. Improve efficiency by avoiding memory allocation when multiple files need to be unpacked.
  • Used after the call endsClose()Methods the recyclingReaderObject.

Compression/Writer

type Writer struct {
	Header      // written at first call to Write, Flush, or Close
	w           io.Writer
	level       int
	wroteHeader bool
	compressor  *flate.Writer
	digest      uint32 // CRC-32, IEEE polynomial (section 8)
	size        uint32 // Uncompressed size (section 2.3.1)
	closed      bool
	buf         [10]byte
	err         error
}

func NewWriter(w io.Writer) *Writer

func NewWriterLevel(w io.Writer, level int) (*Writer, error)

func (z *Writer) Reset(w io.Writer)

func (z *Writer) Write(p []byte) (int, error)

func (z *Writer) Flush(a) error

func (z *Writer) Close(a) error
Copy the code
  • NewWrite()Method creates and returns oneWriterType, using the default compression levelDefaultCompression, all input streams that need to be compressed need to pass this pointer.
  • NewWriterLevel()Method can specify the compression level, and the level parameter can be DefaultCompression, NoCompression, or BestSpeed and BestCompression.
  • Write()Method may not immediately write compressed input to a file, if necessaryFlush()To talk about brushing data in the buffer into the file.

Example

func compress(file string, gzipWriter gzip.Writer) {
	readFile, err := os.Open(file)
	iferr ! =nil {
		log.Fatal(err)
	}
	reader := bufio.NewReader(readFile)
	for {
		s, e := reader.ReadString('\n')
		if e == io.EOF {
			break
		}
		_, err := gzipWriter.Write([]byte(s))
		iferr ! =nil {
			log.Fatal(err)
		}
	}
}

func deCompress(src, dst string) {
	gzipFile, err := os.Open(src)
	iferr ! =nil {
		log.Fatal(err)
	}
	gzipReader, err := gzip.NewReader(gzipFile)
	if err == io.EOF {
		return
	}
	defer gzipReader.Close()

	outfileWriter, err := os.Create(dst)
	iferr ! =nil {
		log.Fatal(err)
	}
	defer outfileWriter.Close()
	
	_, err = io.Copy(outfileWriter, gzipReader)
	iferr ! =nil {
		log.Fatal(err)
	}
}
Copy the code