ziputil

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 29 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const Bzip2 uint16 = 12

Method number of bzip2 compressed members, which readers decode

View Source
const Deflate64 uint16 = 9

Method number of Deflate64 compressed members, which readers decode

View Source
const LZMA uint16 = 14

Method number of LZMA compressed members, which readers decode

View Source
const XZ uint16 = 95

Method number of XZ compressed members, which readers decode

View Source
const Zstd uint16 = 93

Method number of Zstandard compressed members, which readers decode and writers produce

Variables

View Source
var ErrCorrupt = errors.New("corrupt zip member")

Damage in a member's stored data: a bad checksum, a broken or cut short compressed stream, a bad local header, or data that runs into another member

View Source
var ErrEncrypted = errors.New("zip member is encrypted")

Member protected by encryption, which this package does not decrypt

View Source
var ErrLimit = errors.New("zip archive exceeds an extraction limit")

Archive holding more members or declaring more uncompressed bytes than an extraction limit allows

Functions

func IsZip

func IsZip(path string) bool

Whether path holds an archive whose end record leads to a readable central directory, without reading any member

Types

type ExtractOption

type ExtractOption func(*extractLimits)

Limit Extract checks the whole archive against before it writes anything

func MaxBytes

func MaxBytes(n int64) ExtractOption

Most uncompressed bytes the file and link members may declare in total, which their decoding is held to, 16 GiB unless set, none at zero or below

func MaxMembers

func MaxMembers(n int) ExtractOption

Most members Extract accepts, 1<<20 unless set, none at zero or below

type Reader

type Reader struct {
	*zip.Reader
	// contains filtered or unexported fields
}

Archive open for reading: the embedded zip.Reader with member names and comments decoded (Unicode extra fields, code page 437 for bytes from DOS-family systems that are not UTF-8, other bytes kept as stored) and decoders for Deflate, Deflate64, bzip2, LZMA, Zstandard and XZ members registered

func NewReader

func NewReader(ra io.ReaderAt, size int64) (*Reader, error)

Archive of size bytes read through ra; members whose names are unsafe to extract are kept, Extract making every name safe

func OpenReader

func OpenReader(path string) (*Reader, error)

Archive at path opened for reading

func (*Reader) Close

func (r *Reader) Close() error

Closes the file OpenReader opened, nothing to do for a reader NewReader made

func (*Reader) Extract

func (r *Reader) Extract(dir string, opts ...ExtractOption) error

Writes every member under dir, creating it, after checking the archive against the limits (1<<20 members, 16 GiB declared, ErrLimit beyond): names lose roots, drive letters and empty, . and .. elements and are made legal on the host; each file is written beside its path and renamed over it once its data checked, so a failing member leaves nothing and an old file or link there is replaced, never written through; files get mode 0o666 plus the member's execute bits, never setuid or setgid, and their recorded modification time, directories 0o777 and theirs once everything is written; symlinks whose target stays inside dir become links after all files, other symlinks files holding the target; of members with the same path the later one wins; a failure stops at that member; nothing is ever written outside dir

func (*Reader) Lookup

func (r *Reader) Lookup(name string) (*zip.File, bool)

Member of a name, the last one when the archive repeats the name

func (*Reader) Names

func (r *Reader) Names() []string

Member names in central directory order

func (*Reader) Open

func (r *Reader) Open(name string) (io.ReadCloser, error)

Data of the member of a name, the last one when the archive repeats it: refused with ErrEncrypted or zip.ErrAlgorithm when it cannot be decoded, and with damage, overlap with another member included, matching ErrCorrupt; this takes the place of the fs.FS Open, which r.Reader keeps offering

type Writer

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

Archive written to a file member by member, each member's sizes and checksum put in its local header before the next member starts, so that members can be dropped again until Close

func Create

func Create(name string, method uint16, level int) (*Writer, error)

New archive in the file at name, replacing any file there, whose members are compressed with method, one of zip.Store, zip.Deflate and Zstd, at level unless their header names another method

func (*Writer) AddFile

func (w *Writer) AddFile(file, name string) error

Adds the file or directory on disk at file as name, file itself when name is empty, made a relative slash path, with its modification time and mode; a symlink adds what it points to, other kinds of file are refused, and a failure leaves no trace of the member

func (*Writer) Close

func (w *Writer) Close() error

Finishes an unfinished member, writes the central directory and end records and closes the file, returning the first failure; a member that fails to finish is left out of an archive that stays valid

func (*Writer) Create

func (w *Writer) Create(name string) (io.WriteCloser, error)

Member named name compressed with the archive's method, with mode 0o600 (0o700 for a name ending in a slash, which makes a directory) and the 1980-01-01 date of members without a time

func (*Writer) CreateHeader

func (w *Writer) CreateHeader(fh *zip.FileHeader) (io.WriteCloser, error)

Member described by a copy of fh, so that fh stays the caller's to change or reuse, whose Method picks the compressor and whose UncompressedSize64, when set, is the expected size; the writer settles the flags, versions, sizes and checksum in its copy, and a name ending in a slash makes a directory that takes no data

func (*Writer) Len

func (w *Writer) Len() int

Number of members finished so far

func (*Writer) RegisterCompressor

func (w *Writer) RegisterCompressor(method uint16, comp zip.Compressor)

Compressor for members of method, taking the place of the built-in one

func (*Writer) SetComment

func (w *Writer) SetComment(comment string) error

Sets the archive comment Close writes

func (*Writer) Truncate

func (w *Writer) Truncate(n int) error

Drops every member after the first n, an unfinished one included, and cuts their bytes off the file

Jump to

Keyboard shortcuts

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