2011-09-15 185 views
2

Java中是否有用於解壓縮.deb(debian)壓縮文件的庫?不幸的是我找不到任何有用的東西。謝謝。使用Java打開debian軟件包

+1

你是說打開存檔來檢查它的內容還是實際部署存檔? – Peter

+0

用戶寫了「...用於解包.deb ...」,因此他可能意味着提取。 – noamt

+0

我想解壓並將檔案部署到臨時文件夾。因此,如果.deb存檔文件包含文件/文件夾X,Y,Z,我想將X,Y,Z提取到臨時文件夾中,請說「T」並能夠創建「新文件(T,X)」。 –

回答

3

如果您通過解包意味着解壓文件,應該可以使用Apache Commons Compress。 .deb文件是「implemented as an ar archive」,Commons Compress能夠解壓縮存檔。

+0

謝謝,我一定會嘗試Apache Commons Compress ...沒注意到「ar檔案」部分。 –

+0

請注意,頂級'ar'檔案庫將包含兩個'tar'檔案,但顯然ACC也應對這一問題。 – tripleee

1

好吧,所以建議我使用apache commons compress,這裏有一個方法可以實現。從Maven回購下載:http://mvnrepository.com/artifact/org.apache.commons/commons-compress/1.2

/** 
* Unpack a deb archive provided as an input file, to an output directory. 
* <p> 
* 
* @param inputDeb  the input deb file. 
* @param outputDir  the output directory. 
* @throws IOException 
* @throws ArchiveException 
* 
* @returns A {@link List} of all the unpacked files. 
* 
*/ 
private static List<File> unpack(final File inputDeb, final File outputDir) throws IOException, ArchiveException { 

    LOG.info(String.format("Unzipping deb file %s.", deb.getAbsoluteFile())); 
    LOG.info(String.format("Into dir %s.", outDir.getAbsoluteFile())); 

    final List<File> unpackedFiles = new LinkedList<File>(); 
    final InputStream is = new FileInputStream(inputDeb); 
    final ArArchiveInputStream debInputStream = (ArArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("ar", is); 
    ArArchiveEntry entry = null; 
    while ((entry = (ArArchiveEntry)debInputStream.getNextEntry()) != null) { 
     LOG.info("Read entry"); 
     final File outputFile = new File(outputDir, entry.getName()); 
     final OutputStream outputFileStream = new FileOutputStream(outputFile); 
     IOUtils.copy(debInputStream, outputFileStream); 
     outputFileStream.close(); 
     unpackedFiles.add(outputFile); 
    } 
    debInputStream.close(); 
    return unpackedFiles; 
} 
+0

我對上述源代碼進行了更正。請注意,「entry」變量可能代表一個目錄。在這種情況下,請添加檢查if(entry.isDirectory())並確保創建所需的目錄。 –