Documentation
¶
Index ¶
- Constants
- Variables
- func IsZip(path string) bool
- type ExtractOption
- type Reader
- type Writer
- func (w *Writer) AddFile(file, name string) error
- func (w *Writer) Close() error
- func (w *Writer) Create(name string) (io.WriteCloser, error)
- func (w *Writer) CreateHeader(fh *zip.FileHeader) (io.WriteCloser, error)
- func (w *Writer) Len() int
- func (w *Writer) RegisterCompressor(method uint16, comp zip.Compressor)
- func (w *Writer) SetComment(comment string) error
- func (w *Writer) Truncate(n int) error
Constants ¶
const Bzip2 uint16 = 12
Method number of bzip2 compressed members, which readers decode
const Deflate64 uint16 = 9
Method number of Deflate64 compressed members, which readers decode
const LZMA uint16 = 14
Method number of LZMA compressed members, which readers decode
const XZ uint16 = 95
Method number of XZ compressed members, which readers decode
const Zstd uint16 = 93
Method number of Zstandard compressed members, which readers decode and writers produce
Variables ¶
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
var ErrEncrypted = errors.New("zip member is encrypted")
Member protected by encryption, which this package does not decrypt
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 ¶
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 ¶
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 ¶
Archive of size bytes read through ra; members whose names are unsafe to extract are kept, Extract making every name safe
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) 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 ¶
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 ¶
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 ¶
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) 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 ¶
Sets the archive comment Close writes