Unzip a file using Apache Camel UnZippedMessageProcessor

后端 未结 2 833

Trying to unzip a file using Apache Camel, I tried the example given in http://camel.apache.org/zip-file-dataformat.html but I can\'t find UnZippedMessageProcessor

2条回答
  •  不思量自难忘°
    2021-01-21 02:48

    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.

提交回复
热议问题