我正在尝试使用 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/