package zip
Import Path
archive/zip (on golang.org and go.dev)
Dependency Relation
imports 14 packages, and imported by 0 packages
Involved Source Files
reader.go
register.go
d-> struct.go
writer.go
Exported Type Names
type Compressor (func)
A Compressor returns a new compressing writer, writing to w.
The WriteCloser's Close method must be used to flush pending data to w.
The Compressor itself must be safe to invoke from multiple goroutines
simultaneously, but each returned writer will be used only by
one goroutine at a time.
func RegisterCompressor(method uint16, comp Compressor)
func (*Writer).RegisterCompressor(method uint16, comp Compressor)
type Decompressor (func)
A Decompressor returns a new decompressing reader, reading from r.
The ReadCloser's Close method must be used to release associated resources.
The Decompressor itself must be safe to invoke from multiple goroutines
simultaneously, but each returned reader will be used only by
one goroutine at a time.
func RegisterDecompressor(method uint16, dcomp Decompressor)
func (*Reader).RegisterDecompressor(method uint16, dcomp Decompressor)
type File (struct)
FileHeader FileHeader
FileHeader.CRC32 uint32
Comment is any arbitrary user-defined string shorter than 64KiB.
// Deprecated: Use CompressedSize64 instead.
FileHeader.CompressedSize64 uint64
FileHeader.CreatorVersion uint16
// Meaning depends on CreatorVersion
FileHeader.Extra []byte
FileHeader.Flags uint16
Method is the compression method. If zero, Store is used.
Modified is the modified time of the file.
When reading, an extended timestamp is preferred over the legacy MS-DOS
date field, and the offset between the times is used as the timezone.
If only the MS-DOS date is present, the timezone is assumed to be UTC.
When writing, an extended timestamp (which is timezone-agnostic) is
always emitted. The legacy MS-DOS date field is encoded according to the
location of the Modified time.
// Deprecated: Legacy MS-DOS time; use Modified instead.
// Deprecated: Legacy MS-DOS date; use Modified instead.
Name is the name of the file.
It must be a relative path, not start with a drive letter (such as "C:"),
and must use forward slashes instead of back slashes. A trailing slash
indicates that this file is a directory and should have no data.
When reading zip files, the Name field is populated from
the zip file directly and is not validated for correctness.
It is the caller's responsibility to sanitize it as
appropriate, including canonicalizing slash directions,
validating that paths are relative, and preventing path
traversal through filenames ("../../../").
NonUTF8 indicates that Name and Comment are not encoded in UTF-8.
By specification, the only other encoding permitted should be CP-437,
but historically many ZIP readers interpret Name and Comment as whatever
the system's local character encoding happens to be.
This flag should only be set if the user intends to encode a non-portable
ZIP file for a specific localized region. Otherwise, the Writer
automatically sets the ZIP format's UTF-8 flag for valid UTF-8 strings.
FileHeader.ReaderVersion uint16
// Deprecated: Use UncompressedSize64 instead.
FileHeader.UncompressedSize64 uint64
DataOffset returns the offset of the file's possibly-compressed
data, relative to the beginning of the zip file.
Most callers should instead use Open, which transparently
decompresses data and verifies checksums.
FileInfo returns an os.FileInfo for the FileHeader.
ModTime returns the modification time in UTC using the legacy
ModifiedDate and ModifiedTime fields.
Deprecated: Use Modified instead.
Mode returns the permission and mode bits for the FileHeader.
Open returns a ReadCloser that provides access to the File's contents.
Multiple files may be read concurrently.
SetModTime sets the Modified, ModifiedTime, and ModifiedDate fields
to the given time in UTC.
Deprecated: Use Modified instead.
SetMode changes the permission and mode bits for the FileHeader.
type FileHeader (struct)
FileHeader describes a file within a zip file.
See the zip spec for details.
CRC32 uint32
Comment is any arbitrary user-defined string shorter than 64KiB.
// Deprecated: Use CompressedSize64 instead.
CompressedSize64 uint64
CreatorVersion uint16
// Meaning depends on CreatorVersion
Extra []byte
Flags uint16
Method is the compression method. If zero, Store is used.
Modified is the modified time of the file.
When reading, an extended timestamp is preferred over the legacy MS-DOS
date field, and the offset between the times is used as the timezone.
If only the MS-DOS date is present, the timezone is assumed to be UTC.
When writing, an extended timestamp (which is timezone-agnostic) is
always emitted. The legacy MS-DOS date field is encoded according to the
location of the Modified time.
// Deprecated: Legacy MS-DOS time; use Modified instead.
// Deprecated: Legacy MS-DOS date; use Modified instead.
Name is the name of the file.
It must be a relative path, not start with a drive letter (such as "C:"),
and must use forward slashes instead of back slashes. A trailing slash
indicates that this file is a directory and should have no data.
When reading zip files, the Name field is populated from
the zip file directly and is not validated for correctness.
It is the caller's responsibility to sanitize it as
appropriate, including canonicalizing slash directions,
validating that paths are relative, and preventing path
traversal through filenames ("../../../").
NonUTF8 indicates that Name and Comment are not encoded in UTF-8.
By specification, the only other encoding permitted should be CP-437,
but historically many ZIP readers interpret Name and Comment as whatever
the system's local character encoding happens to be.
This flag should only be set if the user intends to encode a non-portable
ZIP file for a specific localized region. Otherwise, the Writer
automatically sets the ZIP format's UTF-8 flag for valid UTF-8 strings.
ReaderVersion uint16
// Deprecated: Use UncompressedSize64 instead.
UncompressedSize64 uint64
FileInfo returns an os.FileInfo for the FileHeader.
ModTime returns the modification time in UTC using the legacy
ModifiedDate and ModifiedTime fields.
Deprecated: Use Modified instead.
Mode returns the permission and mode bits for the FileHeader.
SetModTime sets the Modified, ModifiedTime, and ModifiedDate fields
to the given time in UTC.
Deprecated: Use Modified instead.
SetMode changes the permission and mode bits for the FileHeader.
func FileInfoHeader(fi os.FileInfo) (*FileHeader, error)
func (*Writer).CreateHeader(fh *FileHeader) (io.Writer, error)
type ReadCloser (struct)
Reader Reader
Reader.Comment string
Reader.File []*File
Close closes the Zip file, rendering it unusable for I/O.
RegisterDecompressor registers or overrides a custom decompressor for a
specific method ID. If a decompressor for a given method is not found,
Reader will default to looking up the decompressor at the package level.
*T : io.Closer
func OpenReader(name string) (*ReadCloser, error)
type Reader (struct)
Comment string
File []*File
RegisterDecompressor registers or overrides a custom decompressor for a
specific method ID. If a decompressor for a given method is not found,
Reader will default to looking up the decompressor at the package level.
func NewReader(r io.ReaderAt, size int64) (*Reader, error)
type Writer (struct)
Writer implements a zip file writer.
Close finishes writing the zip file by writing the central directory.
It does not close the underlying writer.
Create adds a file to the zip file using the provided name.
It returns a Writer to which the file contents should be written.
The file contents will be compressed using the Deflate method.
The name must be a relative path: it must not start with a drive
letter (e.g. C:) or leading slash, and only forward slashes are
allowed. To create a directory instead of a file, add a trailing
slash to the name.
The file's contents must be written to the io.Writer before the next
call to Create, CreateHeader, or Close.
CreateHeader adds a file to the zip archive using the provided FileHeader
for the file metadata. Writer takes ownership of fh and may mutate
its fields. The caller must not modify fh after calling CreateHeader.
This returns a Writer to which the file contents should be written.
The file's contents must be written to the io.Writer before the next
call to Create, CreateHeader, or Close.
Flush flushes any buffered data to the underlying writer.
Calling Flush is not normally necessary; calling Close is sufficient.
RegisterCompressor registers or overrides a custom compressor for a specific
method ID. If a compressor for a given method is not found, Writer will
default to looking up the compressor at the package level.
SetComment sets the end-of-central-directory comment field.
It can only be called before Close.
SetOffset sets the offset of the beginning of the zip data within the
underlying writer. It should be used when the zip data is appended to an
existing file, such as a binary executable.
It must be called before any data is written.
*T : io.Closer
func NewWriter(w io.Writer) *Writer
Exported Values
var ErrAlgorithm error
var ErrChecksum error
func FileInfoHeader(fi os.FileInfo) (*FileHeader, error)
FileInfoHeader creates a partially-populated FileHeader from an
os.FileInfo.
Because os.FileInfo's Name method returns only the base name of
the file it describes, it may be necessary to modify the Name field
of the returned header to provide the full path name of the file.
If compression is desired, callers should set the FileHeader.Method
field; it is unset by default.
func NewReader(r io.ReaderAt, size int64) (*Reader, error)
NewReader returns a new Reader reading from r, which is assumed to
have the given size in bytes.
func OpenReader(name string) (*ReadCloser, error)
OpenReader will open the Zip file specified by name and return a ReadCloser.
func RegisterCompressor(method uint16, comp Compressor)
RegisterCompressor registers custom compressors for a specified method ID.
The common methods Store and Deflate are built in.
func RegisterDecompressor(method uint16, dcomp Decompressor)
RegisterDecompressor allows custom decompressors for a specified method ID.
The common methods Store and Deflate are built in.
![]() |
The pages are generated with Golds v0.1.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project and developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |