zip - The Go Programming Language

Golang

Package zip

import "archive/zip"
Overview
Index
Examples

Overview ?

Overview ?

Package zip provides support for reading and writing ZIP archives.

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

This package does not support ZIP64 or disk spanning.

Index

Constants
Variables
type File
    func (f *File) Open() (rc io.ReadCloser, err error)
type FileHeader
    func FileInfoHeader(fi os.FileInfo) (*FileHeader, error)
    func (h *FileHeader) FileInfo() os.FileInfo
    func (h *FileHeader) ModTime() time.Time
    func (h *FileHeader) Mode() (mode os.FileMode)
    func (h *FileHeader) SetModTime(t time.Time)
    func (h *FileHeader) SetMode(mode os.FileMode)
type ReadCloser
    func OpenReader(name string) (*ReadCloser, error)
    func (rc *ReadCloser) Close() error
type Reader
    func NewReader(r io.ReaderAt, size int64) (*Reader, error)
type Writer
    func NewWriter(w io.Writer) *Writer
    func (w *Writer) Close() error
    func (w *Writer) Create(name string) (io.Writer, error)
    func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error)

Examples

Reader
Writer

Package files

reader.go struct.go writer.go

Constants

const (
    Store   uint16 = 0
    Deflate uint16 = 8
)

Compression methods.

Variables

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

type File

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

func (*File) Open

func (f *File) Open() (rc io.ReadCloser, err error)

Open returns a ReadCloser that provides access to the File's contents. Multiple files may be read concurrently.

type FileHeader

type FileHeader struct {
    Name             string
    CreatorVersion   uint16
    ReaderVersion    uint16
    Flags            uint16
    Method           uint16
    ModifiedTime     uint16 // MS-DOS time
    ModifiedDate     uint16 // MS-DOS date
    CRC32            uint32
    CompressedSize   uint32
    UncompressedSize uint32
    Extra            []byte
    ExternalAttrs    uint32 // Meaning depends on CreatorVersion
    Comment          string
}

func FileInfoHeader

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

FileInfoHeader creates a partially-populated FileHeader from an os.FileInfo.

func (*FileHeader) FileInfo

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

FileInfo returns an os.FileInfo for the FileHeader.

func (*FileHeader) ModTime

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

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

func (*FileHeader) Mode

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

Mode returns the permission and mode bits for the FileHeader.

func (*FileHeader) SetModTime

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

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

func (*FileHeader) SetMode

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

SetMode changes the permission and mode bits for the FileHeader.

type ReadCloser

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

func OpenReader

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

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

func (*ReadCloser) Close

func (rc *ReadCloser) Close() error

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

type Reader

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

? Example

? 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

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

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

type Writer

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

Writer implements a zip file writer.

? Example

? 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

func NewWriter(w io.Writer) *Writer

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

func (*Writer) Close

func (w *Writer) Close() error

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

func (*Writer) Create

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

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

func (*Writer) CreateHeader

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

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.