How to recompress many zips on a drive?

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

  • 7Zip
    • -ao (Overwrite mode) switch
    • 7Zip -m (Set compression Method) switch
  • For /F
  • For /?

    %~fI        - expands %I to a fully qualified path name
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    
  • Dir


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.


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