...
Run Format

Package zip

import "archive/zip"
Overview
Index
Examples

Overview ▾

Package zip provides support for reading and writing ZIP archives.

See: https://www.pkware.com/documents/casestudies/APPNOTE.TXT

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.

Constants

Compression methods.

const (
        Store   uint16 = 0
        Deflate uint16 = 8
)

Variables

var (
        ErrFormat    = errors.New("zip: not a valid zip file")
        ErrAlgorithm = errors.New("zip: unsupported compression algorithm")
        ErrChecksum  = errors.New("zip: checksum error")
)

func RegisterCompressor

RegisterCompressor registers custom compressors for a specified method ID. The common methods Store and Deflate are built in.

func RegisterCompressor(method uint16, comp Compressor)

func RegisterDecompressor

RegisterDecompressor allows custom decompressors for a specified method ID. The common methods Store and Deflate are built in.

func RegisterDecompressor(method uint16, dcomp Decompressor)

type Compressor

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 Compressor func(w io.Writer) (io.WriteCloser, error)

type Decompressor

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 Decompressor func(r io.Reader) io.ReadCloser

type File

type File struct {
        FileHeader
        // contains filtered or unexported fields
}

func (*File) DataOffset

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.

func (f *File) DataOffset() (offset int64, err error)

func (*File) Open

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)

type FileHeader

FileHeader describes a file within a zip file. See the zip spec for details.

type FileHeader struct {
        // Name is the name of the file.
        // It 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.
        Name string

        CreatorVersion     uint16
        ReaderVersion      uint16
        Flags              uint16
        Method             uint16
        ModifiedTime       uint16 // MS-DOS time
        ModifiedDate       uint16 // MS-DOS date
        CRC32              uint32
        CompressedSize     uint32 // Deprecated: Use CompressedSize64 instead.
        UncompressedSize   uint32 // Deprecated: Use UncompressedSize64 instead.
        CompressedSize64   uint64
        UncompressedSize64 uint64
        Extra              []byte
        ExternalAttrs      uint32 // Meaning depends on CreatorVersion
        Comment            string
}

func FileInfoHeader

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.

func FileInfoHeader(fi os.FileInfo) (*FileHeader, error)

func (*FileHeader) FileInfo

FileInfo returns an os.FileInfo for the FileHeader.

func (h *FileHeader) FileInfo() os.FileInfo

func (*FileHeader) ModTime

ModTime returns the modification time in UTC. The resolution is 2s.

func (h *FileHeader) ModTime() time.Time

func (*FileHeader) Mode

Mode returns the permission and mode bits for the FileHeader.

func (h *FileHeader) Mode() (mode os.FileMode)

func (*FileHeader) SetModTime

SetModTime sets the ModifiedTime and ModifiedDate fields to the given time in UTC. The resolution is 2s.

func (h *FileHeader) SetModTime(t time.Time)

func (*FileHeader) SetMode

SetMode changes the permission and mode bits for the FileHeader.

func (h *FileHeader) SetMode(mode os.FileMode)

type ReadCloser

type ReadCloser struct {
        Reader
        // contains filtered or unexported fields
}

func OpenReader

OpenReader will open the Zip file specified by name and return a ReadCloser.

func OpenReader(name string) (*ReadCloser, error)

func (*ReadCloser) Close

Close closes the Zip file, rendering it unusable for I/O.

func (rc *ReadCloser) Close() error

type Reader

type Reader struct {
        File    []*File
        Comment string
        // contains filtered or unexported fields
}

Example

Code:

// 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()
    }
    

Output:

Contents of README:
This is the source code repository for the Go programming language.

func NewReader

NewReader returns a new Reader reading from r, which is assumed to have the given size in bytes.

func NewReader(r io.ReaderAt, size int64) (*Reader, error)

func (*Reader) RegisterDecompressor

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 (z *Reader) RegisterDecompressor(method uint16, dcomp Decompressor)

type Writer

Writer implements a zip file writer.

type Writer struct {
        // contains filtered or unexported fields
}

Example

Code:

    // 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)
    }

func NewWriter

NewWriter returns a new Writer writing a zip file to w.

func NewWriter(w io.Writer) *Writer

func (*Writer) Close

Close finishes writing the zip file by writing the central directory. It does not (and cannot) close the underlying writer.

func (w *Writer) Close() error

func (*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 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. The file's contents must be written to the io.Writer before the next call to Create, CreateHeader, or Close.

func (w *Writer) Create(name string) (io.Writer, error)

func (*Writer) CreateHeader

CreateHeader adds a file to the zip file using the provided FileHeader for the file metadata. It 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. The provided FileHeader fh must not be modified after a call to CreateHeader.

func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error)

func (*Writer) Flush

Flush flushes any buffered data to the underlying writer. Calling Flush is not normally necessary; calling Close is sufficient.

func (w *Writer) Flush() error

func (*Writer) RegisterCompressor

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)

Example

func (*Writer) SetOffset

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)