Page 1 of 1

Decompress from Zip and recompress with 7Zip

Posted: 25 Oct 2011 07:56
by darioit
Hello everybody,

I have a new milestone for you
On my server I have a lot of Zip file (all pdf and txt file inside), and I like to recompress using 7zip algorithm

For example 654 MB txt becomes 108 MB with zip (16,51%) and 10,9 MB with 7zip (only 0,016% amazing)

Who can help me to write this script:
Forfiles find D:\ /s *.zip (unzip) and compress again using same name with 7zip software

Regards
Dario

Re: Decompress from Zip and recompress with 7Zip

Posted: 04 Nov 2011 13:26
by djangofan
Here is a script i use for 7-zipping

Code: Select all

TITLE Backup "reports" Directory
@echo off
COLOR 06
echo.
echo Run this from within the reports directory.
echo For xmlfile.xml just edit this batch file
echo.
@echo on
COLOR 11
:: this next line would backup all data, including .jpg files
:: "C:\Program Files\7-Zip\7z.exe" a -r -tzip -y -xr!?*.jpg reports.zip xmlfile.*.xml
"C:\Program Files\7-Zip\7z.exe" a -r -tzip -y reports.zip xmlfile.xml
pause>nul

Re: Decompress from Zip and recompress with 7Zip

Posted: 05 Nov 2011 02:18
by Ed Dyreen
'
Once upon a time I wrote a batch that would compress a folder with several different compressors, and then select the winner based upon filesize. The winner is usually 7zip and occasionally UHA.
.rar, .ace, .zip, .cab score terrible on compression but are still usefull.

Thanx Linux for exporting 7zip to us :mrgreen:

Re: Decompress from Zip and recompress with 7Zip

Posted: 08 Nov 2011 03:17
by darioit
Hello,

This is my script, any suggestion?

Code: Select all

FOR /F "delims=" %%i in ('DIR /b *.zip') DO wzunzip %%~dpnxi %%~dpni & call Routine_Backup_7z.bat %%~dpi%%i.7z %%~dpni & rmdir /s /q %%~ni


Routine_Backup_7z.bat:

Code: Select all

SET SevenZip=7za a -t7z -m0=LZMA -mx9
%SevenZip% %1 %2


Regards
Dario

Re: Decompress from Zip and recompress with 7Zip

Posted: 08 Nov 2011 07:36
by darioit
This is the last version full running, including delete directory and old zip file

Code: Select all

@Echo off

SET SevenZip=7za a -t7z -m0=LZMA -mx9 -mmt=2

For /F "delims=" %%i in ('dir /b *.zip') DO wzunzip %%~dpnxi %%~dpni & %SevenZip% %%~dpni.7z %%~dpni\* & rmdir /s /q %%~ni & del /q %%~nxi


Any suggestion to improve?

Regards
Dario

Re: Decompress from Zip and recompress with 7Zip

Posted: 08 Nov 2011 08:14
by darioit
This script works only if is execute in the same zip directory

I like to run like this "go.bat path", but takes some error

Regards

Re: Decompress from Zip and recompress with 7Zip

Posted: 09 Nov 2011 01:31
by darioit
Script update, now check also the error code


Code: Select all

@Echo off

SET SevenZip=7za a -t7z -m0=LZMA -mx9 -mmt=2

FOR /F "delims=" %%i in ('DIR /b *.zip') DO wzunzip %%~dpnxi %%~dpni > nul && %SevenZip% %%~dpni.7z %%~dpni\* && rmdir /s /q %%~ni && del /q %%~nxi & echo %errorlevel% || Echo ERROR FILE %%~dpnxi >> ERROR_LOG.TXT

Re: Decompress from Zip and recompress with 7Zip

Posted: 09 Nov 2011 01:53
by Ed Dyreen
'
you are wrong, that code does not check the errorlevel, it checks whether echo fails displaying it, and echo doesn't fail !

Code: Select all

& echo %errorlevel% || Echo ERROR FILE %%~dpnxi >> ERROR_LOG.TXT 
What you want is this:

Code: Select all


@echo off &setlocal enabledelayedexpansion

2>> "ERROR_LOG.TXT" (
   for /f "delims=" %%? in (
      'DIR /b *.zip'
   ) do (
      set /a $error = 0
      >nul wzunzip "%%~dpnx?" "%%~dpn?" ||(
         set /a $error = !errorlevel!
         >&2 echo.wzunzip !$error! FILE '%%~dpnxi'
      )
      if !$error! equ 0 %SevenZip% "%%~dpn?.7z" "%%~dpn?\*" ||(
         set /a $error = !errorlevel!
         >&2 echo.%SevenZip% !$error! FILE '%%~dpnxi'
      )
      if !$error! equ 0 rd /s /q "%%~n?" ||(
         set /a $error = !errorlevel!
         >&2 echo.rd !$error! FILE '%%~dpnxi'
      )
      if !$error! equ 0 del /q "%%~nx?" ||(
         set /a $error = !errorlevel!
         >&2 echo.del !$error! FILE '%%~dpnxi'
      )
   )
)

Re: Decompress from Zip and recompress with 7Zip

Posted: 09 Nov 2011 04:08
by darioit
Wow this is a serious script, I'll check asap on all my zip file.

I see it fails "my script" when a name is with blank inside like this : "file name.zip"

Regards

Re: Decompress from Zip and recompress with 7Zip

Posted: 09 Nov 2011 04:18
by darioit
something is wrong the error is "is not recognized as an internal or external command,operable program or batch file."

Re: Decompress from Zip and recompress with 7Zip

Posted: 09 Nov 2011 04:28
by darioit
Now is perfect, and works also with space in zip name file

Code: Select all

@echo off &setlocal enabledelayedexpansion
SET SevenZip=7za a -t7z -m0=LZMA -mx9 -mmt=2

2>> "ERROR_LOG.TXT" (
   for /f "delims=" %%p in (
      'DIR /b *.zip'
   ) do (
      set /a $error = 0
      >nul wzunzip "%%~dpnxp" "%%~dpnp" ||(
         set /a $error = !errorlevel!
         >&2 echo.wzunzip !$error! FILE '%%~dpnxp'
      )
      if !$error! equ 0 %SevenZip% "%%~dpnp.7z" "%%~dpnp\*" ||(
         set /a $error = !errorlevel!
         >&2 echo.%SevenZip% !$error! FILE '%%~dpnxp'
      )
      if !$error! equ 0 rd /s /q "%%~np" ||(
         set /a $error = !errorlevel!
         >&2 echo.rd !$error! FILE '%%~dpnxp'
      )
      if !$error! equ 0 del /q "%%~nxp" ||(
         set /a $error = !errorlevel!
         >&2 echo.del !$error! FILE '%%~dpnxp'
      )
   )
)