pax_global_header00006660000000000000000000000064134413136100014506gustar00rootroot0000000000000052 comment=f498085833dcb9961a12ad9a0202dd244ab62fc5 golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/000077500000000000000000000000001344131361000214775ustar00rootroot00000000000000golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/.travis.yml000066400000000000000000000003311344131361000236050ustar00rootroot00000000000000language: go go: - tip before_install: - go get golang.org/x/tools/cmd/cover script: - go test -coverprofile=coverage.txt -covermode=atomic after_success: - bash <(curl -s https://codecov.io/bash) golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/LICENSE000066400000000000000000000020721344131361000225050ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Alberto Cortés Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/README.md000066400000000000000000000016701344131361000227620ustar00rootroot00000000000000# tgz [![GoDoc](https://godoc.org/github.com/alcortesm/tgz?status.svg)](https://godoc.org/github.com/alcortesm/tgz) [![Build Status](https://travis-ci.org/alcortesm/tgz.svg)](https://travis-ci.org/alcortesm/tgz) [![codecov](https://codecov.io/gh/alcortesm/tgz/branch/master/graph/badge.svg)](https://codecov.io/gh/alcortesm/tgz) A Go library to extract tgz files to temporal directories. # Example The following program will decompress the file "/tmp/foo.tgz" to a temporal directory, print the names of all the files and directories in it and delete the temporal directory: ```go package main import ( "fmt" "io/ioutil" "os" "github.com/alcortesm/tgz" ) func main() { tmpPath, err := tgz.Extract("/tmp/foo.tgz") if tmpPath != "" { defer os.RemoveAll(tmpPath) } if err != nil { panic(err) } infos, err := ioutil.ReadDir(tmpPath) if err != nil { panic(err) } for _, info := range infos { fmt.Println(info.Name()) } } ``` golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/fixtures/000077500000000000000000000000001344131361000233505ustar00rootroot00000000000000golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/fixtures/invalid-gzip.tgz000066400000000000000000000000121344131361000264640ustar00rootroot00000000000000golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/fixtures/not-a-tar.tgz000066400000000000000000000000451344131361000256770ustar00rootroot00000000000000aQWnot-a-tar.tarc`vh golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/fixtures/test-01.tgz000066400000000000000000000002221344131361000252670ustar00rootroot00000000000000CMW= 0 a'pLC@_ghRC, d8iGz= KyJǿ&om^v~6?);-voo~?j?D-b^(golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/fixtures/test-02.tgz000066400000000000000000000002171344131361000252740ustar00rootroot00000000000000[QW= 0 @a'(b籡ԅL))o j˚f>-'ob=zJ]܇Aӱ9)SϽ}}Su*oF¡gK(golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/fixtures/test-03.tgz000066400000000000000000000004101344131361000252700ustar00rootroot00000000000000mQWQj0tgy OPz&KiC c[pFUVmGJ)pQbä|ܷy0u[~Oo}nnhMojD,a"'@[QDje/{Vgf!߀i_gwF]/IM_}KF)(golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/tgz.go000066400000000000000000000042671344131361000226430ustar00rootroot00000000000000package tgz import ( "archive/tar" "compress/gzip" "fmt" "io" "io/ioutil" "os" ) const ( useDefaultTempDir = "" tmpPrefix = "tmp-tgz-" ) // Extract decompress a gziped tarball into a new temporal directory // created just for this purpose. // // On success, the path of the newly created directory and a nil error // is returned. // // A non-nil error is returned if the method fails to complete. The // returned path will be an empty string if no information was extracted // before the error and the temporal directory has not been created. // Otherwise, a non-empty string with the temporal directory holding // whatever information was extracted before the error is returned. func Extract(tgz string) (d string, err error) { f, err := os.Open(tgz) if err != nil { return "", err } defer func() { errClose := f.Close() if err == nil { err = errClose } }() d, err = ioutil.TempDir(useDefaultTempDir, tmpPrefix) if err != nil { return "", err } tar, err := zipTarReader(f) if err != nil { return d, err } if err = unTar(tar, d); err != nil { return d, err } return d, nil } func zipTarReader(r io.Reader) (*tar.Reader, error) { zip, err := gzip.NewReader(r) if err != nil { return nil, err } return tar.NewReader(zip), nil } func unTar(src *tar.Reader, dstPath string) error { for { header, err := src.Next() if err != nil { if err == io.EOF { break } return err } dst := dstPath + "/" + header.Name mode := os.FileMode(header.Mode) switch header.Typeflag { case tar.TypeDir: err := os.MkdirAll(dst, mode) if err != nil { return err } case tar.TypeReg: err := makeFile(dst, mode, src) if err != nil { return err } default: return fmt.Errorf("Unable to untar type : %c in file %s", header.Typeflag, header.Name) } } return nil } func makeFile(path string, mode os.FileMode, contents io.Reader) (err error) { w, err := os.Create(path) if err != nil { return err } defer func() { errClose := w.Close() if err == nil { err = errClose } }() _, err = io.Copy(w, contents) if err != nil { return err } if err = os.Chmod(path, mode); err != nil { return err } return nil } golang-github-alcortesm-tgz-0.0~git20161220.9c5fe88/tgz_test.go000066400000000000000000000055031344131361000236740ustar00rootroot00000000000000package tgz import ( "fmt" "os" "path/filepath" "reflect" "regexp" "sort" "testing" ) func TestExtractError(t *testing.T) { for i, test := range [...]struct { tgz string errRgx *regexp.Regexp }{ { tgz: "not-found", errRgx: regexp.MustCompile("open not-found: no such file .*"), }, { tgz: "fixtures/invalid-gzip.tgz", errRgx: regexp.MustCompile("gzip: invalid header"), }, { tgz: "fixtures/not-a-tar.tgz", errRgx: regexp.MustCompile("unexpected EOF"), }, } { com := fmt.Sprintf("%d) tgz path = %s", i, test.tgz) path, err := Extract(test.tgz) if err == nil { t.Errorf("%s: expect an error, but none was returned", com) } else if errorNotMatch(err, test.errRgx) { t.Errorf("%s:\n\treceived error: %s\n\texpected regexp: %s\n", com, err, test.errRgx) } if path != "" { if err = os.RemoveAll(path); err != nil { t.Fatalf("%s: cannot remove temp directory: %s", com, err) } } } } func errorNotMatch(err error, regexp *regexp.Regexp) bool { return !regexp.MatchString(err.Error()) } func TestExtract(t *testing.T) { for i, test := range [...]struct { tgz string tree []string }{ { tgz: "fixtures/test-01.tgz", tree: []string{ "foo.txt", }, }, { tgz: "fixtures/test-02.tgz", tree: []string{ "baz.txt", "bla.txt", "foo.txt", }, }, { tgz: "fixtures/test-03.tgz", tree: []string{ "bar", "bar/baz.txt", "bar/foo.txt", "baz", "baz/bar", "baz/bar/foo.txt", "baz/baz", "baz/baz/baz", "baz/baz/baz/foo.txt", "foo.txt", }, }, } { com := fmt.Sprintf("%d) tgz path = %s", i, test.tgz) path, err := Extract(test.tgz) if err != nil { t.Fatalf("%s: unexpected error extracting: %s", err) } obt, err := relativeTree(path) if err != nil { t.Errorf("%s: unexpected error calculating relative path: %s", com, err) } sort.Strings(test.tree) if !reflect.DeepEqual(obt, test.tree) { t.Fatalf("%s:\n\tobtained: %v\n\t expected: %v", com, obt, test.tree) } err = os.RemoveAll(path) if err != nil { t.Fatalf("%s: unexpected error removing temporal path: %s", com, err) } } } // relativeTree returns the list of relative paths to the files and // directories inside a given directory, recursively. func relativeTree(dir string) ([]string, error) { dir = filepath.Clean(dir) absPaths := []string{} walkFn := func(path string, _ os.FileInfo, _ error) error { absPaths = append(absPaths, path) return nil } _ = filepath.Walk(dir, walkFn) return toRelative(absPaths[1:], dir) } // toRelative returns the relative paths (form b) of the list of paths in l. func toRelative(l []string, b string) ([]string, error) { r := []string{} for _, p := range l { rel, err := filepath.Rel(b, p) if err != nil { return nil, err } r = append(r, rel) } return r, nil }