简体   繁体   English

在名为zip-file的文件夹中使用骆驼解压缩文件

[英]Unzip file using camel in folder named like zip-file

I'd like to unzip a zip-file to a folder called like the zip-file. 我想将zip文件解压缩到名为zip文件的文件夹中。 Eg original/abc.zip should be unzipped to import/abc/ 例如,Original / abc.zip应该解压缩为import / abc /

What I have now does unzip the files in the specified folder. 我现在所拥有的确实解压缩了指定文件夹中的文件。

from("file:" + "</path/to/original>" + "?noop=true").noAutoStartup().routeId("xxx").split(new ZipSplitter()).streaming()
            .convertBodyTo(String.class).to("file:" +  "</path/to/import>");

How can I get the filename out of the "from" file and put it into the "to" section? 如何从“来自”文件中获取文件名并将其放入“至”部分?

I'm new with Camel, so any help would be apprechiated. 我是骆驼的新手,因此不胜感激。 Thanks! 谢谢!

This route extracts the contexts of a zip file from an inbox folder to an outbox folder, in a parent folder where the parent name is the name of the zip file extracted. 此路由将zip文件的上下文从收件箱文件夹提取到发件箱文件夹,该文件夹位于父文件夹中,其中父名称是提取的zip文件的名称。 I think it covers all your requirements. 我认为它可以满足您的所有要求。

  fromF("file:%s?noop=true","inbox")
     .split(new ZipSplitter())
     .streaming()
     .toD("file:{{my.outbox}}/${file:onlyname.noext}/");

Some notes: 一些注意事项:

  1. String concatenation in the endpoints doesn't look good and it is error prone. 端点中的字符串连接看起来不太好,并且容易出错。 For the consumer I used fromF, and as a second parameter I give the inbox folder.You can take it from a constant value. 对于使用者,我使用fromF,并作为第二个参数提供收件箱文件夹。您可以从恒定值中获取它。 Directory must not contain dynamic expressions with ${ } placeholders. 目录不得包含带有$ {}占位符的动态表达式。
  2. You don't need .convertBodyTo(String.class) 您不需要.convertBodyTo(String.class)
  3. For the producer I used toD which takes the outbox folder from a property. 对于生产者,我使用了toD,它从属性中获取发件箱文件夹。 See details here https://camel.apache.org/properties.html .You can add a properties file in the resources folder(src/main/resources for a maven project) and load them in camel like this (in a java dsl routebuilder) 在此处查看详细信息https://camel.apache.org/properties.html 。您可以在资源文件夹中添加属性文件(maven项目的src / main / resources),然后像这样(在Java DSL中)将它们加载到骆驼中routebuilder)

    PropertiesComponent pc = new PropertiesComponent(); pc.setLocation("classpath:application.properties"); getContext().addComponent("properties PropertiesComponent pc = new PropertiesComponent(); pc.setLocation("classpath:application.properties"); getContext().addComponent("properties ", pc) PropertiesComponent pc = new PropertiesComponent(); pc.setLocation("classpath:application.properties"); getContext().addComponent("properties ”,pc)

  4. And finally the most important, you have to take advantage of the file language and use file:onlyname:noext. 最后,最重要的是,您必须利用文件语言并使用file:onlyname:noext。 to get the original zip file name without the extension. 获取没有扩展名的原始zip文件名。 In your case abc. 在你的情况下abc。 Details here: https://camel.apache.org/file-language.html 此处的详细信息: https : //camel.apache.org/file-language.html

For copying the file path from the producer to consumer the file language can be used. 为了将文件路径从生产者复制到消费者,可以使用文件语言

Something like 就像是

from("file:" + "</path/to/original>" + "?noop=true").noAutoStartup().routeId("xxx").split(new ZipSplitter()).streaming()
            .convertBodyTo(String.class).to("$simple{file:path}"));

(Originally answered for filtering based on file name) (最初回答基于文件名的过滤)

For filtering based on file name :- You can use filter 对于基于文件名的过滤:-您可以使用过滤器

Something like 就像是

from("file:" + "</path/to/original>" + "?noop=true").noAutoStartup().routeId("xxx").fileFilter($org.apache.camel.Exchange.FILE_NAME.contains("xyz")).split(new ZipSplitter()).streaming()
            .convertBodyTo(String.class).to("file:" +  "</path/to/import>");

Also, camel allows regex in the file name itself. 同样,骆驼允许文件名本身中的正则表达式。 So you can use something like 所以你可以使用类似

from("file:" + "</path/to/original>" + "regex pattern  in file name" + "?noop=true").noAutoStartup().routeId("xxx").split(new ZipSplitter()).streaming()
            .convertBodyTo(String.class).to("file:" +  "</path/to/import>");

If you look at http://camel.apache.org/file2.html you will see that the camel file component sets some headers. 如果查看http://camel.apache.org/file2.html,您会发现骆驼文件组件设置了一些标题。 Try something like 尝试类似

from("file:" + "</path/to/original>" + "?noop=true").noAutoStartup().routeId("xxx").split(new ZipSplitter()).streaming()
        .convertBodyTo(String.class).to("file:" +  "</path/to/import>" + header("CamelFileNameOnly"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM