1

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

LevKaz
  • 74
  • 1
  • 7
  • 1
    [According to these users, the answer is no](https://stackoverflow.com/questions/21144140/how-to-show-extraction-progress-of-7zip-inside-cmd). Maybe something has changed or someone has some fresh ideas. – DavidS Apr 07 '16 at 21:37
  • First let me thank you for the answer, but the link you posted is about the _extrating_ process, not the _zipping_ process. While extracting doesn't show a thing (at least in the description of the problem), zipping via command line gives the percentage how far the process is. – LevKaz Apr 07 '16 at 22:01
  • Checked the extracting progress and doing this via command-line gives also a % how far the process is (with the current 7z file)... Any other suggestions? – LevKaz Apr 07 '16 at 22:22
  • In that case can you parse the information out of `Process.getOutputStream()`? Is the information there? – DavidS Apr 07 '16 at 23:18
  • See my answer below, changed the "-bs" switches for the output streams and then I got the output I needed. Don't know if I can get the info out of Process.getOutputStream() since I dont know how to "convert" the OutputStream so I can use it with sysout.println() – LevKaz Apr 08 '16 at 00:11

1 Answers1

1

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"
LevKaz
  • 74
  • 1
  • 7