// SiYuan - https://github.com/siyuan-note/siyuan // Copyright (c) 2020-present, b3log.org // SPDX-License-Identifier: AGPL-3.0-or-later package util import ( "archive/zip" "fmt" "io" "os" "path/filepath" ) const ( maxArchiveEntries = 65536 maxArchiveEntryBytes = 512 << 20 maxArchiveTotalBytes = 2 << 30 ) func CheckArchiveExpansion(entries []*zip.File) error { if len(entries) > maxArchiveEntries { return fmt.Errorf("archive exceeds %d entries", maxArchiveEntries) } var total uint64 for _, entry := range entries { size := entry.UncompressedSize64 if size > maxArchiveEntryBytes || size > maxArchiveTotalBytes-total { return fmt.Errorf("archive exceeds extraction size limit") } total += size } return nil } // ExtractArchiveEntry 把 zip 条目解出到 destination,目录条目只创建目录。 // 文件条目按 0755 建立父目录并把修改时间对齐到条目时间;调用方负责路径越界校验。 func ExtractArchiveEntry(entry *zip.File, destination string) error { if entry.FileInfo().IsDir() { return os.MkdirAll(destination, 0755) } if err := os.MkdirAll(filepath.Dir(destination), 0755); err != nil { return err } source, err := entry.Open() if err != nil { return err } defer source.Close() target, err := os.Create(destination) if err != nil { return err } size, copyErr := io.Copy(target, io.LimitReader(source, int64(entry.UncompressedSize64)+1)) closeErr := target.Close() if copyErr != nil { return copyErr } if size != int64(entry.UncompressedSize64) { return zip.ErrFormat } if closeErr != nil { return closeErr } return os.Chtimes(destination, entry.Modified, entry.Modified) }