2

I'm trying to extract a tar.gz file in windows using command line and get a progress precentage. First, I want it to be extracted in a single operation (not extract the tar and then untar). The only two tools that can do it I found are Tartool and 7zip. Tartool is good (also easy to detect errors) but has no progress. 7zip you must use the strem in and out, so I created a basic bat file:

cd /d %~dp0
7z.exe x "%1" -so | 7z.exe x -aoa -si -ttar -o"%2"

It works fine with extraction, but the progress remains still 0% during the whole process (I tried running from cmd), I think that's because of stream input and output.

I know that winrar can show a progress percentage even when doing this operation (extract a tgz file), so how can I achive the same result?

The closest I can get to my goal is getting the output dir size and dividing by the uncompressed size of tar.gz file (got through 7z.exe l function). Also the progress is not linear, it changes only when a file is extracted (EG if my targz contains 2 very big files, I can only get 0%, 50% and 100% during extraction)

3 Answers 3

7

Many programmers have installed Git for Windows. It comes with a fine Bash environment which also contains GNU tar. Using the regular tar command you can easily use options for checking progress, especially the --checkpoint and --checkpoint-action options. Play around with them, you can do nice things with them, from simple stuff like

tar --checkpoint=1 --checkpoint-action=dot -xzf my-archive.tar.gz
tar --checkpoint=1 --checkpoint-action=echo -xzf my-archive.tar.gz

to more advanced stuff like

tar --checkpoint=1 --checkpoint-action="ttyout=Hit %s checkpoint #%u%*\r" -xzf my-archive.tar.gz
tar --checkpoint=1 --checkpoint-action=ttyout='%{%Y-%m-%d %H:%M:%S}t (%d sec): #%u, %T%*\r' -xzf my-archive.tar.gz

if you do not like scrolling messages but output being updated in-line - or whatever you please. You can even run any shell command you like.

3
+50

This is one option

@echo off
    setlocal enableextensions disabledelayedexpansion

    for %%a in ("%~f1")  do set "inputFile=%%~fa"
    if not defined inputFile    goto :eof
    if not exist "%inputFile%"  goto :eof
    if exist "%inputFile%\"     goto :eof

    for %%a in ("%~f2.") do set "outputFolder=%%~fa"

    (
        7z.exe x -bsp2 -so "%inputFile%" 
    )|(
        7z.exe x -ttar -aoa -si -bso0 -bsp0 -o"%outputFolder%"
    )

There are two different processes, and you need to tell them who will output the progress information. In this code the uncompress part is sending the progress output to standard error stream (-bsp2, send progress to stream 2) while the unarchive part will only show errors (-bso0 -bsp0, disable output, disable progress).

You can also do it the inverse way

    (
        7z.exe x -bsp0 -bse0 -so "%inputFile%" 
    )|(
        7z.exe x -ttar -aoa -si -bso0 -bsp2 -o"%outputFolder%"
    )

The progress indicator depends on the operation being done so select the one that better fits your case.

note: No, it is not needed to write the pipe this way, just to shorten lines and make sample code easier to read.

0

You could take the C# source code to TarTool and add in your own hooks for displaying the progress. It's based on SharpZipLib, which has the events that you can wire up into for handling the progress.

1
  • SharpZipLib is unable to handle long files over 99 characters which means that TarTool also fails on this.
    – Christian
    Jan 23, 2018 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.