【问题标题】:ProcessBuilder with gunzip does not work带有 gunzip 的 ProcessBuilder 不起作用
【发布时间】:2013-05-05 13:41:46
【问题描述】:

我正在尝试运行这段代码,但由于注释失败:

gzip: /home/idob/workspace/DimesScheduler/*.gz: 没有这样的文件或目录

代码:

ProcessBuilder gunzipPB = new ProcessBuilder("gunzip", System.getProperty("user.dir") + File.separator + "*");
gunzipPB.inheritIO();
int gunzipProcessExitValue;

try {
        gunzipProcessExitValue = gunzipPB.start().waitFor();
    } catch (InterruptedException | IOException e) {
        throw new RuntimeException("Service " + this.getClass().getSimpleName() + " could not finish creating WHOIS AS Prefix Table", e);
    }

logger.info("Finished unzipping radb and ripe files. Process exit value : {}", gunzipProcessExitValue);

退出值为 1。

终端中的相同命令可以正常工作(文件存在)。

可能是什么问题?

谢谢。

伊多

编辑:

在尝试使用 DirectoryStrem 后,我得到了这个异常: java.nio.file.NoSuchFileException: /home/idob/workspace/DimesScheduler/*.gz

知道可能是什么问题吗?文件确实存在。

完整代码:

ProcessBuilder radbDownloadPB = new ProcessBuilder("wget", "-q", "ftp://ftp.radb.net    /radb/dbase/*.db.gz");
ProcessBuilder ripeDownloadPB = new ProcessBuilder("wget", "-q", "ftp://ftp.ripe.net/ripe/dbase/split/ripe.db.route.gz"); 

    radbDownloadPB.inheritIO();
    ripeDownloadPB.inheritIO(); 

    try {

        int radbProcessExitValue = radbDownloadPB.start().waitFor();
        logger.info("Finished downloading radb DB files. Process exit value : {}", radbProcessExitValue);

        int ripeProcessExitValue = ripeDownloadPB.start().waitFor();
        logger.info("Finished downloading ripe DB file. Process exit value : {}", ripeProcessExitValue);

        // Unzipping the db files - need to process each file separately since java can't do the globing of '*'  
        try (DirectoryStream<Path> zippedFilesStream = Files.newDirectoryStream(Paths.get(System.getProperty("user.dir"), "*.gz"))){
            for (Path zippedFilePath : zippedFilesStream) {
                ProcessBuilder gunzipPB = new ProcessBuilder("gunzip", zippedFilePath.toString());
                gunzipPB.inheritIO();
                int gunzipProcessExitValue = gunzipPB.start().waitFor();
                logger.debug("Finished unzipping file {}. Process exit value : {}", zippedFilePath, gunzipProcessExitValue);
            }
        }

        logger.info("Finished unzipping ripe and radb DB file");

    } catch (InterruptedException | IOException e) {
        throw new RuntimeException("Service " + this.getClass().getSimpleName() + " could not finish creating WHOIS AS Prefix Table", e);
    }

谢谢...

【问题讨论】:

    标签: java processbuilder gunzip


    【解决方案1】:

    *.gzglob 不是由 gunzip 命令处理,而是由 shell 处理。例如,shell 会将gunzip *.gz 转换为gunzip a.gz b.gz。现在,当您通过 java 执行时,您要么必须调用 bash 为您执行 glob,要么在 java 中扩展 glob,因为 gzip 不知道如何处理 glob。

    Java 7 有 new libraries,这使得扩展 glob 模式更容易。

    【讨论】:

    • 谢谢。正如你所说,我可以使用 java.nio.DirectoryStream 或者我可以运行类似的东西: new ProcessBuilder("/bin/sh", "-c", "gunzip ./*");
    猜你喜欢
    • 2018-09-10
    • 2013-03-26
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多