Class ZipBuilder
java.lang.Object
cloud.opencode.base.io.compress.ZipBuilder
Fluent Builder for Creating Zip Archives
用于创建Zip归档的流式构建器
Provides a fluent API to assemble a Zip archive from files, directories, byte arrays, and strings, then write it to a target path.
提供流式API从文件、目录、字节数组和字符串组装Zip归档,然后写入目标路径。
Features | 主要功能:
- Add files with optional custom entry names - 添加文件并可自定义条目名
- Add directories recursively - 递归添加目录
- Add raw bytes and strings - 添加原始字节和字符串
- Configurable compression level and comment - 可配置压缩级别和注释
Usage Examples | 使用示例:
ZipUtil.builder()
.addFile(path1)
.addFile(path2, "custom/name.txt")
.addDirectory(dirPath)
.addString("readme.txt", "Hello World")
.compressionLevel(6)
.comment("My archive")
.writeTo(outputPath);
Security | 安全性:
- Thread-safe: No (not intended for concurrent use) - 线程安全: 否(不用于并发使用)
- Since:
- JDK 25, opencode-base-io V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionAdds raw bytes as an entry 添加原始字节作为条目addDirectory(Path dir) Adds a directory recursively 递归添加目录Adds a file using its file name as entry name 使用文件名作为条目名添加文件Adds a file with a custom entry name 使用自定义条目名添加文件Adds a string as an entry using UTF-8 encoding 使用UTF-8编码添加字符串作为条目Adds a string as an entry with the specified charset 使用指定字符集添加字符串作为条目Sets the archive comment 设置归档注释compressionLevel(int level) Sets the compression level (0-9) 设置压缩级别(0-9)voidWrites the assembled archive to the target path 将组装好的归档写入目标路径
-
Method Details
-
addFile
Adds a file using its file name as entry name 使用文件名作为条目名添加文件- Parameters:
file- the file to add | 要添加的文件- Returns:
- this builder | 此构建器
- Throws:
NullPointerException- if file is null | 当file为null时抛出
-
addFile
Adds a file with a custom entry name 使用自定义条目名添加文件- Parameters:
file- the file to add | 要添加的文件entryName- the entry name in the archive | 归档中的条目名- Returns:
- this builder | 此构建器
- Throws:
NullPointerException- if file or entryName is null | 当file或entryName为null时抛出
-
addDirectory
Adds a directory recursively 递归添加目录- Parameters:
dir- the directory to add | 要添加的目录- Returns:
- this builder | 此构建器
- Throws:
NullPointerException- if dir is null | 当dir为null时抛出
-
addBytes
Adds raw bytes as an entry 添加原始字节作为条目- Parameters:
entryName- the entry name | 条目名data- the byte data | 字节数据- Returns:
- this builder | 此构建器
- Throws:
NullPointerException- if entryName or data is null | 当entryName或data为null时抛出
-
addString
Adds a string as an entry using UTF-8 encoding 使用UTF-8编码添加字符串作为条目- Parameters:
entryName- the entry name | 条目名content- the string content | 字符串内容- Returns:
- this builder | 此构建器
- Throws:
NullPointerException- if entryName or content is null | 当entryName或content为null时抛出
-
addString
Adds a string as an entry with the specified charset 使用指定字符集添加字符串作为条目- Parameters:
entryName- the entry name | 条目名content- the string content | 字符串内容charset- the charset for encoding | 编码用的字符集- Returns:
- this builder | 此构建器
- Throws:
NullPointerException- if any argument is null | 当任何参数为null时抛出
-
comment
Sets the archive comment 设置归档注释- Parameters:
comment- the archive comment | 归档注释- Returns:
- this builder | 此构建器
-
compressionLevel
Sets the compression level (0-9) 设置压缩级别(0-9)- Parameters:
level- the compression level (0 = no compression, 9 = best compression) | 压缩级别(0=不压缩,9=最佳压缩)- Returns:
- this builder | 此构建器
- Throws:
IllegalArgumentException- if level is out of range | 当级别超出范围时抛出
-
writeTo
Writes the assembled archive to the target path 将组装好的归档写入目标路径- Parameters:
target- the output file path | 输出文件路径- Throws:
NullPointerException- if target is null | 当target为null时抛出OpenIOOperationException- if writing fails | 当写入失败时抛出
-