zipper

package
v0.0.0-...-4e09ae6 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BestCompressionZipWriter

func BestCompressionZipWriter(w io.Writer) *zip.Writer

BestCompressionZipWriter returns a zip.Writer that registers flate.BestCompression as its compressor.

func DefaultProgressReporter

func DefaultProgressReporter(src, dst string, written int64, done bool)

DefaultProgressReporter is the default report that only reports upon a file being successfully added to archive.

func NoCompressionZipWriter

func NoCompressionZipWriter(w io.Writer) *zip.Writer

NoCompressionZipWriter returns a zip.Writer that registers flate.NoCompression as its compressor.

Types

type ProgressReporter

type ProgressReporter func(src, dst string, written int64, done bool)

ProgressReporter is called to provide update on compressing individual files.

  • src: path of the file being added to the archive
  • dst: relative path of the file in the archive
  • written: number of bytes of the file that has been read and written to archive so far
  • done: is true only when the file has been read and written in its entirety

The method will be called at least once for every file being compressed. If the file is small enough to fit into one read (see DefaultBufferSize), then the method is called exactly once with `done` being true.

func NewDirectoryProgressReporter

func NewDirectoryProgressReporter(ctx context.Context, root string, reporter func(src, dst string, written, size int64, done bool, wc, fc int)) (ProgressReporter, error)

NewDirectoryProgressReporter creates a progress reporter intended to be used for compressing a directory.

Specifically, the new progress reporter is aware of how many files are there to be compressed by doing a preflight filepath.WalkDir (also cancellable), and for each file being compressed, the reporter is aware of the total number of bytes for that file. If the initial filepath.WalkDir fails, its error wil be returned.

  • src: path of the file being added to the archive
  • dst: relative path of the file in the archive
  • written: number of bytes of the file that has been read and written to archive so far
  • size: the total number of bytes of the file being compressed. Can be -1 if os.Stat fails.
  • done: is true only when the file has been read and written in its entirety (written==size)
  • wc: the number of files that has been written to archive so far
  • fc: the total number of files to be written to archive

func NewProgressBarReporter

func NewProgressBarReporter(ctx context.Context, root string, bar *progressbar.ProgressBar) (ProgressReporter, error)

NewProgressBarReporter creates a progress report that uses the specified progressbar.ProgressBar.

If the given progress bar is nil, it will be created with progressbar.DefaultBytes.

type Zipper

type Zipper struct {
	// ProgressReporter controls how progress is reported.
	ProgressReporter ProgressReporter

	// NewWriter allows customization of the zip.Writer being used.
	//
	// Default to BestCompressionZipWriter.
	NewWriter func(w io.Writer) *zip.Writer
}

Zipper is used to recursively compress (with zip) a directory or file with progress report and cancellable context.

func New

func New() *Zipper

New creates a new Zipper with default settings.

By default, log.Printf will be used to provide progress update only on each file being added to the archive.

func (Zipper) CompressDir

func (z Zipper) CompressDir(ctx context.Context, root string, dst io.Writer, junkRoot bool) error

CompressDir compresses a directory recursively to the archive opened as io.Writer.

JunkRoot determines whether all compressed files are under a single root directory hierarchy or not.

For example, when compressing directory named "test" (root="test") with JunkRoot being false, all files in that directory and their children will be added to archive using these paths:

test/a.txt
test/path/b.txt
test/another/path/c.txt

If JunkRoot is true, the files will use these paths:

a.txt
path/b.txt
another/path/c.txt

func (Zipper) CompressFile

func (z Zipper) CompressFile(ctx context.Context, name string, dst io.Writer) error

CompressFile compresses a single file to the archive opened as io.Writer.

Jump to

Keyboard shortcuts

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