Golang 中的 archive/zip 包用于處理 ZIP 格式的壓縮文件,提供了一系列用于創(chuàng)建、讀取和解壓縮 ZIP 格式文件的函數(shù)和類型,使用起來非常方便。
zip.File 類型
定義如下:文章來源:http://www.zghlxwxcb.cn/news/detail-672420.html
type File struct {
FileHeader
zip *Reader
zipr io.ReaderAt
headerOffset int64 // includes overall ZIP archive baseOffset
zip64 bool // zip64 extended information extra field presence
}
表示一個 ZIP 文件中的單個文件的信息,文件的元數(shù)據(jù)信息,例如文件名、文件大小、修改時間等包含在?FileHeader 中,有兩個重要的方法:
- func (f *File) DataOffset() (offset int64, err error),返回文件的可能存在的壓縮數(shù)據(jù)相對于 zip 文件起始的偏移量。
- func (f *File) Open() (rc io.ReadCloser, err error),返回一個 io.ReadCloser 類型的對象,提供讀取文件內容的方法。
zip.FileHeader 類型
定義如下:
type FileHeader struct {
Name string
Comment string
NonUTF8 bool
CreatorVersion uint16
ReaderVersion uint16
Flags uint16
Method uint16
Modified time.Time
ModifiedTime uint16
ModifiedDate uint16
CRC32 uint32
CompressedSize uint32
UncompressedSize uint32
CompressedSize64 uint64
UncompressedSize64 uint64
Extra []byte
ExternalAttrs uint32 // Meaning depends on CreatorVersion
}
包含了文件在ZIP文件中的元數(shù)據(jù)信息,例如文件名、文件大小、修改時間等。
zip.Writer 類型
定義如下:
type Writer struct {
cw *countWriter
dir []*header
last *fileWriter
closed bool
compressors map[uint16]Compressor
comment string
// testHookCloseSizeOffset if non-nil is called with the size
// of offset of the central directory at Close.
testHookCloseSizeOffset func(size, offset uint64)
}
實現(xiàn)了一個 zip 文件寫入器。
zip.Reader 類型
定義如下:
type Reader struct {
r io.ReaderAt
File []*File
Comment string
decompressors map[uint16]Decompressor
// Some JAR files are zip files with a prefix that is a bash script.
// The baseOffset field is the start of the zip file proper.
baseOffset int64
// fileList is a list of files sorted by ename,
// for use by the Open method.
fileListOnce sync.Once
fileList []fileListEntry
}
用于創(chuàng)建新的 ZIP 文件并將文件添加到其中。
zip.ReadCloser 類型
定義如下:
type ReadCloser struct {
f *os.File
Reader
}
用于讀取文件的內容,并在讀取完成后關閉文件。
zip.Compressor 類型
定義如下:
type Compressor func(w io.Writer) (io.WriteCloser, error)
返回一個用于壓縮用途的 io.WriteCloser 類型的對象。
zip.Decompressor 類型
定義如下:
type Decompressor func(r io.Reader) io.ReadCloser
返回一個用于解壓縮用途的 io.ReadCloser 類型的對象。文章來源地址http://www.zghlxwxcb.cn/news/detail-672420.html
到了這里,關于Golang 中的 archive/zip 包詳解(二):常用類型的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!