2015-04-23 193 views
3

我想用骆驼压缩文件夹。我有一个Processor,它创建了文件夹里面的文件:Apache骆驼zip文件夹

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 

目录不是文件吗?是否无法用骆驼压缩整个文件夹及其内容?

谢谢大家。

回答

0

警告 - 我不是骆驼专家 - 但我可以为您提供一些指导。

在Java中,至少在处理压缩多个文件时,您需要将每个文件添加为压缩文件中的“zip条目”。这是一个example。因此,不要直接压缩目录 - 而目录是文件时,该文件只包含指向其他文件的指针,因此压缩目录“文件”无法获得理想的结果。

现在对于第二个问题 - 我看到这个链接确实将多个文件压缩成一个。检查这个link。注意使用通配符(* .txt)来引用要压缩的文件而不使用目录。希望这可以帮助。

+0

谢谢,我会检查这个。 –