The tgz
package provides a simple way to create and extract tar.gz
archives in Go.
import github.com/woozymasta/tgz
func main() {
tgz.Pack("source/directory", "archive.tar.gz")
tgz.Unpack("archive.tar.gz", "output/directory")
}
- Support for Relative Paths: Archives can include files and directories using relative paths, ensuring compatibility with various directory structures and simplifying file organization.
- Customizable Directory Prefix: Add a prefix to all files and directories in the archive, allowing more flexible control over extracted folder structures and better organization within archives.
- Adjustable Compression Level: Set a custom gzip compression level from 0 (no compression) to 9 (maximum compression) to balance between archive size and compression speed as needed.
- Cross-Platform Compatibility: Seamlessly supports both Windows and Unix-based systems, automatically handling path separators and ensuring consistent performance across environments.
- Efficient Directory Walking: Recursively walks through all files and directories in the source directory, efficiently gathering content for archiving while excluding redundant or hidden paths.
- Selective File Type Handling: Supports archiving regular files and directories while skipping unsupported file types, ensuring compatibility with various filesystem configurations.
- Simplified Extraction Process: Extracts all files and directories with path integrity, handling any missing directories and preserving original file permissions where applicable.
Add the package to your Go module:
go get github.com/woozymasta/tgz
Functions:
Pack(sourceDir string, targetArchive string) error
Creates atar.gz
archive from the contents of the specifiedsourceDir
and saves it to targetArchive. Uses default compression.PackWithLevel(sourceDir string, targetArchive string, level int) error
Creates atar.gz
archive fromsourceDir
and saves it totargetArchive
, with a specified gzip compressionlevel
(0-9).PackWithPrefix(sourceDir, targetArchive, prefix string, level int) error
Creates atar.gz
archive fromsourceDir
and saves it totargetArchive
, withprefix
added to the archive file paths. Allows gzip compressionlevel
(0-9).Unpack(sourceArchive, targetDir string) error
Extracts atar.gz
archivesourceArchive
into the specifiedtargetDir
.
err := tgz.Pack("source/directory", "archive.tar.gz")
if err != nil {
log.Fatal(err)
}
// 9 is best compression
// 1 is best speed
// -1 default compression
err := tgz.PackWithLevel("source/directory", "archive.tar.gz", 9)
if err != nil {
log.Fatal(err)
}
err := tgz.PackWithPrefix("source/directory", "archive.tar.gz", "some/prefix", -1)
if err != nil {
log.Fatal(err)
}
err := tgz.Unpack("archive.tar.gz", "output/directory")
if err != nil {
log.Fatal(err)
}
go test ./...
- ZIPp Package - simple way to create and extract .zip archives