2014-10-15 5 views
4

내 경로에 여러 개의 CSV 파일을 압축하려고했습니다. 나는 그것을 성공적으로 할 수있었습니다.낙타를 사용하여 파일을 지퍼로 잠그고 암호로 보호

나는 봄을 사용하여 동일한 작업을 수행하고 있습니다.

이제 새로운 요구 사항은 입니다. 비밀번호는입니다. 다음은 내가 사용한 집계 전략입니다. 이것을 달성하는 방법?

<route autoStartup="false" routePolicyRef="routeTwoTimer" startupOrder="2" id="zippingFileRoute"> 
    <from uri="{{to.file.processed1}}"/> 
    <choice id="csvZipFile"> 
     <when> 
      <simple>$simple{header.CamelFileName} regex '^.*(csv|CSV)$'</simple> 
      <aggregate strategyRef="zipAggregationStrategy" completionFromBatchConsumer="true" eagerCheckCompletion="true"> 
       <correlationExpression> 
        <constant>true</constant> 
       </correlationExpression> 
       <to uri="{{to.file.processed2}}"/> 
      </aggregate> 
     </when> 
    </choice> 
</route> 
+0

질문하지 않았습니다. 당신이 무엇을 해야할지 모르겠다는 말입니까? 뭔가 해봤습니까? 작동하지 않습니다. – Ray

+0

예 레이 어떻게 할 수 있는지 모르겠습니다. – Yoogi

+0

맞아, [실제 문서] (http://camel.apache.org/zip-file-dataformat.html)는 zip 옵션 (예 : 비밀번호)에 대해 아무 것도 말하지 않습니다. 나는 그것이있는 그대로 지원되지 않는다고 생각한다. 하나의 옵션은 모든 파일을 암호화 한 다음 zip으로 압축하거나 zip 파일을 압축하고 암호화하는 것입니다. –

답변

0

의견에서 지적한대로 Java API는 ZIP 파일 암호화가 제한되어 있습니다. Apache Camel ZipAggregationStrategyZipOutputStream을 사용하므로이 제한도 있습니다. Zip 파일의 암호화를 허용하는 다른 라이브러리를 사용하여 사용자 정의 Aggregator을 구현할 수 있습니다. 예를 Zip4j

를 들어

import net.lingala.zip4j.core.ZipFile; 
//next few imports. I have added this only to take correct ZipFile class, not the JDK one 

public class PasswordZipAggregationStrategy implements AggregationStrategy { 

public static final String ZIP_PASSWORD_HEADER = "PasswordZipAggregationStrategy.ZipPassword"; 

@Override 
public Exchange aggregate(Exchange oldExchange, Exchange newExchange){ 
    try { 
     if (newExchange == null) { 
      return oldExchange; 
     } 
     return aggregateUnchecked(oldExchange,newExchange); 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
} 

private Exchange aggregateUnchecked(Exchange oldExchange, Exchange newExchange) throws Exception{ 
    ZipFile zipFile; 
    String password; 
    if (oldExchange == null) { // first 
     password = newExchange.getIn().getHeader(ZIP_PASSWORD_HEADER, String.class); 
     zipFile = new ZipFile(newExchange.getExchangeId()+".zip"); 
     File toDelete = new File(zipFile.getFile().getPath()); 
     newExchange.addOnCompletion(new Synchronization() { 
      @Override 
      public void onComplete(Exchange exchange) { 
       toDelete.delete(); 
      } 

      @Override 
      public void onFailure(Exchange exchange) { 
      } 
     }); 
    } else { 
     password = newExchange.getIn().getHeader(ZIP_PASSWORD_HEADER, String.class); 
     zipFile = new ZipFile(oldExchange.getIn().getBody(File.class)); 
    } 

    if (password==null){ 
     throw new IllegalStateException("Null password given"); 
    } 

    ZipParameters zipParameters = new ZipParameters(); 
    zipParameters.setPassword(password); 
    zipParameters.setEncryptFiles(true); 
    zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FAST); 
    zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); 
    zipFile.addFile(newExchange.getIn().getBody(File.class), zipParameters); 
    GenericFile genericFile = FileConsumer.asGenericFile(zipFile.getFile().getParent(), zipFile.getFile(), Charset.defaultCharset().toString(), false); 
    genericFile.bindToExchange(newExchange); 

    newExchange.getIn().setBody(zipFile.getFile()); 
    newExchange.getIn().setHeader(ZIP_PASSWORD_HEADER, password); 
    return newExchange; 
} 
} 

를 사용하여 사용자 정의 어 그리 게이터를 구현 메이븐 의존성

<dependency> 
    <groupId>net.lingala.zip4j</groupId> 
    <artifactId>zip4j</artifactId> 
    <version>1.3.2</version> 
</dependency> 

를 추가 그것은

from("file://in") 
    .to("log:in") 
    .setHeader(PasswordZipAggregationStrategy.ZIP_PASSWORD_HEADER, constant("testPassword")) 
    .aggregate().constant(true).completionFromBatchConsumer() 
     .aggregationStrategy(new PasswordZipAggregationStrategy()) 
.to("log:out") 
.to("file://out"); 
관련 문제