跳到主要内容

压缩支持

QWen Plus 中英对照 Zip Support

此 Spring Integration 模块提供了 Zip (解)压缩支持。一个压缩算法的实现是基于 ZeroTurnaround ZIP 库。以下组件已提供:

你需要将这个依赖添加到你的项目中:

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

命名空间支持

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

<beans xmlns="http://www.springframework.org/schema/beans"
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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/zip http://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
xml

以上配置将允许你在 Spring 配置文件中使用与 Zip 相关的命名空间元素。

<?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>
xml

(解)压缩 Transformer

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

ZipTransformer 可以通过设置几个属性来进行自定义:

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

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

  • fileNameGenerator - 用于根据请求消息生成原始文件名。默认为 DefaultFileNameGenerator。会在这个名称中添加 .zip 扩展名作为目标 zip 文件名。除非它已经由该生成器生成。

此外,可以提供 ZipHeaders.ZIP_ENTRY_FILE_NAMEZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE 用于指定 zip 条目的名称及其 lastmodified 属性。如果不提供,条目名称将是 fileNameGenerator 的确切结果,并且 lastmodified 将回退到当前日期和时间。如果请求消息的有效负载是一个 Iterable,则此条目名称会用从 1 开始的索引进行修改。

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

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

参见 ZipTransformer Javadocs 以获取更多信息。

一个 UnZipTransformer 支持这些类型的输入 payload: File, byte[]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();
}
java

解压分隔器

UnZipResultSplitter 在 zip 文件包含多于 1 个条目的情况下非常有用。它基本上必须作为上述 UnZipTransformer 之后的集成流中的下一步使用。它只支持 Map 作为输入数据,并将每个条目发送到带有 FileHeaders.FILENAMEZipHeaders.ZIP_ENTRY_PATH 标头的 outputChannel

以下示例演示了用于拆分解压结果的简单配置:

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