简体   繁体   English

尝试使用Java运行rpm2cpio

[英]Trying to run rpm2cpio with java

I've tried running the command in java but with no luck 我试过在Java中运行命令但没有运气

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. 主要思想是我有一个压缩文件,其中包含多个文件,并且在一个文件中存在一个rpm列表,我需要对其进行解压缩以获取一些数据以更新项目。

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 更新:感谢@Brian,我设法通过更改从

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. 我将四处寻找解决方案,以防万一我将其发布在此处以供将来参考。

It seems that it works just fine now. 看来现在一切正常。 The solution is pretty simple, it works with both ProcessBuilder and String[]... Runtime.getRuntime.exec(). 该解决方案非常简单,可与ProcessBuilder和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. 由于我要解压缩的文件只能在成为root用户时才能完成,这意味着我需要以管理员身份运行eclipse和程序。 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. 完成所有测试后,我将发布用于检查程序是否正在以admin身份运行的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM