【问题标题】:Apache Camel zip folderApache Camel 压缩包
【发布时间】:2015-04-23 20:07:36
【问题描述】:

我正在尝试使用 Camel 压缩文件夹。我有一个Processor,它创建了包含文件的文件夹:

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

}

它工作正常。

在我的路线中,我尝试压缩 hi 文件夹,所以我这样做了:

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

}

那是行不通的。我得到了:

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

目录不是文件吗?不能用 Camel 压缩整个文件夹及其内容吗?

谢谢大家。

【问题讨论】:

    标签: java apache-camel zipfile


    【解决方案1】:

    警告 - 我不是 Camel 方面的专家 - 但我可以为您提供一些指导。

    至少在 Java 中,当您使用压缩多个文件时,您会将每个文件添加为 zip 文件中的“zip 条目”。这是example。因此,不,您永远不会直接压缩目录 - 虽然目录是文件,但文件仅包含指向其他文件的指针,因此压缩目录“文件”不会获得所需的结果。

    现在是第二个问题 - 我看到此链接确实将多个文件压缩为一个。检查此link。请注意使用通配符 (*.txt) 来引用要压缩的文件,而不是使用目录。希望这会有所帮助。

    【讨论】:

    • 谢谢,我会检查一下。
    猜你喜欢
    • 2013-05-29
    • 2013-07-24
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2018-06-25
    相关资源
    最近更新 更多