Skip to content
/ tgz Public

A cross-platform Go library for creating and extracting .tar.gz archives with custom compression levels, path prefixes, and relative path support.

License

Notifications You must be signed in to change notification settings

WoozyMasta/tgz

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A cross-platform Go library for creating and extracting .tar.gz archives with custom compression levels, path prefixes, and relative path support.

Topics

Resources

License

Stars

Watchers

Forks

Languages