LevKaz
LevKaz

Reputation: 74

Getting 7-Zip information in java

I'm using 7-zip in my java program to zip several files and folders (mostly games) to several 7-zip files.

As an example:

test1.txt and test1 -> test1.7z
test2.txt and test2 -> test2.7z

The zipping is no problem, but since the folders can be really large I wanted to include 2 progress bars (so the user can check how far the zip-process is).

Currently I have the following code (which works so far), but how can I get the percentage of the zipping process? The current output just gives back the start and result of the zipping process.

private void runZipCommand() {
    ProcessBuilder pb = null;

    ArrayList<Game> game = getGames();

    for (Game g : game) {
        pb = new ProcessBuilder(makeZipString(g, false));
        pb.redirectError();
        try {
            Process p = pb.start();

            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;

            while((line = br.readLine()) !=null){
                System.out.println(line);
            }               

            System.out.println("Exited with: " + p.waitFor());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

private String makeZipString(Game g, boolean batch) {
    String programzip = "";
    String gametitle = "";
    String restfiles = "";
    programzip = "\"" + sevenPath + "\\7z.exe\" " + ZIPCOMMANDS;

    if (batch) {
        gametitle = " \"" + g.getName().replaceAll("[_[^\\w\\däüöÄÜÖ\\+\\- ]]", "") + ".7z\" ";
    } else {
        gametitle = " \"" + backupPath.toString() + "\\" + g.getName().replaceAll("[_[^\\w\\däüöÄÜÖ\\+\\- ]]", "")
                + ".7z\" ";
    }

    restfiles = "\"" + g.getAppmanifest() + "\" \"" + steamUtil.getInstallDir() + "\\" + g.getDir() + "\"";

    String s = programzip + gametitle + restfiles;

    return s;
}

Is there any way to get the percentage of how far the single process (zipping test1.7z) is?

If you need more information, I try to add it as fast as possible.

Thanks for your answers.

LevKaz

Upvotes: 1

Views: 616

Answers (1)

LevKaz
LevKaz

Reputation: 74

I think I solved my problem now.

My "old" zip-command had following commands and switches:

7z.exe a -t7z -m0=LZMA2 -mx9 -mmt=2

After checking out the 7-zip.chm, I found out, that I can change the output stream via switch:

-bs

After changing the output streams to the following

7z.exe a -t7z -m0=LZMA2 -mx9 -mmt=2 -bso0 -bse2 -bsp1

I can extract the progress from my BufferedReader.

Anyway, thanks to all who visited my question and special thanks to @DavidS for linking in the other question which led me to the right point.

EDIT:

This also is going well while extracting a *.7z file. Use the following command:

7z.exe x -bso0 -bse2 -bsp1 archive.7z -aoa -o"c:\OutputPath"

Upvotes: 1

Related Questions