Package compress provides convenience functions for creating compressors and uncompressors based on filenames.
func NewReader(r io.Reader) (io.ReadCloser, bool)
NewReader creates an uncompressing reader by reading the first few bytes of the input and finding a magic header for either gzip, zstd, bzip2. If the magic header is found , it returns an uncompressing ReadCloser and true. Else, it returns ioutil.NopCloser(r) and false.
CAUTION: this function will misbehave when the input is a binary string that happens to have the same magic gzip, zstd, or bzip2 header. Thus, you should use this function only when the input is expected to be ASCII.
func NewReaderPath ¶
NewReaderPath creates a reader that uncompresses data read from the given reader. The compression format is determined by the pathname extensions. If the pathname ends with one of the following extensions, it creates an uncompressing ReadCloser and returns true.
.gz => gzip format .zst => zstd format .bz2 => bz2 format
For other extensions, this function returns an ioutil.NopCloser(r) and false.
The caller must close the ReadCloser after use. For some file formats, Close() is the only place that reports file corruption.
func NewWriterPath ¶
NewWriterPath creates a WriteCloser that compresses data. The compression format is determined by the pathname extensions. If the pathname ends with one of the following extensions, it creates an compressing WriteCloser and returns true.
.gz => gzip format .zst => zstd format
For other extensions, this function creates a noop WriteCloser and returns false. The caller must close the WriteCloser after use.