目录 搜索
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "archive/zip"

  • 概述

  • 索引

  • 例子

概述

Zip 压缩包提供了对读取和写入 ZIP 压缩文件的支持。

请参阅:https://www.pkware.com/appnote

该软件包不支持磁盘跨越。

有关 ZIP64 的说明:

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

索引

Constants

Variables

func RegisterCompressor(method uint16, comp Compressor)

func RegisterDecompressor(method uint16, dcomp Decompressor)

type Compressor

type Decompressor

type File

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

  • func (f *File) Open() (io.ReadCloser, 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)

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

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)

  • func (w *Writer) Flush() error

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

  • func (w *Writer) SetOffset(n int64)

示例

Reader

Writer

Writer.RegisterCompressor

包文件

reader.go register.go struct.go writer.go

常量

压缩方法。

const (
        Store   uint16 = 0
        Deflate uint16 = 8)

变量

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

func RegisterCompressor(method uint16, comp Compressor)

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

func RegisterDecompressor

func RegisterDecompressor(method uint16, dcomp Decompressor)

RegisterDecompressor 允许为指定的方法ID定制解压缩器。内置常用方法 Store 和 Deflate 。

type Compressor

Compressor 返回一个新的压缩作家,写入 w。必须使用WriteCloser 的 Close 方法将未处理的数据刷新到w。Compressor 本身必须可以安全地从多个 goroutine 同时调用,但每个返回的作者一次只能由一个 goroutine 使用。

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

type Decompressor

解压缩器返回一个新的解压缩读取器,从 r 读取。 ReadCloser 的 Close 方法必须用于释放相关资源。解压缩器本身必须可以安全地从多个 goroutine 同时调用,但每个返回的读取器一次只能由一个 goroutine 使用。

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

type File

type File struct {
        FileHeader        // 包含过滤或未导出的字段}

func (*File) DataOffset

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 {        // 名称是文件的名称。        // 它必须是相对路径:它不能以驱动器启动        // 字母(例如C:)或前导斜线,并且只有正斜杠        // 允许。
        Name string

        CreatorVersion     uint16
        ReaderVersion      uint16
        Flags              uint16
        Method             uint16
        ModifiedTime       uint16 // MS-DOS time
        ModifiedDate       uint16 // MS-DOS date
        CRC32              uint32
        CompressedSize     uint32 // 弃用:改用CompressedSize64。
        UncompressedSize   uint32 // 弃用:改用UncompressedSize64。
        CompressedSize64   uint64
        UncompressedSize64 uint64
        Extra              []byte
        ExternalAttrs      uint32 // 含义取决于Creator版本
        Comment            string}

func FileInfoHeader

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

FileInfoHeader 从 os.FileInfo 创建一个部分填充的FileHeader 。因为 os.FileInfo 的 Name 方法仅返回它描述的文件的基本名称,所以可能需要修改返回的头的 Name 字段以提供文件的完整路径名。

func (*FileHeader) FileInfo

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

FileInfo 为 FileHeader 返回一个os.FileInfo。

func (*FileHeader) ModTime

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

ModTime 返回 UTC 的修改时间。分辨率是2s。

func (*FileHeader) Mode

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

模式返回 FileHeader 的权限和模式位。

func (*FileHeader) SetModTime

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

SetModTime 将 ModifiedTime 和 ModifiedDate 字段设置为UTC 中给定的时间。分辨率是2s。

func (*FileHeader) SetMode

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

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

type ReadCloser

type ReadCloser struct {
        Reader        // 包含过滤或未导出的字段}

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        // 包含过滤或未导出的字段}

示例

代码:

// 打开一个zip文件供阅读。r, err := zip.OpenReader("testdata/readme.zip")if err != nil {
        log.Fatal(err)}defer r.Close()// 遍历存档中的文件,// 打印他们的一些内容。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()}

输出:

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

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

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

type Writer

Writer 实现了一个 zip 文件编写器。

type Writer struct {        // 包含过滤或未导出的字段}

示例

代码:

// 创建一个缓冲区来写入我们的存档。buf := new(bytes.Buffer)// 创建一个新的zip存档。w := zip.NewWriter(buf)// 将一些文件添加到存档中。将一些文件添加到存档中。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)        }}// 确保在Close时检查错误。err := w.Close()if err != nil {
        log.Fatal(err)}

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter 返回一个新的 Writer 向w写一个zip文件。

func (*Writer) Close

func (w *Writer) Close() error

关闭通过编写中央目录完成编写 zip 文件。它没有(也不能)关闭潜在的写者。

func (*Writer) Create

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

创建使用提供的名称将文件添加到 zip 文件。它返回一个写入文件内容的 Writer。该名称必须是相对路径:不能以驱动器号(例如C:)或前导斜杠开头,并且只允许正斜杠。在下一次调用 Create,CreateHeader 或 Close之前,必须将文件的内容写入 io.Writer。

func (*Writer) CreateHeader

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

CreateHeader 使用提供的 FileHeader 为文件元数据添加一个文件到 zip 文件。它返回一个写入文件内容的 Writer 。

在下一次调用 Create, CreateHeader 或 Close 之前,必须将文件的内容写入 io.Writer。在调用 CreateHeader 之后,不得修改提供的 FileHeader fh。

func (*Writer) Flush

func (w *Writer) Flush() error

刷新将任何缓冲的数据刷新到底层写入器。调用刷新通常不是必需的;调用 Close 就足够了。

func (*Writer) RegisterCompressor

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

RegisterCompressor 为特定方法 ID 注册或覆盖自定义压缩器。如果没有找到给定方法的压缩器,Writer 将默认在包装级查找压缩器。

示例

package mainimport ("archive/zip""bytes""compress/flate""io")func main() {// 用较高的压缩级别覆盖默认的Deflate压缩器。// 创建一个缓冲区来写入我们的存档。
	buf := new(bytes.Buffer)// 创建一个新的zip存档。
	w := zip.NewWriter(buf)// 注册一个自定义的Deflate压缩机。
	w.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {return flate.NewWriter(out, flate.BestCompression)})// 继续向w添加文件。}

func (*Writer) SetOffset

func (w *Writer) SetOffset(n int64)

SetOffset 设置底层编写器中压缩数据开始的偏移量。应将zip数据附加到现有文件(例如二进制可执行文件)时使用,且必须在写入任何数据之前调用。

上一篇: 下一篇: