Skip to content

Latest commit

 

History

History
113 lines (89 loc) · 3.35 KB

README.md

File metadata and controls

113 lines (89 loc) · 3.35 KB

TGZ Package

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.

Installation

Add the package to your Go module:

go get github.com/woozymasta/tgz

Functions:

  • Pack(sourceDir string, targetArchive string) error
    Creates a tar.gz archive from the contents of the specified sourceDir and saves it to targetArchive. Uses default compression.
  • PackWithLevel(sourceDir string, targetArchive string, level int) error
    Creates a tar.gz archive from sourceDir and saves it to targetArchive, with a specified gzip compression level (0-9).
  • PackWithPrefix(sourceDir, targetArchive, prefix string, level int) error
    Creates a tar.gz archive from sourceDir and saves it to targetArchive, with prefix added to the archive file paths. Allows gzip compression level (0-9).
  • Unpack(sourceArchive, targetDir string) error
    Extracts a tar.gz archive sourceArchive into the specified targetDir.

Examples

Creating archive

err := tgz.Pack("source/directory", "archive.tar.gz")
if err != nil {
  log.Fatal(err)
}

Creating archive with custom compression

// 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)
}

More about compression

Creating archive with a path prefix

err := tgz.PackWithPrefix("source/directory", "archive.tar.gz", "some/prefix", -1)
if err != nil {
  log.Fatal(err)
}

Extracting archive

err := tgz.Unpack("archive.tar.gz", "output/directory")
if err != nil {
  log.Fatal(err)
}

Testing

go test ./...

Other archive packages

  • ZIPp Package - simple way to create and extract .zip archives