gozip

package
v1.4.39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package gozip provides ZIP file compression and decompression utilities.

Index

Constants

View Source
const (
	ZipDefaultCompression = 0  // 默认压缩级别
	ZipBestCompression    = -3 // 最佳压缩(最慢)
	ZipBestSpeed          = -1 // 最快速度(最低压缩率)
	ZipNoCompression      = 0  // 不压缩,仅存储
)

ZipCompressionLevel ZIP压缩级别常量

Variables

This section is empty.

Functions

This section is empty.

Types

type ZipEncryptionType

type ZipEncryptionType int

ZipEncryptionType ZIP加密类型

const (
	// ZipStandardEncryption 标准ZIP加密(兼容性最好,安全性较低)
	ZipStandardEncryption ZipEncryptionType = iota
	// ZipAES128Encryption AES-128加密
	ZipAES128Encryption
	// ZipAES192Encryption AES-192加密
	ZipAES192Encryption
	// ZipAES256Encryption AES-256加密(推荐,最安全)
	ZipAES256Encryption
)

type ZipFileResult

type ZipFileResult struct {
	Path        string // 压缩文件路径
	Size        int64  // 压缩文件大小(字节)
	FileCount   int    // 包含的文件数量
	IsEncrypted bool   // 是否加密
}

ZipFileResult ZIP压缩结果

func ZipDirectory

func ZipDirectory(ctx context.Context, sourceDir string, destPath string, password string) (*ZipFileResult, error)

ZipDirectory 压缩整个目录(递归包含子目录)

参数:

  • sourceDir: 源目录路径
  • destPath: 目标ZIP文件路径(如果为空,自动生成临时文件)
  • password: 压缩密码(可选)

返回:

  • *ZipFileResult: 压缩结果
  • error: 错误信息

使用示例:

result, err := tools.ZipDirectory("/path/to/dir", "", "password123")

func ZipDirectoryWithOptions

func ZipDirectoryWithOptions(ctx context.Context, sourceDir string, destPath string, options *ZipOptions) (*ZipFileResult, error)

ZipDirectoryWithOptions 压缩整个目录(支持自定义选项)

参数:

  • sourceDir: 源目录路径
  • destPath: 目标ZIP文件路径
  • options: 压缩选项

返回:

  • *ZipFileResult: 压缩结果
  • error: 错误信息

func ZipFilesFromPaths

func ZipFilesFromPaths(ctx context.Context, sourceFiles []string, destPath string, password string) (*ZipFileResult, error)

ZipFilesFromPaths 从文件路径列表创建ZIP压缩文件(带密码保护)

参数:

  • sourceFiles: 源文件路径列表
  • destPath: 目标ZIP文件路径(如果为空,自动生成临时文件)
  • password: 压缩密码(可选,为空则不加密)

返回:

  • *ZipFileResult: 压缩结果(包含路径、大小、文件数等)
  • error: 错误信息

使用示例:

files := []string{"/path/to/file1.xlsx", "/path/to/file2.xlsx"}
result, err := tools.ZipFilesFromPaths(files, "", "password123")
if err != nil {
    log.Printf("压缩失败: %v", err)
}
fmt.Printf("压缩文件: %s, 大小: %d bytes\n", result.Path, result.Size)

func ZipFilesFromPathsWithOptions

func ZipFilesFromPathsWithOptions(ctx context.Context, sourceFiles []string, destPath string, options *ZipOptions) (*ZipFileResult, error)

ZipFilesFromPathsWithOptions 从文件路径列表创建ZIP压缩文件(支持自定义选项)

参数:

  • sourceFiles: 源文件路径列表
  • destPath: 目标ZIP文件路径(如果为空,自动生成临时文件)
  • options: 压缩选项(密码、加密类型、压缩级别等)

返回:

  • *ZipFileResult: 压缩结果
  • error: 错误信息

使用示例:

files := []string{"/path/to/file1.xlsx", "/path/to/file2.xlsx"}
options := &tools.ZipOptions{
    Password:   "password123",
    Encryption: tools.ZipAES256Encryption,
    Compression: tools.ZipBestCompression,
}
result, err := tools.ZipFilesFromPathsWithOptions(files, "", options)

type ZipOptions

type ZipOptions struct {
	Password       string            // 密码(可选,为空则不加密)
	Encryption     ZipEncryptionType // 加密类型(默认AES-256)
	Compression    int               // 压缩级别(默认DefaultCompression)
	IncludeBaseDir bool              // 是否包含基础目录(仅对目录压缩有效)
}

ZipOptions ZIP压缩选项

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL