3

I'm trying to zip a folder with Camel. I have a Processor that hass created folders with files inside them:

public class CreateDirProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {

        Message in = exchange.getIn();

        File d = new File("myDir/hi");

        d.mkdirs();

        File f = new File(d, "hello.txt");

        f.createNewFile();

        in.setBody(f);
    }

}

It works correctly.

In my route, I try to zip the hi folder so I did this:

public static void main(String[] args) throws Exception {
    CamelContext context = new DefaultCamelContext();

    ProducerTemplate template = context.createProducerTemplate();

    context.addRoutes(new RouteBuilder() {
        public void configure() {
            from("direct:source")
            .process(new CreateDirProcessor())
            .marshal().zipFile().to("file:zipped");
        }
    });

    context.start();

    template.sendBody("direct:source", "test");

    Thread.sleep(3000);

    context.stop();

}

And that does not work. I got:

TypeConversionException: Error during type conversion from type: java.io.File to the required type... `myDir/hi` is a directory

Is a directory not a file? Is it not possible to zip a whole folder and its contents with Camel?

Thank you all.

Farid M
  • 35
  • 5

1 Answers1

0

Caveat - I am not an expert in Camel - but I could contribute to give you some direction.

In Java at least when you are working with zipping multiple files you add each file as a " zip entry" in your zip file. Here is an example. So no you never directly zip a directory - while a directory is a file the file only contains pointers to other files so zipping the directory "file" wouldn't gain the desired results.

Now for the second question - I see this link does zip multiple files in one. Check this link. Notice the use of wild-card (*.txt) to refer to files to zip and not using directory. Hope this helps.

Community
  • 1
  • 1
Y123
  • 915
  • 13
  • 30