6

I have a hard drive that contains many ZIP files of backups of various things. They were compressed at different points in time in different and inconsistent ways, and a lot of them use minimal compression settings.

Is there a way (preferably with 7zip) to recompress all ZIPs on a drive using the same (best) compression settings?

5
+100

Below is a script I've used in the past for a similar task with 100% success that is pure batch with 7Zip where you can explicitly specify the source directory where it can be a drive letter or folder.

This also allows you to explicitly specify an initial root temp directory and then create a new folder within it matching the name of the zip file minus the zip extension, and then removes it once the 7Zip archive operations are complete for each zip file the For /F loop iterates and processes.

Script

You just set the values of the Src=, ExtractDir=, and z7= variable to be whatever they need to be in your environment at the top of the script and the rest will just work.

You can specify whatever 7Zip compression methods you need at the end of the %z7% a ~ command right after the -aoa switch like -aoa -m<method_parameters> for your needs.

To run as a batch script though, save the logic to a text document on your desktop, etc. as <something>.cmd from the text document file | save options. Once saved, just double-click it to execute the logic (or right click run as administrator), and confirm the files have been manipulated as expected afterwards.

@ECHO ON
SET Src=X:\
SET ExtractDir=%Src%\tmpExtract007
SET z7="C:\Program Files\7-Zip\7z.exe"

FOR /F "usebackq tokens=*" %%a IN (`DIR /S /B /A-D "%Src%\*.zip"`) DO (
    IF NOT EXIST "%ExtractDir%\%%~Na" MD "%ExtractDir%\%%~Na"
    ECHO A | %z7% e "%%~Fa" -o"%ExtractDir%\%%~Na" -r
    %z7% a "%%~Fa" "%ExtractDir%\%%~Na\*" -aoa
    RD /Q /S "%ExtractDir%\%%~Na"
)
PAUSE
EXIT

Please note the ECHO A | in front of the %z7% e ~ command to tell it to overwrite any existing files that exist in the out directory rather than waiting on you to tell it to do so, etc.

enter image description here

Also, please note the use of the -aoa switch at the end of the %z7% a ~ command which tells it to overwrite all existing files within the zip file you are zipping to replacing all original files within it without actually deleting the entire original zip file beforehand which is very simple to add to this script if needed by one additional line of IF EXIST "%%~Fa" DEL /Q /F "%%~Fa".


Further Resources

  • Can echo A | %z7% e be replaced with -y option to 7-Zip? – Alexey Ivanov Mar 12 '18 at 16:03
  • This appears to keep the ZIP comment... is there a way to remove them? – Julian Lachniet Mar 24 '18 at 14:34
  • @JulianLachniet I think just above the line in the loop of %z7% a "%%~Fa" "%ExtractDir%\%%~Na\*" -aoa you will want to delete the original file first so add IF EXIST "%%~Fa" DEL /Q /F "%%~Fa" in that line above the other line. Then you will not need to use the -aoa switch in the next line. Let me know if that makes sense or if you need more clarification and whether or not that resolves the comment thing. This way the original zip file is deleted and then recreated rather than updated. Give it a shot with a test zip first to confirm but I think that's all you need to do. – Pimp Juice IT Mar 24 '18 at 21:28
  • @JulianLachniet Here's a link to the new format just to clarify with precision for you what I'm suggesting but please let me know how it goes: textuploader.com/dgvgt – Pimp Juice IT Mar 24 '18 at 21:30
4

Here's a simple script for regular Command Prompt (cmd.exe) that can do this for you:

set ZIP7="C:\Program Files\7-Zip\7z.exe"

for %z in (*.zip) do (
    mkdir tmp
    cd tmp
    %ZIP7% x ..\%z
    %ZIP7% a ..\%z.7z -r -mx9 *
    cd ..
    rmdir /s /q tmp
)

First, ZIP7 variable is defined with the path to 7z.exe. Then for iterates over all zip-files in the current directory. Each zip file is extracted into tmp directory and then recompressed with 7-Zip using maximum compression (-mx9 option).

You can put these commands in a batch file. In this case, use double % in the for-loop variable. That is, use %%z instead of %z.

1

The ReZip freeware will do the job: zipada55-bin-win32.zip. To begin with, the rezip -comp -int archive.zip command will do a fair job without further installation. For an even better compression, you'll need to download zip, 7z, kzip, advzip and run ReZip without the -int option. Full set of command line options are:

Options:  -defl     : repack archive only with the Deflate
                        subformat (most compatible)
          -fast_dec : repack archive only with fast decompressing subformats
          -int      : use internal Zip-Ada algorithms only, no external call
          -touch    : set time stamps to now
          -lower    : set full file names to lower case
          -del_comm : delete comment
          -comp     : compare original and repacked archives (paranoid mode)
          -rs=n     : loop many times over a single compression approach
                        having randomization, and keep optimum when size is
                        stable after n attempts in a row
0

Write a PowerShell script that finds all zip files on your hard drive, for each zip archive found , extract all files and compress them again in the desired format.

You can either use builtin ZIP functions or call 7zip command line tools from PowerShell.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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