使用Java打开debian包

时间:2011-09-15 14:09:11

标签: java debian unpack deb

Java中是否有用于解压缩.deb(debian)存档的库?不幸的是我还没找到任何有用的东西。感谢。

2 个答案:

答案 0 :(得分:3)

如果您通过解压缩意味着提取文件,则应该可以使用Apache Commons Compress。 .deb文件为“implemented as an ar archive”,Commons Compress能够解压缩档案。

答案 1 :(得分:1)

好的,所以我建议使用apache commons compress,这是一个可以解决问题的方法。从maven repo下载它: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;
}
相关问题