# package zip
`import "archive/zip"`
zip包提供了zip档案文件的读写服务。参见[http://www.pkware.com/documents/casestudies/APPNOTE.TXT](http://www.pkware.com/documents/casestudies/APPNOTE.TXT)
本包不支持跨硬盘的压缩。
关于ZIP64:
为了向下兼容,FileHeader同时拥有32位和64位的Size字段。64位字段总是包含正确的值,对普通格式的档案未见它们的值是相同的。对zip64格式的档案文件32位字段将是0xffffffff,必须使用64位字段。
## Index
* [Constants](#pkg-constants)
* [Variables](#pkg-variables)
* [type Compressor](#Compressor)
* [type Decompressor](#Decompressor)
* [func RegisterCompressor(method uint16, comp Compressor)](#RegisterCompressor)
* [func RegisterDecompressor(method uint16, d Decompressor)](#RegisterDecompressor)
* [type FileHeader](#FileHeader)
* [func FileInfoHeader(fi os.FileInfo) (\*FileHeader, error)](#FileInfoHeader)
* [func (h \*FileHeader) FileInfo() os.FileInfo](#FileHeader.FileInfo)
* [func (h \*FileHeader) Mode() (mode os.FileMode)](#FileHeader.Mode)
* [func (h \*FileHeader) SetMode(mode os.FileMode)](#FileHeader.SetMode)
* [func (h \*FileHeader) ModTime() time.Time](#FileHeader.ModTime)
* [func (h \*FileHeader) SetModTime(t time.Time)](#FileHeader.SetModTime)
* [type File](#File)
* [func (f \*File) DataOffset() (offset int64, err error)](#File.DataOffset)
* [func (f \*File) Open() (rc io.ReadCloser, err error)](#File.Open)
* [type Reader](#Reader)
* [func NewReader(r io.ReaderAt, size int64) (\*Reader, error)](#NewReader)
* [type ReadCloser](#ReadCloser)
* [func OpenReader(name string) (\*ReadCloser, error)](#OpenReader)
* [func (rc \*ReadCloser) Close() error](#ReadCloser.Close)
* [type Writer](#Writer)
* [func NewWriter(w io.Writer) \*Writer](#NewWriter)
* [func (w \*Writer) CreateHeader(fh \*FileHeader) (io.Writer, error)](#Writer.CreateHeader)
* [func (w \*Writer) Create(name string) (io.Writer, error)](#Writer.Create)
* [func (w \*Writer) Close() error](#Writer.Close)
### Examples
* [Reader](#example-Reader)
* [Writer](#example-Writer)
## Constants
```
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")
)
```
## type [Compressor](https://github.com/golang/go/blob/master/src/archive/zip/register.go#L17 "View Source")
```
type Compressor func(io.Writer) (io.WriteCloser, error)
```
Compressor函数类型会返回一个io.WriteCloser,该接口会将数据压缩后写入提供的接口。关闭时,应将缓冲中的数据刷新到下层接口中。
## type [Decompressor](https://github.com/golang/go/blob/master/src/archive/zip/register.go#L23 "View Source")
```
type Decompressor func(io.Reader) io.ReadCloser
```
Decompressor函数类型会返回一个io.ReadCloser, 该接口的Read方法会将读取自提供的接口的数据提前解压缩。程序员有责任在读取结束时关闭该io.ReadCloser。
## func [RegisterCompressor](https://github.com/golang/go/blob/master/src/archive/zip/register.go#L90 "View Source")
```
func RegisterCompressor(method uint16, comp Compressor)
```
RegisterCompressor使用指定的方法ID注册一个Compressor类型函数。常用的方法Store和Deflate是内建的。
## func [RegisterDecompressor](https://github.com/golang/go/blob/master/src/archive/zip/register.go#L78 "View Source")
```
func RegisterDecompressor(method uint16, d Decompressor)
```
RegisterDecompressor使用指定的方法ID注册一个Decompressor类型函数。
## type [FileHeader](https://github.com/golang/go/blob/master/src/archive/zip/struct.go#L70 "View Source")
```
type FileHeader struct {
// Name是文件名,它必须是相对路径,不能以设备或斜杠开始,只接受'/'作为路径分隔符
Name string
CreatorVersion uint16
ReaderVersion uint16
Flags uint16
Method uint16
ModifiedTime uint16 // MS-DOS时间
ModifiedDate uint16 // MS-DOS日期
CRC32 uint32
CompressedSize uint32 // 已弃用;请使用CompressedSize64
UncompressedSize uint32 // 已弃用;请使用UncompressedSize64
CompressedSize64 uint64
UncompressedSize64 uint64
Extra []byte
ExternalAttrs uint32 // 其含义依赖于CreatorVersion
Comment string
}
```
FileHeader描述zip文件中的一个文件。参见zip的定义获取细节。
### func [FileInfoHeader](https://github.com/golang/go/blob/master/src/archive/zip/struct.go#L120 "View Source")
```
func FileInfoHeader(fi os.FileInfo) (*FileHeader, error)
```
FileInfoHeader返回一个根据fi填写了部分字段的Header。因为os.FileInfo接口的Name方法只返回它描述的文件的无路径名,有可能需要将返回值的Name字段修改为文件的完整路径名。
### func (\*FileHeader) [FileInfo](https://github.com/golang/go/blob/master/src/archive/zip/struct.go#L94 "View Source")
```
func (h *FileHeader) FileInfo() os.FileInfo
```
FileInfo返回一个根据h的信息生成的os.FileInfo。
### func (\*FileHeader) [Mode](https://github.com/golang/go/blob/master/src/archive/zip/struct.go#L209 "View Source")
```
func (h *FileHeader) Mode() (mode os.FileMode)
```
Mode返回h的权限和模式位。
### func (\*FileHeader) [SetMode](https://github.com/golang/go/blob/master/src/archive/zip/struct.go#L223 "View Source")
```
func (h *FileHeader) SetMode(mode os.FileMode)
```
SetMode修改h的权限和模式位。
### func (\*FileHeader) [ModTime](https://github.com/golang/go/blob/master/src/archive/zip/struct.go#L179 "View Source")
```
func (h *FileHeader) ModTime() time.Time
```
返回最近一次修改的UTC时间。(精度2s)
### func (\*FileHeader) [SetModTime](https://github.com/golang/go/blob/master/src/archive/zip/struct.go#L185 "View Source")
```
func (h *FileHeader) SetModTime(t time.Time)
```
将ModifiedTime和ModifiedDate字段设置为给定的UTC时间。(精度2s)
## type [File](https://github.com/golang/go/blob/master/src/archive/zip/reader.go#L34 "View Source")
```
type File struct {
FileHeader
// 内含隐藏或非导出字段
}
```
### func (\*File) [DataOffset](https://github.com/golang/go/blob/master/src/archive/zip/reader.go#L122 "View Source")
```
func (f *File) DataOffset() (offset int64, err error)
```
DataOffset返回文件的可能存在的压缩数据相对于zip文件起始的偏移量。大多数调用者应使用Open代替,该方法会主动解压缩数据并验证校验和。
### func (\*File) [Open](https://github.com/golang/go/blob/master/src/archive/zip/reader.go#L132 "View Source")
```
func (f *File) Open() (rc io.ReadCloser, err error)
```
Open方法返回一个io.ReadCloser接口,提供读取文件内容的方法。可以同时读取多个文件。
## type [Reader](https://github.com/golang/go/blob/master/src/archive/zip/reader.go#L23 "View Source")
```
type Reader struct {
File []*File
Comment string
// 内含隐藏或非导出字段
}
```
Example
```
// 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](https://github.com/golang/go/blob/master/src/archive/zip/reader.go#L67 "View Source")
```
func NewReader(r io.ReaderAt, size int64) (*Reader, error)
```
NewReader返回一个从r读取数据的\*Reader,r被假设其大小为size字节。
## type [ReadCloser](https://github.com/golang/go/blob/master/src/archive/zip/reader.go#L29 "View Source")
```
type ReadCloser struct {
Reader
// 内含隐藏或非导出字段
}
```
### func [OpenReader](https://github.com/golang/go/blob/master/src/archive/zip/reader.go#L46 "View Source")
```
func OpenReader(name string) (*ReadCloser, error)
```
OpenReader会打开name指定的zip文件并返回一个\*ReadCloser。
### func (\*ReadCloser) [Close](https://github.com/golang/go/blob/master/src/archive/zip/reader.go#L113 "View Source")
```
func (rc *ReadCloser) Close() error
```
Close关闭zip文件,使它不能用于I/O。
## type [Writer](https://github.com/golang/go/blob/master/src/archive/zip/writer.go#L20 "View Source")
```
type Writer struct {
// 内含隐藏或非导出字段
}
```
Writer类型实现了zip文件的写入器。
Example
```
// 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](https://github.com/golang/go/blob/master/src/archive/zip/writer.go#L33 "View Source")
```
func NewWriter(w io.Writer) *Writer
```
NewWriter创建并返回一个将zip文件写入w的\*Writer。
### func (\*Writer) [CreateHeader](https://github.com/golang/go/blob/master/src/archive/zip/writer.go#L183 "View Source")
```
func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error)
```
使用给出的\*FileHeader来作为文件的元数据添加一个文件进zip文件。本方法返回一个io.Writer接口(用于写入新添加文件的内容)。新增文件的内容必须在下一次调用CreateHeader、Create或Close方法之前全部写入。
### func (\*Writer) [Create](https://github.com/golang/go/blob/master/src/archive/zip/writer.go#L170 "View Source")
```
func (w *Writer) Create(name string) (io.Writer, error)
```
使用给出的文件名添加一个文件进zip文件。本方法返回一个io.Writer接口(用于写入新添加文件的内容)。文件名必须是相对路径,不能以设备或斜杠开始,只接受'/'作为路径分隔。新增文件的内容必须在下一次调用CreateHeader、Create或Close方法之前全部写入。
### func (\*Writer) [Close](https://github.com/golang/go/blob/master/src/archive/zip/writer.go#L39 "View Source")
```
func (w *Writer) Close() error
```
Close方法通过写入中央目录关闭该\*Writer。本方法不会也没办法关闭下层的io.Writer接口。
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe