开发者

Open debian package with Java

Are there any libraries in Java fo开发者_运维百科r unpacking a .deb (debian) archive? Unfortunately I Couldn't find anything useful out there yet. Thanks.


If you by unpacking mean extracting the files, it should be possible with Apache Commons Compress. A .deb file is "implemented as an ar archive" and Commons Compress is able to uncompress ar archives.


Ok, so as suggested I've used apache commons compress and here's a method that does the trick. Downladed it from the 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜