Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added LZ4 compression #23

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -14,6 +14,7 @@ Supported formats/extensions:
- .tar.gz & .tgz
- .tar.bz2 & .tbz2
- .tar.xz & .txz
- .tar.lz4 & .tlz4
- .rar (open only)


Expand Down
2 changes: 1 addition & 1 deletion archiver_test.go
Expand Up @@ -74,7 +74,7 @@ func symmetricTest(t *testing.T, name string, ar Archiver) {

if info.IsDir() {
// stat dir instead of read file
_, err := os.Stat(origPath)
_, err = os.Stat(origPath)
if err != nil {
t.Fatalf("%s: Couldn't stat original directory (%s): %v",
fpath, origPath, err)
Expand Down
75 changes: 75 additions & 0 deletions tarlz4.go
@@ -0,0 +1,75 @@
package archiver

import (
"archive/tar"
"fmt"
"os"
"strings"

"github.com/pierrec/lz4"
)

// TarLz4 is for TarLz4 format
var TarLz4 tarLz4Format

func init() {
RegisterFormat("TarLz4", TarLz4)
}

type tarLz4Format struct{}

func (tarLz4Format) Match(filename string) bool {
return strings.HasSuffix(strings.ToLower(filename), ".tar.lz4") || strings.HasSuffix(strings.ToLower(filename), ".tlz4") || isTarLz4(filename)
}

// isTarLz4 checks the file has the bzip2 compressed Tar format header by
// reading its beginning block.
func isTarLz4(tarlz4Path string) bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says bzip2, but the code just checks the tar format?

f, err := os.Open(tarlz4Path)
if err != nil {
return false
}
defer f.Close()

lz4r := lz4.NewReader(f)
buf := make([]byte, tarBlockSize)
n, err := lz4r.Read(buf)
if err != nil || n < tarBlockSize {
return false
}

return hasTarHeader(buf)
}

// Make creates a .tar.bz2 file at tarlz4Path containing
// the contents of files listed in filePaths. File paths
// can be those of regular files or directories. Regular
// files are stored at the 'root' of the archive, and
// directories are recursively added.
func (tarLz4Format) Make(tarlz4Path string, filePaths []string) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to update the godoc comments ;)

out, err := os.Create(tarlz4Path)
if err != nil {
return fmt.Errorf("error creating %s: %v", tarlz4Path, err)
}
defer out.Close()

lz4Writer := lz4.NewWriter(out)
defer lz4Writer.Close()

tarWriter := tar.NewWriter(lz4Writer)
defer tarWriter.Close()

return tarball(filePaths, tarWriter, tarlz4Path)
}

// Open untars source and decompresses the contents into destination.
func (tarLz4Format) Open(source, destination string) error {
f, err := os.Open(source)
if err != nil {
return fmt.Errorf("%s: failed to open archive: %v", source, err)
}
defer f.Close()

lz4r := lz4.NewReader(f)
return untar(tar.NewReader(lz4r), destination)
}