Zip Support
此 Spring 集成模块提供 Zip (解)压缩支持。压缩算法实现基于 ZeroTurnaround ZIP Library。提供以下组件:
This Spring Integration module provides Zip (un-)compression support. A zipping algorithm implementation is based on a ZeroTurnaround ZIP Library. The following components are provided:
你需要将此依赖项包含在你的项目中:
You need to include this dependency into your project:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-zip</artifactId>
<version>{project-version}</version>
</dependency>
compile "org.springframework.integration:spring-integration-zip:{project-version}"
Namespace Support
Spring 集成 Zip 模块内的所有组件都提供命名空间支持。为了启用命名空间支持,您需要导入 Spring 集成 Zip 模块的模式。以下示例展示了一个典型的设置:
All components within the Spring Integration Zip module provide namespace support. In order to enable namespace support, you need to import the schema for the Spring Integration Zip Module. The following example shows a typical setup:
<?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 Transformer
ZipTransformer
为下列类型的输入 payload
实现压缩功能:File
、String
、byte[]
和 Iterable
。输入数据类型可以混合为 Iterable
的一部分。例如,轻松压缩包含字符串、字节数组和文件的集合应而易举。需要注意的是,嵌套的 Iterables 在目前 NOT SUPPORTED。
The ZipTransformer
implements a zipping functionality for these types of input payload
: File
, String
, byte[]
and Iterable
.
In input data types can be mixed as part of an Iterable
.
For example, it should be easily to compress a collection containing Strings, byte arrays and Files.
It is important to note that nested Iterables are NOT SUPPORTED at present time.
可以通过设置以下几个属性来定制 ZipTransformer
:
The ZipTransformer
can be customized by setting several properties:
-
compressionLevel
- sets the compression level. Default isDeflater#DEFAULT_COMPRESSION
. -
useFileAttributes
- specifies whether the name of the file shall be used for the zip entry.
例如,要将一个简单的 test.txt
文件压缩到 test.txt.zip
中,只需要以下配置:
For example to zip a simple test.txt
file into a test.txt.zip
, only this configuration is enough:
-
Java DSL
-
Kotlin DSL
-
Groovy DSL
-
Java
-
XML
@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
Javadoc。
See ZipTransformer
Javadocs for more information.
UnZipTransformer
支持这些输入 payload
:File
、byte[]
和 InputStream
。解压数据时,可以指定 expectSingleResult
属性。如果将其设置为 true
并检测到超过 1
个 zip 条目,则会引发 MessagingException
。此属性还会影响有效负载的返回类型。如果将其设置为 false
(默认值),则有效负载的类型将为 SortedMap
;然而,如果是 true
,则将返回实际的 zip 条目。
An UnZipTransformer
supports these of input payload
: File
, byte[]
and InputStream
.
When unzipping data, an expectSingleResult
property can be specified.
If set to true
and more than 1
zip entry were detected, a MessagingException
will be raised.
This property also influences the return type of the payload.
If set to false
(default), then the payload will be of type SortedMap
, if true
, however, the actual zip entry will be returned.
可以在 UnZipTransformer
上设置的其他属性:
Other properties that can be set on the UnZipTransformer
:
-
deleteFiles
- if the payload is an instance ofFile
, this property specifies whether to delete the File after transformation. Default isfalse
. -
ZipResultType
- defines the format of the data returned after transformation. Available options are:File
,byte[]
. -
workDirectory
- the work directory is used when aZipResultType
is set toZipResultType.FILE
. By default, this property is set to the System temporary directory containing a subdirectoryziptransformer
.
例如,要将一个简单的 test.zip
文件解压到其条目的映射中,只需要以下配置:
For example to zip a simple test.zip
file into a map of its entries, only this configuration is enough:
-
Java DSL
-
Kotlin DSL
-
Groovy DSL
-
Java
-
XML
@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"/>
Unzipped Splitter
当 zip 文件包含多于 1
个条目时,UnZipResultSplitter
很实用。从本质上来说,它必须用作上述 UnZipTransformer
之后的集成流中的下一步。它仅支持将 Map
作为输入数据,并将每个条目以 FileHeaders.FILENAME
和 ZipHeaders.ZIP_ENTRY_PATH
标头发送到 outputChannel
中。
The UnZipResultSplitter
is useful in cases where zip files contain more than 1
entry.
Essentially it has to be used as the next step in the integration flow after the mentioned above UnZipTransformer
.
It supports only a Map
as an input data and emits every entry into an outputChannel
with FileHeaders.FILENAME
and ZipHeaders.ZIP_ENTRY_PATH
headers.
以下示例演示了一个用于分割解压结果的简单配置:
The following example demonstrates a simple configuration for splitting unzipped result:
-
Java DSL
-
Kotlin DSL
-
Groovy DSL
-
Java
-
XML
@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>