Zip 支援
這個 Spring Integration 模組提供了 Zip (解)壓縮支援。壓縮演算法的實現基於 ZeroTurnaround ZIP 庫。提供了以下元件:
專案需要此依賴項
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-zip</artifactId>
<version>7.0.0</version>
</dependency>
compile "org.springframework.integration:spring-integration-zip:7.0.0"
名稱空間支援
Spring Integration Zip 模組中的所有元件都提供名稱空間支援。為了啟用名稱空間支援,你需要匯入 Spring Integration 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>
(解)Zip 轉換器
ZipTransformer 為以下型別的輸入 payload 實現了壓縮功能:File、String、byte[] 和 Iterable。輸入資料型別可以作為 Iterable 的一部分進行混合。例如,壓縮包含字串、位元組陣列和檔案的集合應該很容易。需要注意的是,目前**不支援**巢狀的 Iterables。
ZipTransformer 可以透過設定以下幾個屬性進行定製:
-
compressionLevel- 設定壓縮級別。預設為Deflater#DEFAULT_COMPRESSION。 -
useFileAttributes- 指定是否使用檔名作為 zip 條目。 -
fileNameGenerator- 用於根據請求訊息生成原始檔名。預設為DefaultFileNameGenerator。.zip副檔名會新增到此名稱中作為目標 zip 檔名,除非此生成器已經包含它。
此外,可以提供 ZipHeaders.ZIP_ENTRY_FILE_NAME 和 ZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE 來指定 zip 條目的名稱及其 lastmodified 屬性。如果未提供,條目名稱將是 fileNameGenerator 的確切結果,lastmodified 將回退到當前日期和時間。如果請求訊息的 payload 是 Iterable,則此條目名稱將以從 1 開始的索引進行修改。
例如,要將一個簡單的 test.txt 檔案壓縮成 test.txt.zip,只需以下配置即可:
-
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。
UnZipTransformer 支援以下輸入 payload 型別:File、byte[] 和 InputStream。解壓資料時,可以指定 expectSingleResult 屬性。如果設定為 true 且檢測到超過 1 個 zip 條目,則會丟擲 MessagingException。此屬性還會影響 payload 的返回型別。如果設定為 false(預設值),則 payload 將為 SortedMap 型別;如果設定為 true,則返回實際的 zip 條目。
可以在 UnZipTransformer 上設定的其他屬性:
-
deleteFiles- 如果 payload 是File的例項,此屬性指定轉換後是否刪除檔案。預設為false。 -
ZipResultType- 定義轉換後返回資料的格式。可用選項有:File、byte[]。 -
workDirectory- 當ZipResultType設定為ZipResultType.FILE時使用工作目錄。預設情況下,此屬性設定為包含子目錄ziptransformer的系統臨時目錄。
例如,要將一個簡單的 test.zip 檔案解壓縮為條目對映,只需以下配置即可:
-
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"/>
解壓拆分器
當 zip 檔案包含多個條目時,UnZipResultSplitter 會很有用。它本質上必須作為整合流中的下一步使用,在上面提到的 UnZipTransformer 之後。它只支援 Map 作為輸入資料,並將每個條目傳送到 outputChannel,並帶有 FileHeaders.FILENAME 和 ZipHeaders.ZIP_ENTRY_PATH 頭。
以下示例演示了一個用於拆分解壓結果的簡單配置:
-
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>