Package zip

import "archive/zip"
Overview
Index
Examples

Overview ▾

压缩包提供了对读写ZIP归档文件的支持.

See: https://www.pkware.com/appnote

该软件包不支持磁盘扩展.

关于ZIP64的说明:

为了向后兼容,FileHeader具有32位和64位Size字段. 64位字段将始终包含正确的值,对于普通存档,两个字段将相同. 对于需要ZIP64格式的文件,32位字段将为0xffffffff,而必须使用64位字段.

Constants

压缩方法.

const (
    Store   uint16 = 0 // no compression
    Deflate uint16 = 8 // DEFLATE compressed
)

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 1.2

func RegisterCompressor(method uint16, comp Compressor)

RegisterCompressor为指定的方法ID注册自定义压缩器. 内置了常用的Store和Deflate方法.

func RegisterDecompressor 1.2

func RegisterDecompressor(method uint16, dcomp Decompressor)

RegisterDecompressor允许为指定的方法ID自定义解压缩器. 内置了常用的Store和Deflate方法.

type Compressor 1.2

Compressor返回一个新的压缩编写器,写入w. 必须使用WriteCloser的Close方法将待处理数据刷新到w. Compressor本身必须能够安全地同时从多个goroutine调用,但是每个返回的writer一次只能由一个goroutine使用.

type Compressor func(w io.Writer) (io.WriteCloser, error)

type Decompressor 1.2

解压缩器返回一个新的解压缩读取器,从r读取. 必须使用ReadCloser的Close方法来释放关联的资源. 同时从多个goroutine调用Decompressor本身必须安全,但是每个返回的读取器一次只能由一个goroutine使用.

type Decompressor func(r io.Reader) io.ReadCloser

type File

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

func (*File) DataOffset 1.2

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

DataOffset返回文件的可能压缩数据相对于zip文件开头的偏移量.

相反,大多数调用者应该使用Open,它可以透明地解压缩数据并验证校验和.

func (*File) Open

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

Open返回一个ReadCloser,它提供对文件内容的访问. 可以同时读取多个文件.

type FileHeader

FileHeader描述zip文件中的文件. 有关详细信息,请参见zip规范.

type FileHeader struct {
    // 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 ("../../../").
    Name string

    // Comment is any arbitrary user-defined string shorter than 64KiB.
    Comment string

    // 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.
    NonUTF8 bool // Go 1.10

    CreatorVersion uint16
    ReaderVersion  uint16
    Flags          uint16

    // Method is the compression method. If zero, Store is used.
    Method uint16

    // 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.
    Modified     time.Time // Go 1.10
    ModifiedTime uint16 // Deprecated: Legacy MS-DOS date; use Modified instead.
    ModifiedDate uint16 // Deprecated: Legacy MS-DOS time; use Modified instead.

    CRC32              uint32
    CompressedSize     uint32 // Deprecated: Use CompressedSize64 instead.
    UncompressedSize   uint32 // Deprecated: Use UncompressedSize64 instead.
    CompressedSize64   uint64 // Go 1.1
    UncompressedSize64 uint64 // Go 1.1
    Extra              []byte
    ExternalAttrs      uint32 // Meaning depends on CreatorVersion
}

func FileInfoHeader

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

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. If compression is desired, callers should set the FileHeader.Method field; it is unset by default.

func (*FileHeader) FileInfo

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

FileInfo返回FileHeader的os.FileInfo.

func (*FileHeader) ModTime

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

ModTime使用旧版ModifiedDate和ModifiedTime字段以UTC返回修改时间.

不推荐使用:改用Modified.

func (*FileHeader) Mode

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

Mode返回FileHeader的权限和模式位.

func (*FileHeader) SetModTime

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

SetModTime将Modified,ModifiedTime和ModifiedDate字段设置为UTC中的给定时间.

不推荐使用:改用Modified.

func (*FileHeader) SetMode

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

SetMode更改FileHeader的权限和模式位.

type ReadCloser

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

func OpenReader

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

OpenReader将打开按名称指定的Zip文件,并返回ReadCloser.

func (*ReadCloser) Close

func (rc *ReadCloser) Close() error

Close关闭Zip文件,使其无法用于I / O.

type Reader

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

Example

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从r返回一个新的Reader读数,假定具有给定的字节大小.

func (*Reader) RegisterDecompressor 1.6

func (z *Reader) RegisterDecompressor(method uint16, dcomp Decompressor)

RegisterDecompressor为特定的方法ID注册或覆盖自定义解压缩器. 如果找不到给定方法的解压缩器,则Reader将默认在包级别查找该解压缩器.

type Writer

Writer实现一个zip文件writer.

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

Example

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter返回一个新的Writer,将一个zip文件写入w.

func (*Writer) Close

func (w *Writer) Close() error

Close通过写入中央目录来完成zip文件的写入. 它不会关闭基础编写器.

func (*Writer) Create

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

Create使用提供的名称将文件添加到zip文件中. 它返回应该将文件内容写入的Writer. 文件内容将使用Deflate方法压缩. 该名称必须是相对路径:它不能以驱动器号(例如C :)或前斜杠开头,并且只能使用正斜杠. 要创建目录而不是文件,请在名称后添加斜杠. 在下一次调用Create,CreateHeader或Close之前,必须将文件的内容写入io.Writer.

func (*Writer) CreateHeader

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

CreateHeader使用提供的FileHeader作为文件元数据将文件添加到zip存档中. 作家拥有fh的所有权,并可以对其字段进行更改. 调用CreateHeader后,调用者不得修改fh.

这将返回一个Writer,应该将文件内容写入其中. 在下一次调用Create,CreateHeader或Close之前,必须将文件的内容写入io.Writer.

func (*Writer) Flush 1.4

func (w *Writer) Flush() error

刷新将所有缓冲的数据刷新到基础写入器. 通常不需要调用Flush. 调用Close就足够了.

func (*Writer) RegisterCompressor 1.6

func (w *Writer) RegisterCompressor(method uint16, comp Compressor)

RegisterCompressor注册或覆盖用于特定方法ID的自定义压缩器. 如果找不到用于给定方法的压缩器,Writer将默认在包级别查找该压缩器.

Example

func (*Writer) SetComment 1.10

func (w *Writer) SetComment(comment string) error

SetComment设置中央目录末尾的注释字段. 它只能在关闭之前调用.

func (*Writer) SetOffset 1.5

func (w *Writer) SetOffset(n int64)

SetOffset设置基础编写器中zip数据开头的偏移量. 将zip数据附加到现有文件(例如二进制可执行文件)时,应使用该文件. 在写入任何数据之前必须先调用它.

by  ICOPY.SITE