Stefan Ciprian Iuga
Stefan Ciprian Iuga

Reputation: 35

Trying to run rpm2cpio with java

I've tried running the command in java but with no luck

rpm2cpio <rpmName> | cpio -imdv

This is my current Code

public static void decompress() {
    System.out.println("Decompression has started");
    Archiver archiver = ArchiverFactory.createArchiver(ArchiveFormat.TAR, CompressionType.GZIP);
    try {
        archiver.extract(autoUpdateFile, tempDestination);

        for(File currentRPM: new File("./temp/patches/rpms/").listFiles()) {
            if(currentRPM.getName().contains("Data")) {
                ProcessBuilder decompressRPM = new ProcessBuilder(
                        new String[] {"rpm2cpio", currentRPM.getAbsolutePath(), "cpio -idmv"});
                Process startDecompression = decompressRPM.start();

                InputStream inputFromDecompression = startDecompression.getInputStream();
                BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputFromDecompression));

                String line = null;
                System.out.println("From Input Buffer");
                while((line = inputReader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Decompression ended");
}

The main idea is that I have a compressed file which contains multiple files inside and in one of the files exists a list of rpms that I need decompressed to get some of the data out to update the project.

Is there any way that I can run such a command or do I need to use a library?

Thank you.

Update: thanks to @Brian I managed to fix it by changing from

ProcessBuilder decompressRPM = new ProcessBuilder(
                    new String[] {"rpm2cpio", currentRPM.getAbsolutePath(), "cpio -idmv"});
Process startDecompression = decompressRPM.start();

to

String[] decompressionCommand = {"/bin/sh", "-c", "rpm2cpio " + currentRPM.getAbsolutePath() + " | cpio -idmv"};
Process startDecompression = Runtime.getRuntime().exec(decompressionCommand);

But I ended up with another problem. I'm getting permission denied. I will be searching around for a solution and in case I find something I will post it here for future reference.

Upvotes: 0

Views: 289

Answers (1)

Stefan Ciprian Iuga
Stefan Ciprian Iuga

Reputation: 35

It seems that it works just fine now. The solution is pretty simple, it works with both ProcessBuilder and String[]... Runtime.getRuntime.exec(). Since the file that I'm trying to decompress can only be done while being root, that means that I need to run both eclipse and the program as administrator. I will post the code that checks if the program is being run as admin as soon as I'm done with all the testing.

Upvotes: 0

Related Questions