Functions
AddFS
method
#
AddFS adds the files from fs.FS to the archive.
It walks the directory tree starting at the root of the filesystem
adding each file to the zip using deflate while maintaining the directory structure.
func (w *Writer) AddFS(fsys fs.FS) error
Close
method
#
func (r *dirReader) Close() error
Close
method
#
func (w *pooledFlateWriter) Close() error
Close
method
#
func (w nopCloser) Close() error
Close
method
#
func (d *openDir) Close() error
Close
method
#
Close closes the Zip file, rendering it unusable for I/O.
func (rc *ReadCloser) Close() error
Close
method
#
func (r *pooledFlateReader) Close() error
Close
method
#
func (r *checksumReader) Close() error
Close
method
#
Close finishes writing the zip file by writing the central directory.
It does not close the underlying writer.
func (w *Writer) Close() error
Copy
method
#
Copy copies the file f (obtained from a [Reader]) into w. It copies the raw
form directly bypassing decompression, compression, and validation.
func (w *Writer) Copy(f *File) error
Create
method
#
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. Duplicate names will not overwrite previous entries
and are appended to the zip file.
The file's contents must be written to the [io.Writer] before the next
call to [Writer.Create], [Writer.CreateHeader], or [Writer.Close].
func (w *Writer) Create(name string) (io.Writer, error)
CreateRaw
method
#
CreateRaw adds a file to the zip archive using the provided [FileHeader] and
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 [Writer.Create],
[Writer.CreateHeader], [Writer.CreateRaw], or [Writer.Close].
In contrast to [Writer.CreateHeader], the bytes passed to Writer are not compressed.
CreateRaw's argument is stored in w. If the argument is a pointer to the embedded
[FileHeader] in a [File] obtained from a [Reader] created from in-memory data,
then w will refer to all of that memory.
func (w *Writer) CreateRaw(fh *FileHeader) (io.Writer, error)
DataOffset
method
#
DataOffset returns the offset of the file's possibly-compressed
data, relative to the beginning of the zip file.
Most callers should instead use [File.Open], which transparently
decompresses data and verifies checksums.
func (f *File) DataOffset() (offset int64, err error)
FileInfo
method
#
FileInfo returns an fs.FileInfo for the [FileHeader].
func (h *FileHeader) FileInfo() fs.FileInfo
Flush
method
#
Flush flushes any buffered data to the underlying writer.
Calling Flush is not normally necessary; calling Close is sufficient.
func (w *Writer) Flush() error
Info
method
#
func (f *fileListEntry) Info() (fs.FileInfo, error)
Info
method
#
func (fi headerFileInfo) Info() (fs.FileInfo, error)
IsDir
method
#
func (f *fileListEntry) IsDir() bool
IsDir
method
#
func (fi headerFileInfo) IsDir() bool
ModTime
method
#
func (fi headerFileInfo) ModTime() time.Time
ModTime
method
#
func (f *fileListEntry) ModTime() time.Time
ModTime
method
#
ModTime returns the modification time in UTC using the legacy
[ModifiedDate] and [ModifiedTime] fields.
Deprecated: Use [Modified] instead.
func (h *FileHeader) ModTime() time.Time
Mode
method
#
Mode returns the permission and mode bits for the [FileHeader].
func (h *FileHeader) Mode() (mode fs.FileMode)
Mode
method
#
func (fi headerFileInfo) Mode() fs.FileMode
Mode
method
#
func (f *fileListEntry) Mode() fs.FileMode
Name
method
#
Only used for directories.
func (f *fileListEntry) Name() string
Name
method
#
func (fi headerFileInfo) Name() string
NewReader
function
#
NewReader returns a new [Reader] reading from r, which is assumed to
have the given size in bytes.
If any file inside the archive uses a non-local name
(as defined by [filepath.IsLocal]) or a name containing backslashes
and the GODEBUG environment variable contains `zipinsecurepath=0`,
NewReader returns the reader with an [ErrInsecurePath] error.
A future version of Go may introduce this behavior by default.
Programs that want to accept non-local names can ignore
the [ErrInsecurePath] error and use the returned reader.
func NewReader(r io.ReaderAt, size int64) (*Reader, error)
NewWriter
function
#
NewWriter returns a new [Writer] writing a zip file to w.
func NewWriter(w io.Writer) *Writer
Open
method
#
Open opens the named file in the ZIP archive,
using the semantics of fs.FS.Open:
paths are always slash separated, with no
leading / or ../ elements.
func (r *Reader) Open(name string) (fs.File, error)
Open
method
#
Open returns a [ReadCloser] that provides access to the [File]'s contents.
Multiple files may be read concurrently.
func (f *File) Open() (io.ReadCloser, error)
OpenRaw
method
#
OpenRaw returns a [Reader] that provides access to the [File]'s contents without
decompression.
func (f *File) OpenRaw() (io.Reader, error)
OpenReader
function
#
OpenReader will open the Zip file specified by name and return a ReadCloser.
If any file inside the archive uses a non-local name
(as defined by [filepath.IsLocal]) or a name containing backslashes
and the GODEBUG environment variable contains `zipinsecurepath=0`,
OpenReader returns the reader with an ErrInsecurePath error.
A future version of Go may introduce this behavior by default.
Programs that want to accept non-local names can ignore
the ErrInsecurePath error and use the returned reader.
func OpenReader(name string) (*ReadCloser, error)
Read
method
#
func (r *dirReader) Read([]byte) (int, error)
Read
method
#
func (d *openDir) Read([]byte) (int, error)
Read
method
#
func (r *pooledFlateReader) Read(p []byte) (n int, err error)
Read
method
#
func (r *checksumReader) Read(b []byte) (n int, err error)
ReadDir
method
#
func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error)
RegisterCompressor
method
#
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.
func (w *Writer) RegisterCompressor(method uint16, comp Compressor)
RegisterCompressor
function
#
RegisterCompressor registers custom compressors for a specified method ID.
The common methods [Store] and [Deflate] are built in.
func RegisterCompressor(method uint16, comp Compressor)
RegisterDecompressor
function
#
RegisterDecompressor allows custom decompressors for a specified method ID.
The common methods [Store] and [Deflate] are built in.
func RegisterDecompressor(method uint16, dcomp Decompressor)
RegisterDecompressor
method
#
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 (r *Reader) RegisterDecompressor(method uint16, dcomp Decompressor)
SetModTime
method
#
SetModTime sets the [Modified], [ModifiedTime], and [ModifiedDate] fields
to the given time in UTC.
Deprecated: Use [Modified] instead.
func (h *FileHeader) SetModTime(t time.Time)
SetMode
method
#
SetMode changes the permission and mode bits for the [FileHeader].
func (h *FileHeader) SetMode(mode fs.FileMode)
SetOffset
method
#
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.
func (w *Writer) SetOffset(n int64)
Size
method
#
func (fi headerFileInfo) Size() int64
Size
method
#
func (f *fileListEntry) Size() int64
Stat
method
#
func (d *openDir) Stat() (fs.FileInfo, error)
Stat
method
#
func (r *checksumReader) Stat() (fs.FileInfo, error)
String
method
#
func (f *fileListEntry) String() string
String
method
#
func (fi headerFileInfo) String() string
Sys
method
#
func (f *fileListEntry) Sys() any
Sys
method
#
func (fi headerFileInfo) Sys() any
Type
method
#
func (fi headerFileInfo) Type() fs.FileMode
Type
method
#
func (f *fileListEntry) Type() fs.FileMode
Write
method
#
func (w *fileWriter) Write(p []byte) (int, error)
Write
method
#
func (dirWriter) Write(b []byte) (int, error)
Write
method
#
func (w *countWriter) Write(p []byte) (int, error)
Write
method
#
func (w *pooledFlateWriter) Write(p []byte) (n int, err error)
close
method
#
func (w *fileWriter) close() error
compressor
method
#
func (w *Writer) compressor(method uint16) Compressor
compressor
function
#
func compressor(method uint16) Compressor
decompressor
function
#
func decompressor(method uint16) Decompressor
decompressor
method
#
func (r *Reader) decompressor(method uint16) Decompressor
detectUTF8
function
#
detectUTF8 reports whether s is a valid UTF-8 string, and whether the string
must be considered UTF-8 encoding (i.e., not compatible with CP-437, ASCII,
or any other common encoding).
func detectUTF8(s string) (valid bool, require bool)
fileEntryCompare
function
#
func fileEntryCompare(x string, y string) int
fileModeToUnixMode
function
#
func fileModeToUnixMode(mode fs.FileMode) uint32
findBodyOffset
method
#
findBodyOffset does the minimum work to verify the file has a header
and returns the file body offset.
func (f *File) findBodyOffset() (int64, error)
findDirectory64End
function
#
findDirectory64End tries to read the zip64 locator just before the
directory end and returns the offset of the zip64 directory end if
found.
func findDirectory64End(r io.ReaderAt, directoryEndOffset int64) (int64, error)
findSignatureInBlock
function
#
func findSignatureInBlock(b []byte) int
hasDataDescriptor
method
#
func (h *FileHeader) hasDataDescriptor() bool
init
method
#
func (r *Reader) init(rdr io.ReaderAt, size int64) error
init
function
#
func init()
initFileList
method
#
func (r *Reader) initFileList()
isZip64
method
#
isZip64 reports whether the file size exceeds the 32 bit limit
func (h *FileHeader) isZip64() bool
msDosTimeToTime
function
#
msDosTimeToTime converts an MS-DOS date and time into a time.Time.
The resolution is 2s.
See: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-dosdatetimetofiletime
func msDosTimeToTime(dosDate uint16, dosTime uint16) time.Time
msdosModeToFileMode
function
#
func msdosModeToFileMode(m uint32) (mode fs.FileMode)
newFlateReader
function
#
func newFlateReader(r io.Reader) io.ReadCloser
newFlateWriter
function
#
func newFlateWriter(w io.Writer) io.WriteCloser
openLookup
method
#
func (r *Reader) openLookup(name string) *fileListEntry
openReadDir
method
#
func (r *Reader) openReadDir(dir string) []fileListEntry
prepare
method
#
prepare performs the bookkeeping operations required at the start of
CreateHeader and CreateRaw.
func (w *Writer) prepare(fh *FileHeader) error
readDataDescriptor
function
#
func readDataDescriptor(r io.Reader, f *File) error
readDirectory64End
function
#
readDirectory64End reads the zip64 directory end and updates the
directory end with the zip64 directory end values.
func readDirectory64End(r io.ReaderAt, offset int64, d *directoryEnd) (err error)
readDirectoryEnd
function
#
func readDirectoryEnd(r io.ReaderAt, size int64) (dir *directoryEnd, baseOffset int64, err error)
split
function
#
func split(name string) (dir string, elem string, isDir bool)
stat
method
#
func (f *fileListEntry) stat() (fileInfoDirEntry, error)
sub
method
#
func (b *readBuf) sub(n int) readBuf
timeToMsDosTime
function
#
timeToMsDosTime converts a time.Time to an MS-DOS date and time.
The resolution is 2s.
See: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-filetimetodosdatetime
func timeToMsDosTime(t time.Time) (fDate uint16, fTime uint16)
timeZone
function
#
timeZone returns a *time.Location based on the provided offset.
If the offset is non-sensible, then this uses an offset of zero.
func timeZone(offset time.Duration) *time.Location
toValidName
function
#
toValidName coerces name to be a valid name for fs.FS.Open.
func toValidName(name string) string
uint16
method
#
func (b *readBuf) uint16() uint16
uint16
method
#
func (b *writeBuf) uint16(v uint16)
uint32
method
#
func (b *readBuf) uint32() uint32
uint32
method
#
func (b *writeBuf) uint32(v uint32)
uint64
method
#
func (b *readBuf) uint64() uint64
uint64
method
#
func (b *writeBuf) uint64(v uint64)
uint8
method
#
func (b *readBuf) uint8() uint8
uint8
method
#
func (b *writeBuf) uint8(v uint8)
unixModeToFileMode
function
#
func unixModeToFileMode(m uint32) fs.FileMode
writeDataDescriptor
method
#
func (w *fileWriter) writeDataDescriptor() error