Ido
Ido

Reputation: 583

ProcessBuilder with gunzip does not work

I am trying to run this code which fails with the note:

gzip: /home/idob/workspace/DimesScheduler/*.gz: No such file or directory

The code:

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);

Exit value is 1.

Same command in terminal works just fine (the files exist).

What can be the problem?

Thanks.

Ido

EDIT:

After trying to use DirectoryStrem I am getting this exception: java.nio.file.NoSuchFileException: /home/idob/workspace/DimesScheduler/*.gz

Any idea what can be the problem? The files do exist.

The full code:

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);
    }

Thanks...

Upvotes: 1

Views: 816

Answers (1)

sbridges
sbridges

Reputation: 25150

the *.gz glob is not handled by the gunzip command, but the shell. For example, the shell will translate gunzip *.gz to gunzip a.gz b.gz. Now when you exec through java, you either have to invoke bash to do the globbing for you, or expand the glob in java, since gzip doesn't know how to handle the glob.

Java 7 has new libraries which make expanding glob patterns easier.

Upvotes: 1

Related Questions