簡體   English   中英

使用Apache Camel UnZippedMessageProcessor解壓縮文件

[英]Unzip a file using Apache Camel UnZippedMessageProcessor

嘗試使用Apache Camel解壓縮文件,我嘗試了http://camel.apache.org/zip-file-dataformat.html中給出的示例,但我找不到UnZippedMessageProcessor類。 這是代碼:

import java.util.Iterator;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.zipfile.ZipFileDataFormat;

public class TestRoute extends RouteBuilder {

@Override
public void configure() throws Exception {

    ZipFileDataFormat zipFile = new ZipFileDataFormat();
    zipFile.setUsingIterator(true);
    from("file:src/test/resources/org/apache/camel/dataformat/zipfile/")
            .unmarshal(zipFile).split(body(Iterator.class)).streaming()
            .process(new UnZippedMessageProcessor()).end();

}
}

有人試圖這樣做或有另一種方法通過Camel路由解壓縮文件?

先感謝您!

你也可以像這樣定義路線,你可以在camel-zipfile中找到ZipSplitter

 from("file:src/test/resources/org/apache/camel/dataformat/zipfile?consumer.delay=1000&noop=true")
  .split(new ZipSplitter())
  .streaming().convertBodyTo(String.class).to("mock:processZipEntry")
  .end()

如果文檔不是那么稀疏,這將更容易弄清楚。 首先,就像其他人提到的那樣,文檔假設您將編寫自己的Processor實現。 一個簡單的看起來像這樣:

public class ZipEntryProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {
        System.out.println(exchange.getIn().getBody().toString());
    }

}

如果調試process方法,您將看到輸入消息的主體是ZipInputStreamWrapper類型,它擴展了Java類BufferedInputStream 這是有用的信息,因為它告訴您可以使用Camel的內置數據轉換,這樣您就不必編寫自己的處理器。

所以這里是你如何使用zip文件並將其所有條目提取到文件系統上的目錄:

from("file:src/test/resources/org/apache/camel/dataformat/zipfile/")
            .split(new ZipSplitter())
                .streaming()
                .to("file://C:/Temp/")
                .end();

這實際上非常簡單。 此外,您還必須確保正確理解文件組件URI語法。 這讓我感到很沮喪。 這是關於該主題的優秀博客文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM