package zip
import (
)
var (
errLongName = errors.New("zip: FileHeader.Name too long")
errLongExtra = errors.New("zip: FileHeader.Extra too long")
)
type Writer struct {
cw *countWriter
dir []*header
last *fileWriter
closed bool
compressors map[uint16]Compressor
comment string
testHookCloseSizeOffset func(size, offset uint64)
}
type header struct {
*FileHeader
offset uint64
raw bool
}
func ( io.Writer) *Writer {
return &Writer{cw: &countWriter{w: bufio.NewWriter()}}
}
func ( *Writer) ( int64) {
if .cw.count != 0 {
panic("zip: SetOffset called after data was written")
}
.cw.count =
}
func ( *Writer) () error {
return .cw.w.(*bufio.Writer).Flush()
}
func ( *Writer) ( string) error {
if len() > uint16max {
return errors.New("zip: Writer.Comment too long")
}
.comment =
return nil
}
func ( *Writer) () error {
if .last != nil && !.last.closed {
if := .last.close(); != nil {
return
}
.last = nil
}
if .closed {
return errors.New("zip: writer closed twice")
}
.closed = true
:= .cw.count
:= false
for , := range .dir {
:= .ReaderVersion
if .CompressedSize64 >= uint32max || .UncompressedSize64 >= uint32max || .offset >= uint32max {
= true
= max(, zipVersion45)
var uint16
var [28]byte
:= writeBuf([:])
.uint16(zip64ExtraID)
.uint16(0)
if .UncompressedSize64 >= uint32max {
.uint64(.UncompressedSize64)
+= 8
}
if .CompressedSize64 >= uint32max {
.uint64(.CompressedSize64)
+= 8
}
if .offset >= uint32max {
.uint64(.offset)
+= 8
}
:= writeBuf([2:])
.uint16()
.Extra = append(.Extra, [:4+]...)
}
var [directoryHeaderLen]byte
:= writeBuf([:])
.uint32(uint32(directoryHeaderSignature))
.uint16(.CreatorVersion)
.uint16()
.uint16(.Flags)
.uint16(.Method)
.uint16(.ModifiedTime)
.uint16(.ModifiedDate)
.uint32(.CRC32)
.uint32(uint32(min(.CompressedSize64, uint32max)))
.uint32(uint32(min(.UncompressedSize64, uint32max)))
.uint16(uint16(len(.Name)))
.uint16(uint16(len(.Extra)))
.uint16(uint16(len(.Comment)))
= [4:]
.uint32(.ExternalAttrs)
.uint32(uint32(min(.offset, uint32max)))
if , := .cw.Write([:]); != nil {
return
}
if , := io.WriteString(.cw, .Name); != nil {
return
}
if , := .cw.Write(.Extra); != nil {
return
}
if , := io.WriteString(.cw, .Comment); != nil {
return
}
}
:= .cw.count
:= uint64(len(.dir))
:= uint64( - )
:= uint64()
if := .testHookCloseSizeOffset; != nil {
(, )
}
if || >= uint16max || >= uint32max || >= uint32max {
var [directory64EndLen + directory64LocLen]byte
:= writeBuf([:])
.uint32(directory64EndSignature)
.uint64(directory64EndLen - 12)
.uint16(zipVersion45)
.uint16(zipVersion45)
.uint32(0)
.uint32(0)
.uint64()
.uint64()
.uint64()
.uint64()
.uint32(directory64LocSignature)
.uint32(0)
.uint64(uint64())
.uint32(1)
if , := .cw.Write([:]); != nil {
return
}
}
var [directoryEndLen]byte
:= writeBuf([:])
.uint32(uint32(directoryEndSignature))
= [4:]
.uint16(uint16(min(uint16max, )))
.uint16(uint16(min(uint16max, )))
.uint32(uint32(min(uint32max, )))
.uint32(uint32(min(uint32max, )))
.uint16(uint16(len(.comment)))
if , := .cw.Write([:]); != nil {
return
}
if , := io.WriteString(.cw, .comment); != nil {
return
}
return .cw.w.(*bufio.Writer).Flush()
}
func ( *Writer) ( string) (io.Writer, error) {
:= &FileHeader{
Name: ,
Method: Deflate,
}
return .CreateHeader()
}
func ( string) (, bool) {
for := 0; < len(); {
, := utf8.DecodeRuneInString([:])
+=
if < 0x20 || > 0x7d || == 0x5c {
if !utf8.ValidRune() || ( == utf8.RuneError && == 1) {
return false, false
}
= true
}
}
return true,
}
func ( *Writer) ( *FileHeader) error {
if .last != nil && !.last.closed {
if := .last.close(); != nil {
return
}
}
if len(.dir) > 0 && .dir[len(.dir)-1].FileHeader == {
return errors.New("archive/zip: invalid duplicate FileHeader")
}
return nil
}
func ( *Writer) ( *FileHeader) (io.Writer, error) {
if := .prepare(); != nil {
return nil,
}
, := detectUTF8(.Name)
, := detectUTF8(.Comment)
switch {
case .NonUTF8:
.Flags &^= 0x800
case ( || ) && ( && ):
.Flags |= 0x800
}
.CreatorVersion = .CreatorVersion&0xff00 | zipVersion20
.ReaderVersion = zipVersion20
if !.Modified.IsZero() {
.ModifiedDate, .ModifiedTime = timeToMsDosTime(.Modified)
var [9]byte
:= uint32(.Modified.Unix())
:= writeBuf([:])
.uint16(extTimeExtraID)
.uint16(5)
.uint8(1)
.uint32()
.Extra = append(.Extra, [:]...)
}
var (
io.Writer
*fileWriter
)
:= &header{
FileHeader: ,
offset: uint64(.cw.count),
}
if strings.HasSuffix(.Name, "/") {
.Method = Store
.Flags &^= 0x8
.CompressedSize = 0
.CompressedSize64 = 0
.UncompressedSize = 0
.UncompressedSize64 = 0
= dirWriter{}
} else {
.Flags |= 0x8
= &fileWriter{
zipw: .cw,
compCount: &countWriter{w: .cw},
crc32: crc32.NewIEEE(),
}
:= .compressor(.Method)
if == nil {
return nil, ErrAlgorithm
}
var error
.comp, = (.compCount)
if != nil {
return nil,
}
.rawCount = &countWriter{w: .comp}
.header =
=
}
.dir = append(.dir, )
if := writeHeader(.cw, ); != nil {
return nil,
}
.last =
return , nil
}
func ( io.Writer, *header) error {
const = 1<<16 - 1
if len(.Name) > {
return errLongName
}
if len(.Extra) > {
return errLongExtra
}
var []byte
:= .ReaderVersion
:= .raw && !.hasDataDescriptor()
if && (.CompressedSize64 > uint32max || .UncompressedSize64 > uint32max) {
= max(, zipVersion45)
= make([]byte, 20)
:= writeBuf()
.uint16(zip64ExtraID)
.uint16(16)
.uint64(.UncompressedSize64)
.uint64(.CompressedSize64)
}
var [fileHeaderLen]byte
:= writeBuf([:])
.uint32(uint32(fileHeaderSignature))
.uint16()
.uint16(.Flags)
.uint16(.Method)
.uint16(.ModifiedTime)
.uint16(.ModifiedDate)
if {
.uint32(.CRC32)
if != nil {
.uint32(uint32max)
.uint32(uint32max)
} else {
.uint32(uint32(.CompressedSize64))
.uint32(uint32(.UncompressedSize64))
}
} else {
.uint32(0)
.uint32(0)
.uint32(0)
}
.uint16(uint16(len(.Name)))
.uint16(uint16(len(.Extra) + len()))
if , := .Write([:]); != nil {
return
}
if , := io.WriteString(, .Name); != nil {
return
}
if , := .Write(.Extra); != nil {
return
}
if , := .Write(); != nil {
return
}
return nil
}
func ( *Writer) ( *FileHeader) (io.Writer, error) {
if := .prepare(); != nil {
return nil,
}
.CompressedSize = uint32(min(.CompressedSize64, uint32max))
.UncompressedSize = uint32(min(.UncompressedSize64, uint32max))
:= &header{
FileHeader: ,
offset: uint64(.cw.count),
raw: true,
}
.dir = append(.dir, )
if := writeHeader(.cw, ); != nil {
return nil,
}
if strings.HasSuffix(.Name, "/") {
.last = nil
return dirWriter{}, nil
}
:= &fileWriter{
header: ,
zipw: .cw,
}
.last =
return , nil
}
func ( *Writer) ( *File) error {
, := .OpenRaw()
if != nil {
return
}
:= .FileHeader
, := .CreateRaw(&)
if != nil {
return
}
_, = io.Copy(, )
return
}
func ( *Writer) ( uint16, Compressor) {
if .compressors == nil {
.compressors = make(map[uint16]Compressor)
}
.compressors[] =
}
func ( *Writer) ( fs.FS) error {
return fs.WalkDir(, ".", func( string, fs.DirEntry, error) error {
if != nil {
return
}
if == "." {
return nil
}
, := .Info()
if != nil {
return
}
if !.IsDir() && !.Mode().IsRegular() {
return errors.New("zip: cannot add non-regular file")
}
, := FileInfoHeader()
if != nil {
return
}
.Name =
if .IsDir() {
.Name += "/"
}
.Method = Deflate
, := .CreateHeader()
if != nil {
return
}
if .IsDir() {
return nil
}
, := .Open()
if != nil {
return
}
defer .Close()
_, = io.Copy(, )
return
})
}
func ( *Writer) ( uint16) Compressor {
:= .compressors[]
if == nil {
= compressor()
}
return
}
type dirWriter struct{}
func (dirWriter) ( []byte) (int, error) {
if len() == 0 {
return 0, nil
}
return 0, errors.New("zip: write to directory")
}
type fileWriter struct {
*header
zipw io.Writer
rawCount *countWriter
comp io.WriteCloser
compCount *countWriter
crc32 hash.Hash32
closed bool
}
func ( *fileWriter) ( []byte) (int, error) {
if .closed {
return 0, errors.New("zip: write to closed file")
}
if .raw {
return .zipw.Write()
}
.crc32.Write()
return .rawCount.Write()
}
func ( *fileWriter) () error {
if .closed {
return errors.New("zip: file closed twice")
}
.closed = true
if .raw {
return .writeDataDescriptor()
}
if := .comp.Close(); != nil {
return
}
:= .header.FileHeader
.CRC32 = .crc32.Sum32()
.CompressedSize64 = uint64(.compCount.count)
.UncompressedSize64 = uint64(.rawCount.count)
if .CompressedSize64 > uint32max || .UncompressedSize64 > uint32max {
.CompressedSize = uint32max
.UncompressedSize = uint32max
.ReaderVersion = zipVersion45
} else {
.CompressedSize = uint32(.CompressedSize64)
.UncompressedSize = uint32(.UncompressedSize64)
}
return .writeDataDescriptor()
}
func ( *fileWriter) () error {
if !.hasDataDescriptor() {
return nil
}
var []byte
if .CompressedSize64 > uint32max || .UncompressedSize64 > uint32max {
= make([]byte, dataDescriptor64Len)
} else {
= make([]byte, dataDescriptorLen)
}
:= writeBuf()
.uint32(dataDescriptorSignature)
.uint32(.CRC32)
if .CompressedSize64 > uint32max || .UncompressedSize64 > uint32max {
.uint64(.CompressedSize64)
.uint64(.UncompressedSize64)
} else {
.uint32(.CompressedSize)
.uint32(.UncompressedSize)
}
, := .zipw.Write()
return
}
type countWriter struct {
w io.Writer
count int64
}
func ( *countWriter) ( []byte) (int, error) {
, := .w.Write()
.count += int64()
return ,
}
type nopCloser struct {
io.Writer
}
func ( nopCloser) () error {
return nil
}
type writeBuf []byte
func ( *writeBuf) ( uint8) {
(*)[0] =
* = (*)[1:]
}
func ( *writeBuf) ( uint16) {
binary.LittleEndian.PutUint16(*, )
* = (*)[2:]
}
func ( *writeBuf) ( uint32) {
binary.LittleEndian.PutUint32(*, )
* = (*)[4:]
}
func ( *writeBuf) ( uint64) {
binary.LittleEndian.PutUint64(*, )
* = (*)[8:]
}