dbt
dbt

Reputation: 113

How to extract RPM files to specific package from Java

I want to extract a rpm file to target directory from Java code. I'm aware of the following approaches:

Is there a better approach that I'm not aware of? Ideally something that works like a TarArchiveInputStream would be perfect.

Upvotes: 1

Views: 604

Answers (2)

user3076105
user3076105

Reputation: 416

Yes. An alternative using pure java is as follows:

  • Use com.jguild.jrpm.io.RPMFile.readFromStream to read the RPM file opened as a stream. This will:
    • Read the RPM headers
    • Position the stream to the beginning of the archive data
  • Wrap the stream with an org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream starting at the current position
  • Use getNextCPIOEntry to read the CPIO archive, entry by entry
    • Use the properties of the result, a CpioArchiveEntry, to create a corresponding local file system entry
    • If a directory, it has no data; just create it
    • If a symbolic link, the data is the target of the link; create it on the local file system
    • If a regular file, the data is contents of the file; write it to the local file until a (simulated) end of file is reached
    • Other types (pipe, device node, ...) can be handled as appropriate
    • Update local entry's permissions, ownership, group, etc., as appropriate
  • Repeat until no more archive entries remain

Upvotes: 0

dbt
dbt

Reputation: 113

This is the middle ground that I found working:

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(String.format("rpm2cpio %s", packagePath));
try (CpioArchiveInputStream cpioStream = new CpioArchiveInputStream(proc.getInputStream())) {
    System.out.println(cpioStream.getNextCPIOEntry().getName());
}

Not perfect, but makes it easier so that we only rely on a new process to read file.

Upvotes: 2

Related Questions