Zip 支援

此 Spring Integration 模組提供 Zip (解)壓縮支援。壓縮演算法實作基於 ZeroTurnaround ZIP 程式庫。提供以下元件

您需要將此依賴項包含到您的專案中

  • Maven

  • Gradle

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

命名空間支援

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>

(Un)Zip 轉換器

ZipTransformer 實作了以下輸入 payload 類型的壓縮功能:FileStringbyte[]Iterable。輸入資料類型可以混合作為 Iterable 的一部分。例如,應該可以輕鬆壓縮包含字串、位元組陣列和檔案的集合。重要的是要注意,目前不支援巢狀 Iterables。

可以透過設定多個屬性來自訂 ZipTransformer

  • compressionLevel - 設定壓縮等級。預設值為 Deflater#DEFAULT_COMPRESSION

  • useFileAttributes - 指定是否應將檔案名稱用於 zip 條目。

例如,若要將簡單的 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 支援以下輸入 payloadFilebyte[]InputStream。解壓縮資料時,可以指定 expectSingleResult 屬性。如果設定為 true 且偵測到超過 1 個 zip 條目,則會引發 MessagingException。此屬性也會影響 payload 的傳回類型。如果設定為 false (預設值),則 payload 的類型將為 SortedMap,如果為 true,則會傳回實際的 zip 條目。

可以在 UnZipTransformer 上設定的其他屬性

  • deleteFiles - 如果 payload 是 File 的實例,則此屬性指定是否在轉換後刪除檔案。預設值為 false

  • ZipResultType - 定義轉換後傳回的資料格式。可用選項為:Filebyte[]

  • workDirectory - 當 ZipResultType 設定為 ZipResultType.FILE 時,會使用工作目錄。預設情況下,此屬性設定為包含子目錄 ziptransformer 的系統暫存目錄。

例如,若要將簡單的 test.zip 檔案解壓縮成其條目的 map,只需此設定就已足夠

  • 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 分割器

在 zip 檔案包含超過 1 個條目的情況下,UnZipResultSplitter 非常有用。基本上,它必須用作上述 UnZipTransformer 之後的整合流程中的下一步。它僅支援 Map 作為輸入資料,並將每個條目與 FileHeaders.FILENAMEZipHeaders.ZIP_ENTRY_PATH 標頭發送到 outputChannel

以下範例示範用於分割解壓縮結果的簡單設定

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