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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?