Unzip a file using Apache Camel UnZippedMessageProcessor

◇◆丶佛笑我妖孽 提交于 2019-12-01 21:09:59

You can also define the route like this, you can find the ZipSplitter inside of camel-zipfile.

 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()

This would be a lot easier to figure out if the documentation wasn't so sparse. First, like someone else mentioned, the docs assume that you'll write your own Processor implementation. A simple one looks like this:

public class ZipEntryProcessor implements Processor {

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

}

If you debug the process method, you'll see that the body of the input message is of type ZipInputStreamWrapper, which extends the Java class BufferedInputStream. That's useful information because it tells you that you could probably use Camel's built-in data transformations so that you don't have to write your own processor.

So here's how you consume a zip file and extract all of its entries to a directory on the file system:

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

It's actually ridiculously simple. Also, you have to make sure you understand the file component URI syntax properly too. That tripped me up the most. Here's an excellent blog post about that topic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!