【问题标题】:Unzip a file using Apache Camel UnZippedMessageProcessor使用 Apache Camel UnZippedMessageProcessor 解压缩文件
【发布时间】:2013-07-24 05:54:01
【问题描述】:

尝试使用 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依赖吗? (在链接底部描述)
  • 是的,我有。我已经检查了 camel-zipfile jar,但我无法在其中或任何其他 jar 中找到该类。
  • UnZippedMessageProcessor需要自己写,我猜这只是一个示例名称。
  • 我没有使用 zipSplitter,而是以与您相同的方式进行操作,只是我只是给出了我希望看到的目标目录而不是 .process(new UnZippedMessageProcessor())解压文件 - .to("file:/destDirForUnzippedFiles/")

标签: java apache apache-camel


【解决方案1】:

你也可以这样定义路由,你可以在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()

【讨论】:

  • 这并没有将文件解压缩到目录中。但我怀疑 Camel 的 ZipFile 数据格式是否可以直接将文件解压缩到目录中。我们应该通过流来创建目录和其中的文件。
  • 不,ZipSplitter 不会将文件解压缩到目录中。它只是帮助骆驼消费压缩文件的输入流。
【解决方案2】:

如果文档不是那么稀疏,这将更容易弄清楚。首先,就像其他人提到的那样,文档假设您将编写自己的 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 语法。那是最让我绊倒的。这是关于该主题的出色blog post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多