mod: golang.org/x/mod/zip Index | Files

package zip

import "golang.org/x/mod/zip"

Package zip provides functions for creating and extracting module zip files.

Module zip files have several restrictions listed below. These are necessary to ensure that module zip files can be extracted consistently on supported platforms and file systems.

• All file paths within a zip file must start with "<module>@<version>/", where "<module>" is the module path and "<version>" is the version. The module path must be valid (see golang.org/x/mod/module.CheckPath). The version must be valid and canonical (see golang.org/x/mod/module.CanonicalVersion). The path must have a major version suffix consistent with the version (see golang.org/x/mod/module.Check). The part of the file path after the "<module>@<version>/" prefix must be valid (see golang.org/x/mod/module.CheckFilePath).

• No two file paths may be equal under Unicode case-folding (see strings.EqualFold).

• A go.mod file may or may not appear in the top-level directory. If present, it must be named "go.mod", not any other case. Files named "go.mod" are not allowed in any other directory.

• The total size in bytes of a module zip file may be at most MaxZipFile bytes (500 MiB). The total uncompressed size of the files within the zip may also be at most MaxZipFile bytes.

• Each file's uncompressed size must match its declared 64-bit uncompressed size in the zip file header.

• If the zip contains files named "<module>@<version>/go.mod" or "<module>@<version>/LICENSE", their sizes in bytes may be at most MaxGoMod or MaxLICENSE, respectively (both are 16 MiB).

• Empty directories are ignored. File permissions and timestamps are also ignored.

• Symbolic links and other irregular files are not allowed.

Note that this package does not provide hashing functionality. See golang.org/x/mod/sumdb/dirhash.

Index

Package Files

zip.go

Constants

const (
    // MaxZipFile is the maximum size in bytes of a module zip file. The
    // go command will report an error if either the zip file or its extracted
    // content is larger than this.
    MaxZipFile = 500 << 20

    // MaxGoMod is the maximum size in bytes of a go.mod file within a
    // module zip file.
    MaxGoMod = 16 << 20

    // MaxLICENSE is the maximum size in bytes of a LICENSE file within a
    // module zip file.
    MaxLICENSE = 16 << 20
)

func Create Uses

func Create(w io.Writer, m module.Version, files []File) (err error)

Create builds a zip archive for module m from an abstract list of files and writes it to w.

Create verifies the restrictions described in the package documentation and should not produce an archive that Unzip cannot extract. Create does not include files in the output archive if they don't belong in the module zip. In particular, Create will not include files in modules found in subdirectories, most files in vendor directories, or irregular files (such as symbolic links) in the output archive.

func CreateFromDir Uses

func CreateFromDir(w io.Writer, m module.Version, dir string) (err error)

CreateFromDir creates a module zip file for module m from the contents of a directory, dir. The zip content is written to w.

CreateFromDir verifies the restrictions described in the package documentation and should not produce an archive that Unzip cannot extract. CreateFromDir does not include files in the output archive if they don't belong in the module zip. In particular, CreateFromDir will not include files in modules found in subdirectories, most files in vendor directories, or irregular files (such as symbolic links) in the output archive. Additionally, unlike Create, CreateFromDir will not include directories named ".bzr", ".git", ".hg", or ".svn".

func Unzip Uses

func Unzip(dir string, m module.Version, zipFile string) (err error)

Unzip extracts the contents of a module zip file to a directory.

Unzip checks all restrictions listed in the package documentation and returns an error if the zip archive is not valid. In some cases, files may be written to dir before an error is returned (for example, if a file's uncompressed size does not match its declared size).

dir may or may not exist: Unzip will create it and any missing parent directories if it doesn't exist. If dir exists, it must be empty.

type File Uses

type File interface {
    // Path returns a clean slash-separated relative path from the module root
    // directory to the file.
    Path() string

    // Lstat returns information about the file. If the file is a symbolic link,
    // Lstat returns information about the link itself, not the file it points to.
    Lstat() (os.FileInfo, error)

    // Open provides access to the data within a regular file. Open may return
    // an error if called on a directory or symbolic link.
    Open() (io.ReadCloser, error)
}

File provides an abstraction for a file in a directory, zip, or anything else that looks like a file.

Package zip imports 12 packages (graph). Updated 2019-11-20. Refresh now. Tools for package owners.