zip
5 分钟阅读
zip
https://pkg.go.dev/archive/zip@go1.20.1
Package zip provides support for reading and writing ZIP archives.
See the ZIP specification for details.
This package does not support disk spanning.
A note about ZIP64:
To be backwards compatible the FileHeader has both 32 and 64 bit Size fields. The 64 bit fields will always contain the correct value and for normal archives both fields will be the same. For files requiring the ZIP64 format the 32 bit fields will be 0xffffffff and the 64 bit fields must be used instead.
常量
|
|
Compression methods.
变量
|
|
函数
func RegisterCompressor <- go1.2
|
|
RegisterCompressor registers custom compressors for a specified method ID. The common methods Store and Deflate are built in.
func RegisterDecompressor <- go1.2
|
|
RegisterDecompressor allows custom decompressors for a specified method ID. The common methods Store and Deflate are built in.
类型
type Compressor <- go1.2
|
|
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.
type Decompressor <- go1.2
|
|
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.
type File
|
|
A File is a single file in a ZIP archive. The file information is in the embedded FileHeader. The file content can be accessed by calling Open.
(*File) DataOffset <- go1.2
|
|
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.
(*File) Open
|
|
Open returns a ReadCloser that provides access to the File’s contents. Multiple files may be read concurrently.
(*File) OpenRaw <- go1.17
|
|
OpenRaw returns a Reader that provides access to the File’s contents without decompression.
type FileHeader
|
|
FileHeader describes a file within a ZIP file. See the ZIP specification for details.
func FileInfoHeader
|
|
FileInfoHeader creates a partially-populated FileHeader from an fs.FileInfo. Because fs.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.
(*FileHeader) FileInfo
|
|
FileInfo returns an fs.FileInfo for the FileHeader.
Example
|
|
(*FileHeader) Mode
|
|
Mode returns the permission and mode bits for the FileHeader.
Example
|
|
(*FileHeader) SetMode
|
|
SetMode changes the permission and mode bits for the FileHeader.
type ReadCloser
|
|
A ReadCloser is a Reader that must be closed when no longer needed.
func OpenReader
|
|
OpenReader will open the Zip file specified by name and return a ReadCloser.
(*ReadCloser) Close
|
|
Close closes the Zip file, rendering it unusable for I/O.
type Reader
|
|
A Reader serves content from a ZIP archive.
Example
|
|
func NewReader
|
|
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.
(*Reader) Open <- go1.16
|
|
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.
(*Reader) RegisterDecompressor <- go1.6
|
|
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.
type Writer
|
|
Writer implements a zip file writer.
Example
|
|
func NewWriter
|
|
NewWriter returns a new Writer writing a zip file to w.
(*Writer) Close
|
|
Close finishes writing the zip file by writing the central directory. It does not close the underlying writer.
(*Writer) Copy <- go1.17
|
|
Copy copies the file f (obtained from a Reader) into w. It copies the raw form directly bypassing decompression, compression, and validation.
(*Writer) Create
|
|
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.
(*Writer) CreateHeader
|
|
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, CreateRaw, or Close.
(*Writer) CreateRaw <- go1.17
|
|
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 Create, CreateHeader, CreateRaw, or Close.
In contrast to CreateHeader, the bytes passed to Writer are not compressed.
(*Writer) Flush <- go1.4
|
|
Flush flushes any buffered data to the underlying writer. Calling Flush is not normally necessary; calling Close is sufficient.
(*Writer) RegisterCompressor <- go1.6
|
|
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.
Example
|
|
(*Writer) SetComment <- go1.10
|
|
SetComment sets the end-of-central-directory comment field. It can only be called before Close.
(*Writer) SetOffset <- go1.5
|
|
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.