如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

ZIP 支持

此 Spring Integration 模块提供了 Zip (解)压缩支持。 Zipping 算法的实现基于 ZeroTurnaround ZIP 库。 提供的组件如下:spring-doc.cadn.net.cn

您需要将以下依赖项包含到您的项目中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-zip</artifactId>
    <version>6.4.10</version>
</dependency>
compile "org.springframework.integration:spring-integration-zip:6.4.10"

命名空间支持

Spring Integration Zip 模块内的所有组件都提供命名空间支持。 要启用命名空间支持,您需要导入 Spring Integration Zip 模块的架构。 以下示例展示了一个典型配置:spring-doc.cadn.net.cn

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

(Un)Zip 转换器

The ZipTransformer 实现了针对这些类型的输入 payload 的拉链功能:FileStringbyte[]Iterable。 输入数据类型可以在 Iterable 中混合使用。 例如,压缩包含字符串、字节数组和文件的集合应该是很容易的。 需要注意的是,嵌套的 Iterables 目前不支持spring-doc.cadn.net.cn

ZipTransformer 可以通过设置多个属性来自定义:spring-doc.cadn.net.cn

  • compressionLevel - 设置压缩级别。 默认值为 Deflater#DEFAULT_COMPRESSIONspring-doc.cadn.net.cn

  • useFileAttributes - 指定是否将文件名用作 zip 条目。spring-doc.cadn.net.cn

  • fileNameGenerator - 用于根据请求消息生成原始文件名。 默认为 DefaultFileNameGenerator。 目标 zip 文件名的扩展名 .zip 会被添加到该名称中, 除非该扩展名已作为此生成器的结果存在。spring-doc.cadn.net.cn

此外,可以为 zip 条目的名称及其 lastmodified 属性提供 ZipHeaders.ZIP_ENTRY_FILE_NAMEZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE。 如果未提供,则条目名称为 fileNameGenerator 的精确结果,且 lastmodified 回退到当前日期和时间。 如果请求消息的有效载荷是 Iterable,则该条目名称将使用从 1 开始的索引进行修改。spring-doc.cadn.net.cn

例如,要将一个简单的 test.txt 文件压缩到 test.txt.zip 中,只需以下配置即可:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow zipFlow() {
    return IntegrationFlow
             .from("zipChannel")
             .transform(new ZipTransformer())
             .get();
}
@Bean
fun zipFlow() =
    integrationFlow("zipChannel") {
        transform(ZipTransformer())
    }
@Bean
zipFlow() {
    integrationFlow 'zipChannel',
            {
                transform new ZipTransformer()
            }
}
@Transformer(inputChannel = "zipChannel")
@Bean
ZipTransformer zipTransformer() {
    return new ZipTransformer();
}
<int-zip:zip-transformer input-channel="zipChannel"/>

查看 ZipTransformer 个 Javadocs 以获取更多信息。spring-doc.cadn.net.cn

一个UnZipTransformer支持这些输入payloadFilebyte[]InputStream。 在解压数据时,可以指定一个expectSingleResult属性。 如果设置为true且检测到超过1个zip条目,将抛出MessagingException异常。 此属性也会影响负载的返回类型。 如果设置为false(默认值),则负载类型为SortedMap;但如果设置为true,则将返回实际的zip条目。spring-doc.cadn.net.cn

其他可设置在 UnZipTransformer 上的属性:spring-doc.cadn.net.cn

  • deleteFiles - 如果负载是 File 的实例,此属性指定是否在转换后删除文件。 默认值为 falsespring-doc.cadn.net.cn

  • ZipResultType - 定义转换后返回的数据格式。 可用选项为:File, byte[]spring-doc.cadn.net.cn

  • workDirectory - 当将 ZipResultType 设置为 ZipResultType.FILE 时,会使用工作目录。 默认情况下,此属性被设置为包含子目录 ziptransformer 的系统临时目录。spring-doc.cadn.net.cn

例如,要将一个简单的 test.zip 文件压缩并映射为其条目,只需以下配置即可:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow unzipFlow() {
    return IntegrationFlow
             .from("unzipChannel")
             .transform(new UnZipTransformer())
             .get();
}
@Bean
fun unzipFlow() =
    integrationFlow("unzipChannel") {
        transform(UnZipTransformer())
    }
@Bean
unzipFlow() {
    integrationFlow 'unzipChannel',
            {
                transform new UnZipTransformer()
            }
}
@Transformer(inputChannel = "unzipChannel")
@Bean
UnZipTransformer unzipTransformer() {
    return new UnZipTransformer();
}
<int-zip:unzip-transformer input-channel="unzipChannel"/>

解压分割器

UnZipResultSplitter在zip文件包含多于1个条目时非常有用。 本质上,它必须作为上述UnZipTransformer之后集成流程中的下一步来使用。 它仅支持将Map作为输入数据,并将每个条目输出到带有FileHeaders.FILENAMEZipHeaders.ZIP_ENTRY_PATH头的outputChannel中。spring-doc.cadn.net.cn

以下示例演示了拆分解压结果的简单配置:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow unzipSplitFlow(Executor executor) {
    return IntegrationFlow
             .from("unzipChannel")
             .transform(new UnZipTransformer())
             .split(new UnZipResultSplitter())
             .channel(c -> c.executor("entriesChannel", executor))
             .get();
}
@Bean
fun unzipFlow(executor: Executor) =
    integrationFlow("unzipChannel") {
        transform(UnZipTransformer())
        split(UnZipResultSplitter())
        channel { executor("entriesChannel", executor) }
    }
@Bean
unzipFlow(Executor executor) {
    integrationFlow 'unzipChannel',
            {
                transformWith {
                    ref new UnZipTransformer()
                }
                splitWith {
                    ref new UnZipResultSplitter()
                }
                channel { executor 'entriesChannel', executor }
            }
}
@Transformer(inputChannel = "unzipChannel", outputChannel = "splitChannel")
@Bean
UnZipTransformer unzipTransformer() {
    return new UnZipTransformer();
}

@Spitter(inputChannel = "splitChannel", outputChannel = "entriesChannel")
@Bean
UnZipResultSplitter unZipSplitter() {
    return new UnZipResultSplitter();
}

@Bean
ExecutorChannel entriesChannel(Executor executor) {
    return new ExecutorChannel(executor);
}
<int:chain input-channel="unzipChannel" output-channel="entriesChannel">
    <int-zip:unzip-transformer/>
    <int:splitter>
        <bean class="org.springframework.integration.zip.splitter.UnZipResultSplitter"/>
    </int:splitter>
</int:chain>

<int:channel id="entriesChannel">
    <int:dispatcher task-executor="executor"/>
</int:channel>