2012-04-04 3 views
3

기존 tar 파일에 구성 파일을 추가해야합니다. 나는 apache.commons.compress 라이브러리를 사용하고 있습니다. 다음 코드 조각은 항목을 올바르게 추가하지만 tar 파일의 기존 항목을 덮어 씁니다. 타르 확인에기존 내용을 덮어 쓰지 않고 항목을 tar 파일에 추가하십시오.

public static void injectFileToTar() throws IOException, ArchiveException { 
     String agentSourceFilePath = "C:\\Work\\tar.gz\\"; 
     String fileToBeAdded = "activeSensor.cfg"; 
     String unzippedFileName = "sample.tar"; 

    File f2 = new File(agentSourceFilePath+unzippedFileName); // Refers to the .tar file 
    File f3 = new File(agentSourceFilePath+fileToBeAdded); // The new entry to be added to the .tar file 

    // Injecting an entry in the tar 
    OutputStream tarOut = new FileOutputStream(f2); 
    TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("tar", tarOut); 
    TarArchiveEntry entry = new TarArchiveEntry(fileToBeAdded); 
    entry.setMode(0100000); 
    entry.setSize(f3.length()); 
    aos.putArchiveEntry(entry); 
    FileInputStream fis = new FileInputStream(f3); 
    IOUtils.copy(fis, aos); 
    fis.close(); 
    aos.closeArchiveEntry(); 
    aos.finish(); 
    aos.close(); 
    tarOut.close(); 

}

만 "activeSensor.cfg"파일이 발견되고 타르의 초기 내용이 누락 발견된다. "모드"가 올바르게 설정되지 않았습니까?

답변

0

한번에 변경

OutputStream tarOut = new FileOutputStream(f2);

OutputStream tarOut = new FileOutputStream(f2, true); // true로 설정

+0

@biggusjimmusThanks –

+0

감사합니다. 나는 위의 변화를 만들었다. 타르 내용은 유지되지만 새로운 항목 (activeSensor.cfg)은 타르에 추가되지 않습니다. TarArchiveEntry 생성자에 전달 된 경로 또는 인수로 무언가를 엉망으로 만들고 있습니까? –

+0

여기에서 검색하기 전에이 접근법을 사용하고있었습니다. 엔트리가 tar 아카이브의 끝 부분을 넘어서는 것처럼 보이기 때문에 그 결과에 오류가 발생하지만 새 파일은 거기에 없을 것입니다. – froggythefrog

2

문제는 TarArchiveOutputStream가 자동으로 뭔가가 기존 아카이브에 읽지 않습니다 있는지에 추가하는 당신은해야 할 것입니다.

CompressorStreamFactory csf = new CompressorStreamFactory(); 
ArchiveStreamFactory asf = new ArchiveStreamFactory(); 

String tarFilename = "test.tgz"; 
String toAddFilename = "activeSensor.cfg"; 
File toAddFile = new File(toAddFilename); 
File tempFile = File.createTempFile("updateTar", "tgz"); 
File tarFile = new File(tarFilename); 

FileInputStream fis = new FileInputStream(tarFile); 
CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis); 
ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis); 

FileOutputStream fos = new FileOutputStream(tempFile); 
CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos); 
ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos); 

// copy the existing entries  
ArchiveEntry nextEntry; 
while ((nextEntry = ais.getNextEntry()) != null) { 
    aos.putArchiveEntry(nextEntry); 
    IOUtils.copy(ais, aos, (int)nextEntry.getSize()); 
    aos.closeArchiveEntry(); 
} 

// create the new entry 
TarArchiveEntry entry = new TarArchiveEntry(toAddFilename); 
entry.setSize(toAddFile.length()); 
aos.putArchiveEntry(entry); 
IOUtils.copy(new FileInputStream(toAddFile), aos, (int)toAddFile.length()); 
aos.closeArchiveEntry(); 

aos.finish(); 

ais.close(); 
aos.close(); 

// copies the new file over the old 
tarFile.delete(); 
tempFile.renameTo(tarFile); 

노트의 몇 :의 라인을 따라 뭔가

  • 이 코드는 (해당 try-catch-finally 블록을 추가하시기 바랍니다) 처리 예외를 포함하지 않는
  • 의 파일을 처리하지 않습니다이 코드 2147483647 이상의 크기 (Integer.MAX_VALUE)는 파일 크기를 정수 정밀도 바이트로만 읽습니다 (캐스팅을 int 참조). 그러나 Apache 압축은 2GB 이상의 파일을 처리하지 않으므로 문제가되지 않습니다.
+0

감사합니다. .tar.gz 파일 형식으로 직접 공유 한 전체 코드를 실행할 수 없다는 점에서 코드를 약간 수정해야했습니다. 그래서 먼저 파일을 압축 해제하여 .tar 형식으로 가져와야했습니다. 제안 된 방식에 따라 필요한 항목을 tar 파일에 삽입 한 후 다시 압축을 풀어야했습니다. 압축 해제 및 재 압축의 경우, java.util.zip.GZIPInputStream 및 java.util.zip.GZIPOutputStream 유틸리티를 사용했습니다. –

관련 문제