vacation

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Overview

Package vacation provides a set of functions that enable input stream decompression logic from several popular decompression formats. This allows from decompression from either a file or any other byte stream, which is useful for decompressing files that are being downloaded.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Archive added in v0.2.8

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

An Archive decompresses tar, gzip and xz compressed tar, and zip files from an input stream.

Example
package main

import (
	"archive/tar"
	"archive/zip"
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	tarBuffer := bytes.NewBuffer(nil)
	tw := tar.NewWriter(tarBuffer)

	tarFiles := []ArchiveFile{
		{Name: "some-tar-dir/"},
		{Name: "some-tar-dir/some-tar-file", Content: []byte("some-tar-dir/some-tar-file")},
		{Name: "tar-file", Content: []byte("tar-file")},
	}

	for _, file := range tarFiles {
		err := tw.WriteHeader(&tar.Header{Name: file.Name, Mode: 0755, Size: int64(len(file.Content))})
		if err != nil {
			log.Fatal(err)
		}

		_, err = tw.Write(file.Content)
		if err != nil {
			log.Fatal(err)
		}
	}

	tw.Close()

	zipBuffer := bytes.NewBuffer(nil)
	zw := zip.NewWriter(zipBuffer)

	zipFiles := []ArchiveFile{
		{Name: "some-zip-dir/"},
		{Name: "some-zip-dir/some-zip-file", Content: []byte("some-zip-dir/some-zip-file")},
		{Name: "zip-file", Content: []byte("zip-file")},
	}

	for _, file := range zipFiles {
		header := &zip.FileHeader{Name: file.Name}
		header.SetMode(0755)

		f, err := zw.CreateHeader(header)
		if err != nil {
			log.Fatal(err)
		}

		if _, err := f.Write(file.Content); err != nil {
			log.Fatal(err)
		}
	}

	zw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewArchive(bytes.NewReader(tarBuffer.Bytes()))
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	archive = vacation.NewArchive(bytes.NewReader(zipBuffer.Bytes()))
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

some-tar-dir/some-tar-file
some-zip-dir/some-zip-file
tar-file
zip-file

func NewArchive added in v0.2.8

func NewArchive(inputReader io.Reader) Archive

NewArchive returns a new Archive that reads from inputReader.

func (Archive) Decompress added in v0.2.8

func (a Archive) Decompress(destination string) error

Decompress reads from Archive, determines the archive type of the input stream, and writes files into the destination specified.

Archive decompression will also handle files that are types "text/plain; charset=utf-8" and write the contents of the input stream to a file name "artifact" in the destination directory.

func (Archive) StripComponents added in v0.2.8

func (a Archive) StripComponents(components int) Archive

StripComponents behaves like the --strip-components flag on tar command removing the first n levels from the final decompression destination. Setting this is a no-op for archive types that do not use --strip-components (such as zip).

Example
package main

import (
	"archive/tar"
	"archive/zip"
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	tarBuffer := bytes.NewBuffer(nil)
	tw := tar.NewWriter(tarBuffer)

	tarFiles := []ArchiveFile{
		{Name: "some-tar-dir/"},
		{Name: "some-tar-dir/some-tar-file", Content: []byte("some-tar-dir/some-tar-file")},
		{Name: "tar-file", Content: []byte("tar-file")},
	}

	for _, file := range tarFiles {
		err := tw.WriteHeader(&tar.Header{Name: file.Name, Mode: 0755, Size: int64(len(file.Content))})
		if err != nil {
			log.Fatal(err)
		}

		_, err = tw.Write(file.Content)
		if err != nil {
			log.Fatal(err)
		}
	}

	tw.Close()

	zipBuffer := bytes.NewBuffer(nil)
	zw := zip.NewWriter(zipBuffer)

	zipFiles := []ArchiveFile{
		{Name: "some-zip-dir/"},
		{Name: "some-zip-dir/some-zip-file", Content: []byte("some-zip-dir/some-zip-file")},
		{Name: "zip-file", Content: []byte("zip-file")},
	}

	for _, file := range zipFiles {
		header := &zip.FileHeader{Name: file.Name}
		header.SetMode(0755)

		f, err := zw.CreateHeader(header)
		if err != nil {
			log.Fatal(err)
		}

		if _, err := f.Write(file.Content); err != nil {
			log.Fatal(err)
		}
	}

	zw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewArchive(bytes.NewReader(tarBuffer.Bytes())).StripComponents(1)
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	archive = vacation.NewArchive(bytes.NewReader(zipBuffer.Bytes())).StripComponents(1)
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

some-tar-file
some-zip-dir/some-zip-file
zip-file

type TarArchive

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

A TarArchive decompresses tar files from an input stream.

Example
package main

import (
	"archive/tar"
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	buffer := bytes.NewBuffer(nil)
	tw := tar.NewWriter(buffer)

	files := []ArchiveFile{
		{Name: "some-dir/"},
		{Name: "some-dir/some-other-dir/"},
		{Name: "some-dir/some-other-dir/some-file", Content: []byte("some-dir/some-other-dir/some-file")},
		{Name: "first", Content: []byte("first")},
		{Name: "second", Content: []byte("second")},
		{Name: "third", Content: []byte("third")},
	}

	for _, file := range files {
		err := tw.WriteHeader(&tar.Header{Name: file.Name, Mode: 0755, Size: int64(len(file.Content))})
		if err != nil {
			log.Fatal(err)
		}

		_, err = tw.Write(file.Content)
		if err != nil {
			log.Fatal(err)
		}
	}

	tw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

first
second
some-dir/some-other-dir/some-file
third

func NewTarArchive

func NewTarArchive(inputReader io.Reader) TarArchive

NewTarArchive returns a new TarArchive that reads from inputReader.

func (TarArchive) Decompress

func (ta TarArchive) Decompress(destination string) error

Decompress reads from TarArchive and writes files into the destination specified.

func (TarArchive) StripComponents

func (ta TarArchive) StripComponents(components int) TarArchive

StripComponents behaves like the --strip-components flag on tar command removing the first n levels from the final decompression destination.

Example
package main

import (
	"archive/tar"
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	buffer := bytes.NewBuffer(nil)
	tw := tar.NewWriter(buffer)

	files := []ArchiveFile{
		{Name: "some-dir/"},
		{Name: "some-dir/some-other-dir/"},
		{Name: "some-dir/some-other-dir/some-file", Content: []byte("some-dir/some-other-dir/some-file")},
		{Name: "first", Content: []byte("first")},
		{Name: "second", Content: []byte("second")},
		{Name: "third", Content: []byte("third")},
	}

	for _, file := range files {
		err := tw.WriteHeader(&tar.Header{Name: file.Name, Mode: 0755, Size: int64(len(file.Content))})
		if err != nil {
			log.Fatal(err)
		}

		_, err = tw.Write(file.Content)
		if err != nil {
			log.Fatal(err)
		}
	}

	tw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewTarArchive(bytes.NewReader(buffer.Bytes())).StripComponents(1)
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

some-other-dir/some-file

type TarGzipArchive

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

A TarGzipArchive decompresses gziped tar files from an input stream.

Example
package main

import (
	"archive/tar"
	"bytes"
	"compress/gzip"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	buffer := bytes.NewBuffer(nil)
	gw := gzip.NewWriter(buffer)
	tw := tar.NewWriter(gw)

	files := []ArchiveFile{
		{Name: "some-dir/"},
		{Name: "some-dir/some-other-dir/"},
		{Name: "some-dir/some-other-dir/some-file", Content: []byte("some-dir/some-other-dir/some-file")},
		{Name: "first", Content: []byte("first")},
		{Name: "second", Content: []byte("second")},
		{Name: "third", Content: []byte("third")},
	}

	for _, file := range files {
		err := tw.WriteHeader(&tar.Header{Name: file.Name, Mode: 0755, Size: int64(len(file.Content))})
		if err != nil {
			log.Fatal(err)
		}

		_, err = tw.Write(file.Content)
		if err != nil {
			log.Fatal(err)
		}
	}

	tw.Close()
	gw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewTarGzipArchive(bytes.NewReader(buffer.Bytes()))
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

first
second
some-dir/some-other-dir/some-file
third

func NewTarGzipArchive

func NewTarGzipArchive(inputReader io.Reader) TarGzipArchive

NewTarGzipArchive returns a new TarGzipArchive that reads from inputReader.

func (TarGzipArchive) Decompress

func (gz TarGzipArchive) Decompress(destination string) error

Decompress reads from TarGzipArchive and writes files into the destination specified.

func (TarGzipArchive) StripComponents

func (gz TarGzipArchive) StripComponents(components int) TarGzipArchive

StripComponents behaves like the --strip-components flag on tar command removing the first n levels from the final decompression destination.

Example
package main

import (
	"archive/tar"
	"bytes"
	"compress/gzip"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	buffer := bytes.NewBuffer(nil)
	gw := gzip.NewWriter(buffer)
	tw := tar.NewWriter(gw)

	files := []ArchiveFile{
		{Name: "some-dir/"},
		{Name: "some-dir/some-other-dir/"},
		{Name: "some-dir/some-other-dir/some-file", Content: []byte("some-dir/some-other-dir/some-file")},
		{Name: "first", Content: []byte("first")},
		{Name: "second", Content: []byte("second")},
		{Name: "third", Content: []byte("third")},
	}

	for _, file := range files {
		err := tw.WriteHeader(&tar.Header{Name: file.Name, Mode: 0755, Size: int64(len(file.Content))})
		if err != nil {
			log.Fatal(err)
		}

		_, err = tw.Write(file.Content)
		if err != nil {
			log.Fatal(err)
		}
	}

	tw.Close()
	gw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewTarGzipArchive(bytes.NewReader(buffer.Bytes())).StripComponents(1)
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

some-other-dir/some-file

type TarXZArchive

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

A TarXZArchive decompresses xz tar files from an input stream.

Example
package main

import (
	"archive/tar"
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
	"github.com/ulikunitz/xz"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	buffer := bytes.NewBuffer(nil)
	xw, err := xz.NewWriter(buffer)
	if err != nil {
		log.Fatal(err)
	}

	tw := tar.NewWriter(xw)

	files := []ArchiveFile{
		{Name: "some-dir/"},
		{Name: "some-dir/some-other-dir/"},
		{Name: "some-dir/some-other-dir/some-file", Content: []byte("some-dir/some-other-dir/some-file")},
		{Name: "first", Content: []byte("first")},
		{Name: "second", Content: []byte("second")},
		{Name: "third", Content: []byte("third")},
	}

	for _, file := range files {
		err := tw.WriteHeader(&tar.Header{Name: file.Name, Mode: 0755, Size: int64(len(file.Content))})
		if err != nil {
			log.Fatal(err)
		}

		_, err = tw.Write(file.Content)
		if err != nil {
			log.Fatal(err)
		}
	}

	tw.Close()
	xw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewTarXZArchive(bytes.NewReader(buffer.Bytes()))
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

first
second
some-dir/some-other-dir/some-file
third

func NewTarXZArchive

func NewTarXZArchive(inputReader io.Reader) TarXZArchive

NewTarXZArchive returns a new TarXZArchive that reads from inputReader.

func (TarXZArchive) Decompress

func (txz TarXZArchive) Decompress(destination string) error

Decompress reads from TarXZArchive and writes files into the destination specified.

func (TarXZArchive) StripComponents

func (txz TarXZArchive) StripComponents(components int) TarXZArchive

StripComponents behaves like the --strip-components flag on tar command removing the first n levels from the final decompression destination.

Example
package main

import (
	"archive/tar"
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
	"github.com/ulikunitz/xz"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	buffer := bytes.NewBuffer(nil)
	xw, err := xz.NewWriter(buffer)
	if err != nil {
		log.Fatal(err)
	}

	tw := tar.NewWriter(xw)

	files := []ArchiveFile{
		{Name: "some-dir/"},
		{Name: "some-dir/some-other-dir/"},
		{Name: "some-dir/some-other-dir/some-file", Content: []byte("some-dir/some-other-dir/some-file")},
		{Name: "first", Content: []byte("first")},
		{Name: "second", Content: []byte("second")},
		{Name: "third", Content: []byte("third")},
	}

	for _, file := range files {
		err := tw.WriteHeader(&tar.Header{Name: file.Name, Mode: 0755, Size: int64(len(file.Content))})
		if err != nil {
			log.Fatal(err)
		}

		_, err = tw.Write(file.Content)
		if err != nil {
			log.Fatal(err)
		}
	}

	tw.Close()
	xw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewTarXZArchive(bytes.NewReader(buffer.Bytes())).StripComponents(1)
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

some-other-dir/some-file

type ZipArchive

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

A ZipArchive decompresses zip files from an input stream.

Example
package main

import (
	"archive/zip"
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/paketo-buildpacks/packit/vacation"
)

type ArchiveFile struct {
	Name    string
	Content []byte
}

func main() {
	buffer := bytes.NewBuffer(nil)
	zw := zip.NewWriter(buffer)

	files := []ArchiveFile{
		{Name: "some-dir/"},
		{Name: "some-dir/some-other-dir/"},
		{Name: "some-dir/some-other-dir/some-file", Content: []byte("some-dir/some-other-dir/some-file")},
		{Name: "first", Content: []byte("first")},
		{Name: "second", Content: []byte("second")},
		{Name: "third", Content: []byte("third")},
	}

	for _, file := range files {
		header := &zip.FileHeader{Name: file.Name}
		header.SetMode(0755)

		f, err := zw.CreateHeader(header)
		if err != nil {
			log.Fatal(err)
		}

		if _, err := f.Write(file.Content); err != nil {
			log.Fatal(err)
		}
	}

	zw.Close()

	destination, err := os.MkdirTemp("", "destination")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(destination)

	archive := vacation.NewZipArchive(bytes.NewReader(buffer.Bytes()))
	if err := archive.Decompress(destination); err != nil {
		log.Fatal(err)
	}

	err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			rel, err := filepath.Rel(destination, path)
			if err != nil {
				log.Fatal(err)
			}

			fmt.Printf("%s\n", rel)
			return nil
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

first
second
some-dir/some-other-dir/some-file
third

func NewZipArchive

func NewZipArchive(inputReader io.Reader) ZipArchive

NewZipArchive returns a new ZipArchive that reads from inputReader.

func (ZipArchive) Decompress

func (z ZipArchive) Decompress(destination string) error

Decompress reads from ZipArchive and writes files into the destination specified.

Jump to

Keyboard shortcuts

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