yazip

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package yazip provides a high-level, ergonomic API for extracting zip archives. It wraps Go's archive/zip standard library with a functional options pattern that enables common operations, such as filtering files, controlling destination directories, and enforcing security limits, without requiring boilerplate code.

Basic Usage

err := yazip.Extract(ctx, "archive.zip")

With Options

err := yazip.Extract(ctx, "archive.zip",
    yazip.WithDestination("/tmp/output"),
    yazip.WithCreateDir(),
    yazip.WithIgnorePrefix("__MACOSX", "."),
    yazip.WithMaxTotalSize(500*1024*1024),
)

Security

Zip-slip protection is always enabled and cannot be disabled. Archive entries that would resolve outside the destination directory cause an immediate error.

Size and count limits can be set via WithMaxTotalSize and WithMaxFiles to protect against zip bombs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Extract

func Extract(ctx context.Context, zipPath string, opts ...Option) error

Extract extracts files from a zip archive path to a destination directory.

func ExtractReader

func ExtractReader(ctx context.Context, r io.ReaderAt, size int64, opts ...Option) error

ExtractReader extracts files from an in-memory or streamed zip archive.

Types

type Option

type Option func(*options)

Option configures extraction behavior. Use the With* functions to create options.

func WithCreateDir

func WithCreateDir() Option

WithCreateDir creates a directory named after the zip file inside the destination.

func WithDestination

func WithDestination(dir string) Option

WithDestination sets the extraction target directory.

func WithFilter

func WithFilter(fn func(string, fs.FileInfo) bool) Option

WithFilter sets a custom per-entry filter function.

func WithFlatten

func WithFlatten() Option

WithFlatten discards directory structure and extracts all files into destination root.

func WithIgnore

func WithIgnore(paths ...string) Option

WithIgnore adds exact relative paths to the ignore set.

func WithIgnorePrefix

func WithIgnorePrefix(prefixes ...string) Option

WithIgnorePrefix skips entries whose name starts with any provided prefix. Prefixes are matched against the last element of the relative entry path

func WithIgnoreRegex

func WithIgnoreRegex(patterns ...string) Option

WithIgnoreRegex skips entries matching any provided regex pattern.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets a custom structured logger for extraction.

func WithMaxFiles

func WithMaxFiles(n int) Option

WithMaxFiles limits the number of files processed from the archive.

func WithMaxTotalSize

func WithMaxTotalSize(n int64) Option

WithMaxTotalSize limits cumulative extracted bytes across all files.

func WithMode

func WithMode(mode fs.FileMode) Option

WithMode overrides permissions for all extracted files and created directories.

func WithOnly

func WithOnly(paths ...string) Option

WithOnly adds exact relative paths to the allowlist.

func WithOnlyRegex

func WithOnlyRegex(patterns ...string) Option

WithOnlyRegex extracts only entries matching at least one provided regex pattern.

func WithOverwrite

func WithOverwrite(mode OverwriteMode) Option

WithOverwrite sets the overwrite policy.

func WithPreservePermissions

func WithPreservePermissions() Option

WithPreservePermissions uses permissions stored in the archive when available.

type OverwriteMode

type OverwriteMode int

OverwriteMode controls behavior when a destination file already exists.

const (
	// OverwriteAlways overwrites destination files when they already exist.
	OverwriteAlways OverwriteMode = iota
	// OverwriteNever keeps destination files unchanged when they already exist.
	OverwriteNever
	// OverwriteIfNewer overwrites destination files only when the archive entry
	// has a newer modification time than the destination file.
	OverwriteIfNewer
)

Jump to

Keyboard shortcuts

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