Skip to content

Commit

Permalink
Add support for zlib compression
Browse files Browse the repository at this point in the history
I needed it for something, so.
  • Loading branch information
mholt committed Jun 1, 2022
1 parent 8489bc1 commit 3869ede
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ Introducing **Archiver 4.0** - a cross-platform, multi-format archive utility an

### Supported compression formats

- brotli (br)
- bzip2 (bz2)
- flate (zip)
- gzip (gz)
- lz4
- snappy (sz)
- xz
- zstandard (zst)
- brotli (.br)
- bzip2 (.bz2)
- flate (.zip)
- gzip (.gz)
- lz4 (.lz4)
- snappy (.sz)
- xz (.xz)
- zlib (.zz)
- zstandard (.zst)

### Supported archive formats

Expand Down
52 changes: 52 additions & 0 deletions zlib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package archiver

import (
"bytes"
"io"
"strings"

"github.com/klauspost/compress/zlib"
)

func init() {
RegisterFormat(Zlib{})
}

// Zlib facilitates zlib compression.
type Zlib struct {
CompressionLevel int
}

func (Zlib) Name() string { return ".zz" }

func (zz Zlib) Match(filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult

// match filename
if strings.Contains(strings.ToLower(filename), zz.Name()) {
mr.ByName = true
}

// match file header
buf, err := readAtMost(stream, len(ZlibHeader))
if err != nil {
return mr, err
}
mr.ByStream = bytes.Equal(buf, ZlibHeader)

return mr, nil
}

func (zz Zlib) OpenWriter(w io.Writer) (io.WriteCloser, error) {
level := zz.CompressionLevel
if level == 0 {
level = zlib.DefaultCompression
}
return zlib.NewWriterLevel(w, level)
}

func (Zlib) OpenReader(r io.Reader) (io.ReadCloser, error) {
return zlib.NewReader(r)
}

var ZlibHeader = []byte{0x78}

0 comments on commit 3869ede

Please sign in to comment.