2012-04-04 97 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); //設置追加到真正

+0

@biggusjimmusThanks – 2012-04-04 08:32:21

+0

謝謝。我做了上述改變。 tar內容被保留,但新條目(activeSensor.cfg)不會被添加到焦油中。我是否將傳遞給TarArchiveEntry構造函數的路徑或參數搞亂了? – 2012-04-04 08:44:51

+0

在我去這裏搜索之前,我正在使用這種方法。似乎導致條目超出tar歸檔的末尾,所以我會得到一個錯誤,但我的新文件不會在那裏。 – froggythefrog 2017-05-11 02:09:30

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)的大小,因爲它只將文件大小讀取爲整數精度字節(請參閱cast轉換爲int)。但是,這不是問題,因爲無論如何,Apache Compress都不能處理超過2 GB的文件。
+0

謝謝,它工作。我不得不稍微調整代碼,因爲我無法直接以.tar.gz文件格式執行您共享的整個代碼。 所以我必須首先解壓文件才能達到.tar格式。按照建議的方式在tar文件中注入必需的條目後,我不得不重新壓縮它。 對於解壓縮和重新壓縮,我使用了 java.util.zip.GZIPInputStream和java.util.zip.GZIPOutputStream實用程序。 – 2012-04-04 12:38:16

相關問題