Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/woozymasta/tgz
A cross-platform Go library for creating and extracting .tar.gz archives with custom compression levels, path prefixes, and relative path support.
https://github.com/woozymasta/tgz
archive go golang gzip tar targz tgz
Last synced: 10 days ago
JSON representation
A cross-platform Go library for creating and extracting .tar.gz archives with custom compression levels, path prefixes, and relative path support.
- Host: GitHub
- URL: https://github.com/woozymasta/tgz
- Owner: WoozyMasta
- License: bsd-3-clause
- Created: 2024-10-30T11:54:00.000Z (14 days ago)
- Default Branch: master
- Last Pushed: 2024-10-30T12:15:16.000Z (14 days ago)
- Last Synced: 2024-11-01T08:54:55.637Z (12 days ago)
- Topics: archive, go, golang, gzip, tar, targz, tgz
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TGZ Package
The `tgz` package provides a simple way to create and extract `tar.gz`
archives in Go.```go
import github.com/woozymasta/tgzfunc 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:
```bash
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
```go
err := tgz.Pack("source/directory", "archive.tar.gz")
if err != nil {
log.Fatal(err)
}
```### Creating archive with custom compression
```go
// 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](https://pkg.go.dev/compress/flate#pkg-constants)
### Creating archive with a path prefix
```go
err := tgz.PackWithPrefix("source/directory", "archive.tar.gz", "some/prefix", -1)
if err != nil {
log.Fatal(err)
}
```### Extracting archive
```go
err := tgz.Unpack("archive.tar.gz", "output/directory")
if err != nil {
log.Fatal(err)
}
```## Testing
```bash
go test ./...
```