-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
460 additions
and
28 deletions.
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
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
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,51 @@ | ||
package zstd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
|
||
"github.com/meltwater/drone-cache/archive/tar" | ||
"github.com/meltwater/drone-cache/internal" | ||
|
||
"github.com/go-kit/kit/log" | ||
) | ||
|
||
// Archive implements archive for zstd. | ||
type Archive struct { | ||
logger log.Logger | ||
|
||
root string | ||
compressionLevel int | ||
skipSymlinks bool | ||
} | ||
|
||
// New creates an archive that uses the .tar.zst file format. | ||
func New(logger log.Logger, root string, skipSymlinks bool, compressionLevel int) *Archive { | ||
return &Archive{logger, root, compressionLevel, skipSymlinks} | ||
} | ||
|
||
// Create writes content of the given source to an archive, returns written bytes. | ||
func (a *Archive) Create(srcs []string, w io.Writer) (int64, error) { | ||
zw, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(a.compressionLevel))) | ||
if err != nil { | ||
return 0, fmt.Errorf("create archive writer, %w", err) | ||
} | ||
|
||
defer internal.CloseWithErrLogf(a.logger, zw, "zstd writer") | ||
|
||
return tar.New(a.logger, a.root, a.skipSymlinks).Create(srcs, zw) | ||
} | ||
|
||
// Extract reads content from the given archive reader and restores it to the destination, returns written bytes. | ||
func (a *Archive) Extract(dst string, r io.Reader) (int64, error) { | ||
zr, err := zstd.NewReader(r) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
defer internal.CloseWithErrLogf(a.logger, zr.IOReadCloser(), "zstd reader") | ||
|
||
return tar.New(a.logger, a.root, a.skipSymlinks).Extract(dst, zr) | ||
} |
Oops, something went wrong.