java - Apache Camel zip 文件夹

标签 java apache-camel zip

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

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

}

它工作正常。

在我的 route ,我尝试压缩 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 压缩整个文件夹及其内容?

谢谢大家。

最佳答案

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

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

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

关于java - Apache Camel zip 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29833315/

相关文章:

python - python 中的交错字符串

java - 未知故障(在 android.os.Binder.execTransact(Binder.java :674)) Error while Installing APKs

java - 将应用程序从 Oracle 更改为 Hadoop

java - 如何防止Camel swagger 2.16.2的枚举参数类型

java - 从 camel xmpp 发送消息时出现问题,jid 格式错误

windows - 如何检测 Windows 批处理文件是否从 ZIP 存档运行

macos - 如何使用 Mac Finder 列出文件夹中的所有别名

java - 向 JNA 类 Java (Kernel32) 添加另一个方法

java - super 构造函数调用

cross-domain - Camel JAX-RS 和跨域请求