hutool.core.util.zip¶
Module Contents¶
Classes¶
压缩工具类 |
API¶
- class hutool.core.util.zip.ZipUtil¶
压缩工具类
- static zip(src_path: str, dest_path: Optional[str] = None, charset: str = 'utf-8') str¶
压缩文件或目录为zip
- 参数:
src_path – 源文件或目录路径
dest_path – 目标zip文件路径,默认为源路径加.zip后缀
charset – 文件名编码字符集,默认utf-8
- 返回:
生成的zip文件路径
- static unzip(zip_file: str, dest_path: str, charset: str = 'utf-8') str¶
解压zip文件
- 参数:
zip_file – zip文件路径
dest_path – 解压目标目录
charset – 文件名编码字符集,默认utf-8
- 返回:
解压目标目录路径
- static gzip(data: bytes) bytes¶
Gzip压缩
- 参数:
data – 待压缩的字节数据
- 返回:
压缩后的字节数据
- static gzip_str(data: str, charset: str = 'utf-8') bytes¶
字符串Gzip压缩
- 参数:
data – 待压缩的字符串
charset – 字符编码,默认utf-8
- 返回:
压缩后的字节数据
- static ungzip(data: bytes) bytes¶
Gzip解压
- 参数:
data – 待解压的字节数据
- 返回:
解压后的字节数据
- static ungzip_str(data: bytes, charset: str = 'utf-8') str¶
Gzip解压为字符串
- 参数:
data – 待解压的字节数据
charset – 字符编码,默认utf-8
- 返回:
解压后的字符串
- static zlib_compress(data: bytes, level: int = -1) bytes¶
Zlib压缩
- 参数:
data – 待压缩的字节数据
level – 压缩级别,-1为默认,0为不压缩,1-9为压缩级别(1最快,9最小)
- 返回:
压缩后的字节数据
- static zlib_uncompress(data: bytes) bytes¶
Zlib解压
- 参数:
data – 待解压的字节数据
- 返回:
解压后的字节数据
- static zip_to_stream(src_path: str) bytes¶
将文件或目录压缩为 zip 格式的字节流。
- 参数:
src_path – 源文件或目录路径
- 返回:
zip 格式字节数据
- static zip_entries(src_path: str, dest_path: str) List[str]¶
将文件或目录压缩为 zip 并返回压缩的文件名列表。
- 参数:
src_path – 源文件或目录路径
dest_path – 目标 zip 文件路径
- 返回:
压缩的文件名列表
- static append(zip_file: str, file_path: str, arcname: Optional[str] = None) str¶
向已有 zip 文件追加文件。
- 参数:
zip_file – 已有 zip 文件路径
file_path – 要追加的文件路径
arcname – zip 内的文件名,默认使用原文件名
- 返回:
zip 文件路径
- static unzip_stream(zip_data: bytes, dest_path: str) str¶
将 zip 字节流解压到指定目录。
- 参数:
zip_data – zip 格式的字节数据
dest_path – 解压目标目录
- 返回:
解压目录路径
- static get(zip_file: str, entry_name: str) Optional[zipfile.ZipInfo]¶
从 zip 文件中获取指定条目的信息。
- 参数:
zip_file – zip 文件路径
entry_name – 条目名称
- 返回:
ZipInfo 对象,未找到返回 None
- static read(zip_file: str, entry_name: str) bytes¶
从 zip 文件中读取指定条目的内容。
- 参数:
zip_file – zip 文件路径
entry_name – 条目名称
- 返回:
条目内容字节数据
- 抛出:
KeyError – 条目不存在时
- static unzip_file_bytes(zip_file: str, entry_name: str) bytes¶
从 zip 文件中读取指定条目的字节数据(同 read)。
- 参数:
zip_file – zip 文件路径
entry_name – 条目名称
- 返回:
条目内容字节数据
- static list_file_names(zip_file: str) List[str]¶
列出 zip 文件中的所有文件名。
- 参数:
zip_file – zip 文件路径
- 返回:
文件名列表
- static to_zip_file(src_path: str, dest_path: Optional[str] = None) str¶
将文件或目录转换为 zip 文件。
与
zip方法类似,但默认目标路径为源路径加.zip后缀。- 参数:
src_path – 源文件或目录路径
dest_path – 目标 zip 文件路径,默认为源路径加 .zip 后缀
- 返回:
生成的 zip 文件路径
- 返回类型:
str
- static get_zip_output_stream(file_path: str) zipfile.ZipFile¶
获取 zip 写入流
返回一个可写入的
ZipFile对象,调用者负责关闭。- 参数:
file_path – zip 文件路径
- 返回:
可写入的 ZipFile 对象
- 返回类型:
zipfile.ZipFile
- static get_zip_stream(file_path: str) zipfile.ZipFile¶
获取 zip 读取流
返回一个可读取的
ZipFile对象,调用者负责关闭。- 参数:
file_path – zip 文件路径
- 返回:
可读取的 ZipFile 对象
- 返回类型:
zipfile.ZipFile