2011-09-15 95 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())并确保创建所需的目录。 –