archive

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2020 License: MIT Imports: 12 Imported by: 3

README

goulash/archive

GoDoc

Package archive provides functions for working with archives.

For more information, see the documentation! :-)

This package is licensed under the MIT license.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var EOA error = errors.New("end of archive")

EOA signifies that the archive has been exhausted.

View Source
var ErrUnknownCodec = errors.New("unknown compression codec")

Functions

func DirReader

func DirReader(tr *tar.Reader, dirHeader **tar.Header) io.Reader

DirReader returns a specialized reader that reads all the files in a directory in a tar archive as if they were one file.

In this sense, it is very similar to the reader returned by io.MultiReader.

The reader advances the header of tar forward as long as it reads from files in a given directory; this is modified on the callers side, hence the need to pass a pointer to a pointer of tar.Header. When Read then returns io.EOF, the current header is on the next valid file. tr.Next() must not be called after reading from DirReader.

DirReader differentiates between the end of directory and end of archive by returning io.EOF in the first and EOA in the latter case.

BUG: at the moment it chokes if there is another directory in the directory given.

Example
package main

import (
	"archive/tar"
	"bufio"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/goulash/archive"
)

func main() {
	file, err := os.Open("testdata/dir_reader_data.tar")
	if err != nil {
		die(err)
	}
	defer file.Close()

	tr := tar.NewReader(file)
	hdr, err := tr.Next()
	for hdr != nil {
		fi := hdr.FileInfo()
		if !fi.IsDir() {
			fmt.Fprintf(os.Stderr, "error: unexpected file '%s'\n", hdr.Name)

			hdr, err = tr.Next()
			if err != nil {
				if err == io.EOF {
					break
				}
				die(err)
			}

			continue
		}

		fmt.Println(hdr.Name)
		r := archive.DirReader(tr, &hdr)
		err = printPrefixed(r, "\t")
		if err != nil {
			if err == archive.EOA {
				break
			}
			die(err)
		}
	}

}

func printPrefixed(r io.Reader, prefix string) error {
	br := bufio.NewReader(r)
	for {
		line, err := br.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}

		fmt.Printf("%s%s", prefix, line)
	}
	return nil
}

func die(err error) {
	log.Fatalf("error: %s\n", err)
}
Output:

dir1/
	dir1/file2 content
	dir1/file1 content
dir2/
	dir2/file3
	this sentence should span three files.
	dir2/file2
	and which will appear in betwwen the three files,
	dir2/file1
	Apart from the header which is written in each file,

func ExtractArchive added in v1.2.0

func ExtractArchive(archive, destdir string) error

ExtractArchive extracts an archive on disk to the provided destination directory.

func ExtractTar added in v1.2.0

func ExtractTar(r io.Reader, destdir string) error

ExtractTar extracts all files from the reader into the provided destination directory.

func ReadFileFromArchive

func ReadFileFromArchive(archive, file string) ([]byte, error)

ReadFileFromArchive tries to read the file specified from the (compressed) archive. Archive formats supported are:

.tar
.tar.gz
.tar.bz2
.tar.xz
.tar.zst

func ReadFileFromTar

func ReadFileFromTar(r io.Reader, file string) ([]byte, error)

ReadFileFromTar tries to read the file specified from an opened tar file. This function is used together with ReadFileFromArchive, hence the io.Reader.

Types

type Decompressor

type Decompressor struct {
	// contains filtered or unexported fields
}

Decompressor is a universal decompressor that, given a filepath, chooses the appropriate decompression algorithm.

At the moment, only the gzip, bzip2, and lzma (as in ".xz") are supported. The decompressor needs to be closed after usage.

func NewDecompressor

func NewDecompressor(fpath string) (*Decompressor, error)

NewDecompressor creates a new decompressor based on the file extension of the given file. If no known file extension is encountered, each of the supported compression codecs is tried until a positive match is found.

The returned Decompressor can be Read and Closed, even if the underlying compressor doesn't support the Close interface.

func (*Decompressor) Close

func (d *Decompressor) Close() error

func (*Decompressor) Read

func (d *Decompressor) Read(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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