跳到主要内容
版本:7.0.2

Zip 支持

DeepSeek V3 中英对照 Zip Support

该 Spring Integration 模块提供 Zip 文件的压缩与解压缩支持。其压缩算法实现基于 ZeroTurnaround ZIP 库。该模块提供以下组件:

此依赖项为项目所需:

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-zip</artifactId>
<version>7.0.2</version>
</dependency>

命名空间支持

Spring Integration Zip 模块中的所有组件都提供了命名空间支持。为了启用命名空间支持,你需要导入 Spring Integration Zip 模块的 schema。以下示例展示了一个典型的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
https://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/zip
https://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
</beans>

(解)压缩转换器

ZipTransformer 为以下类型的输入 payload 实现了压缩功能:FileStringbyte[]Iterable。输入数据类型可以作为 Iterable 的一部分混合使用。例如,应该能够轻松压缩包含字符串、字节数组和文件的集合。需要注意的是,目前不支持嵌套的 Iterable

ZipTransformer 可通过设置以下属性进行自定义:

  • compressionLevel - 设置压缩级别。默认值为 Deflater#DEFAULT_COMPRESSION

  • useFileAttributes - 指定是否将文件名用于 zip 条目。

  • fileNameGenerator - 用于基于请求消息生成原始文件名。默认为 DefaultFileNameGenerator。目标 zip 文件名会在此名称后添加 .zip 扩展名,除非生成器已生成包含该扩展名的结果。

此外,可以为压缩条目名称及其 lastmodified 属性提供 ZipHeaders.ZIP_ENTRY_FILE_NAMEZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE。若未提供,条目名称将直接采用 fileNameGenerator 的生成结果,而 lastmodified 将默认为当前日期和时间。若请求消息的有效载荷是一个 Iterable,则此条目名称会附带从 1 开始的索引进行修改。

例如,要将一个简单的 test.txt 文件压缩为 test.txt.zip,仅需以下配置:

@Bean
public IntegrationFlow zipFlow() {
return IntegrationFlow
.from("zipChannel")
.transform(new ZipTransformer())
.get();
}

有关更多信息,请参阅 ZipTransformer 的 Javadocs。

UnZipTransformer 支持以下类型的输入 payloadFilebyte[]InputStream。在解压数据时,可以指定 expectSingleResult 属性。如果设置为 true 且检测到超过 1 个 zip 条目,则会引发 MessagingException。此属性还会影响 payload 的返回类型。如果设置为 false(默认值),则 payload 的类型为 SortedMap;如果设置为 true,则会返回实际的 zip 条目。

可在 UnZipTransformer 上设置的其他属性:

  • deleteFiles - 如果载荷是 File 的实例,此属性指定转换后是否删除该文件。默认值为 false

  • ZipResultType - 定义转换后返回数据的格式。可用选项为:Filebyte[]

  • workDirectory - 当 ZipResultType 设置为 ZipResultType.FILE 时使用的工作目录。默认情况下,此属性设置为系统临时目录下的 ziptransformer 子目录。

例如,要将一个简单的 test.zip 文件压缩为其条目映射,仅需以下配置:

@Bean
public IntegrationFlow unzipFlow() {
return IntegrationFlow
.from("unzipChannel")
.transform(new UnZipTransformer())
.get();
}

解压缩分割器

UnZipResultSplitter 在 zip 文件包含超过 1 个条目时非常有用。本质上,它必须作为集成流程中紧随上述 UnZipTransformer 之后的下一步使用。它仅支持 Map 作为输入数据,并将每个条目发送到 outputChannel,同时携带 FileHeaders.FILENAMEZipHeaders.ZIP_ENTRY_PATH 头部信息。

以下示例展示了拆分未压缩结果的简单配置:

@Bean
public IntegrationFlow unzipSplitFlow(Executor executor) {
return IntegrationFlow
.from("unzipChannel")
.transform(new UnZipTransformer())
.split(new UnZipResultSplitter())
.channel(c -> c.executor("entriesChannel", executor))
.get();
}