0
votes

I'd like to unzip a zip-file to a folder called like the zip-file. E.g. original/abc.zip should be unzipped to 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!

3

3 Answers

6
votes

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. 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. Directory must not contain dynamic expressions with ${ } placeholders.

  2. You don't need .convertBodyTo(String.class)

  3. For the producer I used toD which takes the outbox folder from a property. 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)

    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. to get the original zip file name without the extension. In your case abc. Details here: https://camel.apache.org/file-language.html

0
votes

If you look at http://camel.apache.org/file2.html you will see that the camel file component sets some headers. 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"));
0
votes

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>");