zipr

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Store   compressionAlgo = 0 // no compression
	Deflate compressionAlgo = 8 // max compression
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Zipr

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

Zipr tracks and reports progress of zip operations.

Zipr is designed to be thread-safe and can be used concurrently from multiple goroutines. It maintains a cumulative count of bytes processed across all read operations and periodically reports progress through the provided channel.

func New

func New(ctx context.Context, progressCh chan<- uint64, logCh chan<- string, algo compressionAlgo) *Zipr

New creates a new Zipr instance with the specified compression algorithm.

Parameters:

  • progressCh: A channel that receives progress updates during zip operations. The first value sent is the total size of all files to be zipped, and subsequent values report the number of bytes processed so far. To avoid missing the initial total filesize report, ensure you're already reading from this channel before calling Zipr.CreateArchive/Zipr.CreateArchives or make this channel buffered with at least a capacity of 1.
  • logCh: A channel that receives paths to the files under progress.
  • algo: The compression algorithm to use for the zip operation. Supported algorithms are defined in the compressionAlgo type.

WARNING: All writes to channels are non-blocking.

Example:

progressCh := make(chan uint64, 1) // Buffered to ensure total size is not missed
logCh := make(chan string) // Buffered/Unbuffered as needed
zipper := zipr.Get(progressCh, logCh, zipr.Deflate) // Using DEFLATE compression
noReportZipper := zipr.Get(nil, nil, zipr.Store) // No progress reporting

func (*Zipr) Close

func (z *Zipr) Close() error

Close sends a final progress update and closes the progress channel, and sets the accumulative read bytes to 0.

This should be called when all zip operations are complete to ensure the final progress is reported and to allow any goroutines reading from the progress channel to terminate properly.

func (*Zipr) CreateArchive

func (z *Zipr) CreateArchive(path, archiveName, root string, files ...string) (string, error)

CreateArchive creates a zip archive of the specified files/directories.

If no filenames are provided, it creates a zip archive of the entire root directory. The first write to progressChan will be the total size of the archive.

Parameters:

  • ctx: Context for cancelling the operation - if cancelled, any partially created archive will be deleted
  • path: The directory where the zip archive will be created
  • archiveName: The name of the zip archive to create (should include .zip extension)
  • root: The path to the root directory to zip
  • files: Optional list of file or directory names within the root directory to zip

Returns:

  • The full path to the created zip archive
  • An error if the operation fails

Example:

// Zip an entire directory
path, err := zipper.CreateArchive(context.Background(), "/tmp", "backup.zip", "/home/user/documents")

// Zip specific files within a directory
path, err := zipper.CreateArchive(context.Background() ,"/tmp", "partial.zip", "/home/user/documents", "file1.txt", "folder1")

func (*Zipr) CreateArchives

func (z *Zipr) CreateArchives(path, root string, dirs ...string) ([]string, error)

CreateArchives concurrently creates zip archives for multiple directories.

This method creates a separate zip file for each directory specified in the dirs parameter. All zipping operations run concurrently using a worker pool for maximum efficiency.

Parameters:

  • path: The directory where the zip archives will be created
  • root: The base path containing the directories to be zipped
  • dirs: List of directory names within the root to zip (each becomes a separate archive)

Returns:

  • A slice of paths to the created zip archives, in the same order as the input dirs
  • An error if any zipping operation fails

Example:

// Zip multiple directories concurrently
paths, err := zipper.CreateArchives("/tmp", "/home/user", "documents", "pictures", "music")
if err != nil {
    log.Fatal(err)
}
for _, path := range paths {
    fmt.Println("Created archive:", path)
}

Jump to

Keyboard shortcuts

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