Skip to content

Commit

Permalink
Initial proof of concept version
Browse files Browse the repository at this point in the history
  • Loading branch information
astrolox committed Jan 5, 2024
0 parents commit dccf9cf
Show file tree
Hide file tree
Showing 12 changed files with 975 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

dist/
temp/
.idea/

41 changes: 41 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# See http://goreleaser.com
project_name: klzip
before:
hooks:
- go mod download
builds:
- id: klzip
binary: klzip
env:
- CGO_ENABLED=0
goos:
- linux
#- windows
- darwin
goarch:
- amd64
- arm64
nfpms:
- description: Better faster gzip
vendor: K42 Software
homepage: https://www.k42.io/
maintainer: Brian Wojtczak <[email protected]>
#epoch: 1
#release: 1
formats:
- deb
- rpm
checksum:
algorithm: sha256
signs:
- id: gpg
artifacts: all
signature: "${artifact}.gpg"
snapshot:
name_template: "{{ .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Copyright (c) 2012 The Go Authors. All rights reserved.
Copyright (c) 2019 Klaus Post. All rights reserved.
Copyright (c) 2024 Brian Wojtczak. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# klzip

This is a command line tool for compressing and decompressing files. It is
intended to be a drop-in replacement for the standard `gzip` command.

For my use cases on my hardware, it is faster and produces smaller files. Your
mileage may vary.

## Installation

Grab the latest release from the github releases page, or build from source.

This project is compatible with the standard golang build tools.

## Usage

Usage is identical to the standard `gzip` command. Run `klzip -h` for more
information.

Files compressed with `klzip` can be decompressed with `gzip` and vice versa.

## rsyncable support using xflate

The file produced by `rsyncable` flag is constructed differently to that from
the standard `gzip` command. You may find that `rsync` will copy the entire
file on the first sync after switching to this tool. Subsequent syncs will be
faster.

Using the `rsyncable` flag has the side effect that the archive becomes seekable
when using a reader that supports the `xflate` format. This provides the ability
to read just a portion of the archive without decompressing the entire file.

See the following for more information on the `xflate` format:
https://github.com/dsnet/compress/blob/master/doc/xflate-format.pdf

## License

This project is covered by a BSD-style license that can be found in the LICENSE file.
132 changes: 132 additions & 0 deletions compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// @author: Brian Wojtczak
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"bufio"
"github.com/dustin/go-humanize"
"github.com/google/renameio"
"github.com/klauspost/compress/gzip"
"github.com/pkg/errors"
"io"
"log"
"os"
"path/filepath"
"time"
)

// CompressFile takes a filename and compresses it to a new file of the
// same name with a .gz suffix added. If keep is false, the original file is
// deleted if compression is successful.
func CompressFile(filename, suffix string, level int, keep, force, named, verbose bool) (err error) {

var (
inputInfo os.FileInfo
outputInfo os.FileInfo
inputFile *os.File
outputFile *renameio.PendingFile
zw *gzip.Writer

outputFilename string
)

started := time.Now().UTC()

if verbose {
log.Printf(
"Compressing %s",
filename,
)
}

inputInfo, err = os.Stat(filename)
if err != nil {
return errors.Wrap(err, "error stating input file")
}

inputFile, err = os.Open(filename)
if err != nil {
return errors.Wrap(err, "error opening input file")
}
//goland:noinspection GoUnhandledErrorResult
defer inputFile.Close()

outputFilename = filename + suffix

if !force {
if _, err := os.Stat(outputFilename); err == nil {
return errors.New("output file already exists")
}
}

outputFile, err = renameio.TempFile(filepath.Dir(outputFilename), outputFilename)
if err != nil {
return errors.Wrap(err, "error creating temporary output file")
}
//goland:noinspection GoUnhandledErrorResult
defer outputFile.Cleanup()

outputBuffer := bufio.NewWriter(outputFile)

zw, err = gzip.NewWriterLevel(outputBuffer, level)
if err != nil {
return errors.Wrap(err, "error creating gzip writer")
}

if named {
zw.Name = filepath.Base(filename)
zw.ModTime = inputInfo.ModTime()
}

_, err = io.Copy(zw, bufio.NewReader(inputFile))
if err != nil {
return errors.Wrap(err, "error writing data")
}

if err := inputFile.Close(); err != nil {
return errors.Wrap(err, "error closing input file")
}

if err := zw.Flush(); err != nil {
return errors.Wrap(err, "error flushing data")
}

if err := zw.Close(); err != nil {
return errors.Wrap(err, "error closing output writer")
}

if err := outputBuffer.Flush(); err != nil {
return errors.Wrap(err, "error flushing output buffer")
}

if err := outputFile.CloseAtomicallyReplace(); err != nil {
return errors.Wrap(err, "error atomically closing output file")
}

outputInfo, err = os.Stat(outputFilename)
if err != nil {
return errors.Wrap(err, "error stating output file")
}

if !keep {
if err = os.Remove(filename); err != nil {
return errors.Wrap(err, "error removing input file")
}
}

if verbose {
duration := time.Since(started)
log.Printf(
"Compressed %s from %s to %s (level %d) in %v",
outputFilename,
humanize.Bytes(uint64(inputInfo.Size())),
humanize.Bytes(uint64(outputInfo.Size())),
level,
duration,
)
}

return nil
}
122 changes: 122 additions & 0 deletions decompress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// @author: Brian Wojtczak
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"bufio"
"github.com/dustin/go-humanize"
"github.com/google/renameio"
"github.com/klauspost/compress/gzip"
"github.com/pkg/errors"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
)

// DecompressFile takes a filename and decompresses it to a new file of the
// same name without the .gz suffix. If keep is false, the original file is
// deleted if decompression is successful.
func DecompressFile(filename, suffix string, keep, force, verbose bool) (err error) {

var (
inputInfo os.FileInfo
outputInfo os.FileInfo
inputFile *os.File
outputFile *renameio.PendingFile
zr *gzip.Reader

outputFilename string
)

started := time.Now().UTC()

if verbose {
log.Printf(
"Decompressing %s",
filename,
)
}

inputInfo, err = os.Stat(filename)
if err != nil {
return err
}

inputFile, err = os.Open(filename)
if err != nil {
return err
}
//goland:noinspection GoUnhandledErrorResult
defer inputFile.Close()

outputFilename = strings.TrimSuffix(filename, suffix)

if !force {
if _, err := os.Stat(outputFilename); err == nil {
return errors.New("output file already exists")
}
}

outputFile, err = renameio.TempFile(filepath.Dir(outputFilename), outputFilename)
if err != nil {
return err
}
//goland:noinspection GoUnhandledErrorResult
defer outputFile.Cleanup()

outputBuffer := bufio.NewWriter(outputFile)

zr, err = gzip.NewReader(bufio.NewReader(inputFile))
if err != nil {
return err
}

_, err = io.Copy(outputBuffer, zr)
if err != nil {
return err
}

if err := zr.Close(); err != nil {
return err
}

if err := inputFile.Close(); err != nil {
return err
}

if err := outputBuffer.Flush(); err != nil {
return errors.Wrap(err, "error flushing output buffer")
}

if err := outputFile.CloseAtomicallyReplace(); err != nil {
return err
}

outputInfo, err = os.Stat(outputFilename)
if err != nil {
return err
}

if !keep {
if err = os.Remove(filename); err != nil {
return err
}
}

if verbose {
log.Printf(
"Decompressed %s from %s to %s in %v",
outputFilename,
humanize.Bytes(uint64(inputInfo.Size())),
humanize.Bytes(uint64(outputInfo.Size())),
time.Since(started),
)
}

return nil
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module klzip

go 1.21

require (
github.com/davecgh/go-spew v1.1.1
github.com/devfacet/gocmd v3.1.0+incompatible
github.com/dsnet/compress v0.0.1
github.com/dustin/go-humanize v1.0.1
github.com/google/renameio v1.0.1
github.com/klauspost/compress v1.17.4
github.com/pkg/errors v0.9.1
)

require github.com/smartystreets/goconvey v1.8.1 // indirect
Loading

0 comments on commit dccf9cf

Please sign in to comment.