Involved Source Filesreader.goregister.go
Package zip provides support for reading and writing ZIP archives.
See: https://www.pkware.com/appnote
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.
writer.go
Code Examples
package main
import (
"archive/zip"
"fmt"
"io"
"log"
"os"
)
func main() {
// Open a zip archive for reading.
r, err := zip.OpenReader("testdata/readme.zip")
if err != nil {
log.Fatal(err)
}
defer r.Close()
// Iterate through the files in the archive,
// printing some of their contents.
for _, f := range r.File {
fmt.Printf("Contents of %s:\n", f.Name)
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
_, err = io.CopyN(os.Stdout, rc, 68)
if err != nil {
log.Fatal(err)
}
rc.Close()
fmt.Println()
}
}
package main
import (
"archive/zip"
"bytes"
"log"
)
func main() {
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new zip archive.
w := zip.NewWriter(buf)
// Add some files to the archive.
var files = []struct {
Name, Body string
}{
{"readme.txt", "This archive contains some text files."},
{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
{"todo.txt", "Get animal handling licence.\nWrite more examples."},
}
for _, file := range files {
f, err := w.Create(file.Name)
if err != nil {
log.Fatal(err)
}
_, err = f.Write([]byte(file.Body))
if err != nil {
log.Fatal(err)
}
}
// Make sure to check the error on Close.
err := w.Close()
if err != nil {
log.Fatal(err)
}
}
package main
import (
"archive/zip"
"bytes"
"compress/flate"
"io"
)
func main() {
// Override the default Deflate compressor with a higher compression level.
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new zip archive.
w := zip.NewWriter(buf)
// Register a custom Deflate compressor.
w.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(out, flate.BestCompression)
})
// Proceed to add files to w.
}
Package-Level Type Names (total 22, in which 7 are exported)
/* sort exporteds by: | */
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 compressor(method uint16) Compressor
func (*Writer).compressor(method uint16) Compressor
func RegisterCompressor(method uint16, comp Compressor)
func (*Writer).RegisterCompressor(method uint16, comp Compressor)
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 decompressor(method uint16) Decompressor
func (*Reader).decompressor(method uint16) Decompressor
func RegisterDecompressor(method uint16, dcomp Decompressor)
func (*Reader).RegisterDecompressor(method uint16, dcomp Decompressor)
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.
FileHeaderFileHeaderFileHeader.CRC32uint32
Comment is any arbitrary user-defined string shorter than 64KiB.
// Deprecated: Use CompressedSize64 instead.
FileHeader.CompressedSize64uint64FileHeader.CreatorVersionuint16
// Meaning depends on CreatorVersion
FileHeader.Extra[]byteFileHeader.Flagsuint16
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.ReaderVersionuint16
// Deprecated: Use UncompressedSize64 instead.
FileHeader.UncompressedSize64uint64headerOffsetint64zip*Readerziprio.ReaderAtzipsizeint64
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 fs.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.
findBodyOffset does the minimum work to verify the file has a header
and returns the file body offset.
(*T) hasDataDescriptor() bool
isZip64 reports whether the file size exceeds the 32 bit limit
func golang.org/x/pkgsite/internal/licenses.(*Detector).Files(which licenses.WhichFiles) []*File
func golang.org/x/pkgsite/internal/fetch.zipFile(r *Reader, name string) *File
func readDataDescriptor(r io.Reader, f *File) error
func readDirectoryHeader(f *File, r io.Reader) error
func golang.org/x/pkgsite/internal/fetch.loadPackage(ctx context.Context, zipGoFiles []*File, innerPath string, sourceInfo *source.Info, modInfo *godoc.ModuleInfo) (_ *fetch.goPackage, err error)
func golang.org/x/pkgsite/internal/fetch.readZipFile(f *File, limit int64) (_ []byte, err error)
func golang.org/x/pkgsite/internal/licenses.readZipFile(f *File) ([]byte, error)
func golang.org/x/pkgsite/internal/licenses.(*Detector).detectFiles(files []*File) []*licenses.License
FileHeader describes a file within a zip file.
See the zip spec for details.
CRC32uint32
Comment is any arbitrary user-defined string shorter than 64KiB.
// Deprecated: Use CompressedSize64 instead.
CompressedSize64uint64CreatorVersionuint16
// Meaning depends on CreatorVersion
Extra[]byteFlagsuint16
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.
ReaderVersionuint16
// Deprecated: Use UncompressedSize64 instead.
UncompressedSize64uint64
FileInfo returns an fs.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.
isZip64 reports whether the file size exceeds the 32 bit limit
func FileInfoHeader(fi fs.FileInfo) (*FileHeader, error)
func (*Writer).CreateHeader(fh *FileHeader) (io.Writer, error)
func writeHeader(w io.Writer, h *FileHeader) error
Writer implements a zip file writer.
closedboolcommentstringcompressorsmap[uint16]Compressorcw*countWriterdir[]*headerlast*fileWriter
testHookCloseSizeOffset if non-nil is called with the size
of offset of the central directory at Close.
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) compressor(method uint16) Compressor
*T : io.Closer
func NewWriter(w io.Writer) *Writer
func golang.org/x/pkgsite/internal/stdlib.addFiles(z *Writer, r *git.Repository, t *object.Tree, dirpath string, recursive bool) (err error)
func golang.org/x/pkgsite/internal/stdlib.writeZipFile(z *Writer, pathname string, src io.Reader) (err error)
( T) Write(b []byte) (int, error)
T : github.com/go-git/go-git/v5/plumbing/protocol/packp/sideband.Progress
T : github.com/jbenet/go-context/io.Writer
T : io.Writer
Info returns the FileInfo for the file or subdirectory described by the entry.
The returned FileInfo may be from the time of the original directory read
or from the time of the call to Info. If the file has been removed or renamed
since the directory read, Info may return an error satisfying errors.Is(err, ErrNotExist).
If the entry denotes a symbolic link, Info reports the information about the link itself,
not the link's target.
// abbreviation for Mode().IsDir()
// modification time
// file mode bits
// base name of the file
// length in bytes for regular files; system-dependent for others
// underlying data source (can return nil)
Type returns the type bits for the entry.
The type bits are a subset of the usual FileMode bits, those returned by the FileMode.Type method.
*fileListEntryheaderFileInfo
T : io/fs.DirEntry
T : io/fs.FileInfo
header.FileHeader*FileHeaderheader.FileHeader.CRC32uint32
Comment is any arbitrary user-defined string shorter than 64KiB.
// Deprecated: Use CompressedSize64 instead.
header.FileHeader.CompressedSize64uint64header.FileHeader.CreatorVersionuint16
// Meaning depends on CreatorVersion
header.FileHeader.Extra[]byteheader.FileHeader.Flagsuint16
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.
header.FileHeader.ReaderVersionuint16
// Deprecated: Use UncompressedSize64 instead.
header.FileHeader.UncompressedSize64uint64closedboolcompio.WriteClosercompCount*countWritercrc32hash.Hash32header*headerheader.offsetuint64rawCount*countWriterzipwio.Writer
FileInfo returns an fs.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.
(*T) Write(p []byte) (int, error)(*T) close() error
isZip64 reports whether the file size exceeds the 32 bit limit
*T : github.com/go-git/go-git/v5/plumbing/protocol/packp/sideband.Progress
*T : github.com/jbenet/go-context/io.Writer
*T : io.Writer
FileHeader*FileHeaderFileHeader.CRC32uint32
Comment is any arbitrary user-defined string shorter than 64KiB.
// Deprecated: Use CompressedSize64 instead.
FileHeader.CompressedSize64uint64FileHeader.CreatorVersionuint16
// Meaning depends on CreatorVersion
FileHeader.Extra[]byteFileHeader.Flagsuint16
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.ReaderVersionuint16
// Deprecated: Use UncompressedSize64 instead.
FileHeader.UncompressedSize64uint64offsetuint64
FileInfo returns an fs.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.
isZip64 reports whether the file size exceeds the 32 bit limit
Package-Level Functions (total 28, in which 6 are exported)
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.
NewReader returns a new Reader reading from r, which is assumed to
have the given size in bytes.
NewWriter returns a new Writer writing a zip file to w.
OpenReader will open the Zip file specified by name and return a ReadCloser.
RegisterCompressor registers custom compressors for a specified method ID.
The common methods Store and Deflate are built in.
RegisterDecompressor allows custom decompressors for a specified method ID.
The common methods Store and Deflate are built in.
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).
msDosTimeToTime converts an MS-DOS date and time into a time.Time.
The resolution is 2s.
See: https://msdn.microsoft.com/en-us/library/ms724247(v=VS.85).aspx
readDirectoryHeader attempts to read a directory header from r.
It returns io.ErrUnexpectedEOF if it cannot read a complete header,
and ErrFormat if it doesn't find a valid header signature.
timeToMsDosTime converts a time.Time to an MS-DOS date and time.
The resolution is 2s.
See: https://msdn.microsoft.com/en-us/library/ms724274(v=VS.85).aspx
timeZone returns a *time.Location based on the provided offset.
If the offset is non-sensible, then this uses an offset of zero.
toValidName coerces name to be a valid name for fs.FS.Open.
Extra header IDs.
IDs 0..31 are reserved for official use by PKWARE.
IDs above that range are defined by third-party vendors.
Since ZIP lacked high precision timestamps (nor a official specification
of the timezone used for the date fields), many competing extra fields
have been invented. Pervasive use effectively makes them "official".
See http://mdfs.net/Docs/Comp/Archiving/Zip/ExtraField
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project 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.