diff --git a/cmd/htpacker/packer/packer.go b/cmd/htpacker/packer/packer.go index 6dba182..a6b0a1b 100644 --- a/cmd/htpacker/packer/packer.go +++ b/cmd/htpacker/packer/packer.go @@ -64,6 +64,11 @@ func (ignoreProgress) Begin(_, _ string) {} func (ignoreProgress) End(_, _ string) {} const ( + // minCompressionFileSize is the minimum filesize we need before + // considering compression. Note this must be at least 2, to avoid + // known bugs in go-zopfli. + minCompressionFileSize = 128 + // minCompressionSaving means we'll only use the compressed version of // the file if it's at least this many bytes smaller than the original. // Chosen somewhat arbitrarily; we have to add an HTTP header, and the @@ -433,6 +438,10 @@ func (p *packer) Uncompressed(srcPath string, dir *packed.File) error { // Gzip will gzip input data to a temporary file, and then append that to the // output file. func (p *packer) Gzip(data []byte, dir *packed.File) error { + if len(data) < minCompressionFileSize { + return nil + } + // write via temporary file tmpfile, err := ioutil.TempFile("", "") if err != nil { @@ -474,6 +483,9 @@ func (p *packer) Gzip(data []byte, dir *packed.File) error { // Brotli will compress input data to a temporary file, and then append that to // the output file. func (p *packer) Brotli(data []byte, dir *packed.File) error { + if len(data) < minCompressionFileSize { + return nil + } // write via temporary file tmpfile, err := ioutil.TempFile("", "") if err != nil {