骆驼解压 tar.gzip 个文件

camel unpacking tar.gzip files

在 FTP 上用 camel 下载了几个文件后,我应该处理它们,但它们是 tar.gzip 格式。 Camel 支持 gzip,我也可以看到 tar 端点从 2.16.0 开始(http://camel.apache.org/camel-2160-release.html)。

我提取gzip的代码:

from("file:modelFiles?readLock=changed&recursive=true&consumer.delay=1000")
        .unmarshal(new ZipFileDataFormat())
               .choice()
                    .when(body().isNotNull())
                        .log("Uziping file ${file:name}.")
                        .to("file:modelFiles_unzipped")
                    .endChoice()
        .end();

所有文件 运行 通过规则,但它们再次创建为 .tar.gz 但更糟糕的是内容也变得损坏,因此它们甚至无法在之后作为 gzip 存档打开。

问题:

  1. 我应该如何解压缩 gzip 档案?
  2. 我怎么能为 tar 个文件?

更新 1:

感谢 post Jeremie。我按照建议更改了这样的代码:

                from("file:modelFilesSBML2?readLock=changed&recursive=true&consumer.delay=1000")
                    .unmarshal().gzip()
                    .split(new TarSplitter())
                    .to("file:modelFilesSBML_unzipped");

然后我收到以下异常(仅供参考,tar.gzip 文件的长度不是零):FailedException:无法将空正文写入文件:modelFilesSBML_unzipped06-01-31\BioModels_Database-r4-sbml_files.tar.gz :

2016-03-22 14:11:47,950 [ERROR|org.apache.camel.processor.DefaultErrorHandler|MarkerIgnoringBase] Failed delivery for (MessageId: ID-JOY-49807-1458652278822-0-592 on ExchangeId: ID-JOY-49807-1458652278822-0-591). Exhausted after delivery attempt: 1 caught: org.apache.camel.component.file.GenericFileOperationFailedException: Cannot write null body to file: modelFilesSBML_unzipped06-01-31\BioModels_Database-r4-sbml_files.tar.gz

解法:

在尝试了很多方法之后,我最终使用它如下(使用 Camel 2.17.0 它不适用于 2.16.0 或 2.16.1):

from("file:modelFilesSBML?noop=true&recursive=true&consumer.delay=1000" )
    .unmarshal().gzip()
    .split(new TarSplitter())
    .to("log:tar.gzip?level=INFO&showHeaders=true")
           .choice()
                .when(body().isNotNull())
                    .log("### Extracting file: ${file:name}.")
                    .to("file:modelFilesSBML_unzipped?fileName=${in.header.CamelFileRelativePath}_${file:name}")    
            .endChoice()                                
    .end();

使用 Camel 2.17.0,您还可以跳过 body().isNotNull() 检查。

Jeremie 的建议很有帮助,所以我会接受他的回答作为解决方案。尽管如此,如果我不检查消息正文是否为空,异常仍然会出现。 fileName=${in.header.CamelFileRelativePath}_${file:name} 还保留原始文件结构,其中文件名以文件为前缀。tar.gz 但是我没有找到任何其他方法来保留目录结构,因为文件端点不接受 ("file:directory?options...").

中目录的表达式

您可以使用 camel-tarfile 组件。

如果您的 tar.gz 包含多个文件,您应该先解压缩,然后再解压缩 tar 并拆分每个文件的交换。 TarSplitter 是一个表达式,它将 tar 拆分为 tar.

中包含的每个文件的迭代器
from("file:target/from")
    .unmarshal().gzip()
    .split(new TarSplitter())
    .to("file:target/to");