targz

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 11 Imported by: 1

README

Targz

Library for packaging/extracting folders in tar.gz archives.

Documentation on godoc.org

Installation

Installing using go get is the easiest.

go get github.com/walle/targz

Usage

The API is really simple, there are only two methods.

  • Compress
  • Extract
Create an archive containing a folder
import "github.com/walle/targz"
...
err := targz.Compress("my_folder", "my_file.tar.gz")
Extract an archive
import "github.com/walle/targz"
...
err := targz.Extract("my_file.tar.gz", "path/to/extract/to")

Contributing

All contributions are welcome! See CONTRIBUTING for more info.

License

Licensed under MIT license. See LICENSE for more information.

Documentation

Overview

Package targz contains methods to create and extract tar gz archives.

Usage (discarding potential errors):

targz.Compress("path/to/the/directory/to/compress", "my_archive.tar.gz")
targz.Extract("my_archive.tar.gz", "directory/to/extract/to")

This creates an archive in ./my_archive.tar.gz with the folder "compress" (last in the path). And extracts the folder "compress" to "directory/to/extract/to/". The folder structure is created if it doesn't exist.

Example
package main

import (
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/walle/targz"
)

func main() {
	// Create a temporary file structure we can use
	tmpDir, dirToCompress := createExampleData()

	// Compress a folder to my_archive.tar.gz
	err := targz.Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err != nil {
		panic(err)
	}

	// Extract my_archive.tar.gz to a new folder called extracted
	err = targz.Extract(filepath.Join(tmpDir, "my_archive.tar.gz"), filepath.Join(tmpDir, "extracted"))
	if err != nil {
		panic(err)
	}
}

func createExampleData() (string, string) {
	tmpDir, err := ioutil.TempDir("", "targz-example")
	if err != nil {
		panic(err)
	}

	directory := filepath.Join(tmpDir, "my_folder")
	subDirectory := filepath.Join(directory, "my_sub_folder")
	err = os.MkdirAll(subDirectory, 0755)
	if err != nil {
		panic(err)
	}

	_, err = os.Create(filepath.Join(subDirectory, "my_file.txt"))
	if err != nil {
		panic(err)
	}

	return tmpDir, directory
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compress

func Compress(inputFilePath, outputFilePath string) (err error)

Compress creates an archive from the folder inputFilePath points to in the file outputFilePath points to. Only adds the last directory in inputFilePath to the archive, not the whole path. It tries to create the directory structure outputFilePath contains if it doesn't exist. It returns potential errors to be checked or nil if everything works.

func Extract

func Extract(inputFilePath, outputFilePath string) (err error)

Extract extracts an archive from the file inputFilePath points to in the directory outputFilePath points to. It tries to create the directory structure outputFilePath contains if it doesn't exist. It returns potential errors to be checked or nil if everything works.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL