Decompress from Zip and recompress with 7Zip

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Decompress from Zip and recompress with 7Zip

#1 Post by darioit » 25 Oct 2011 07:56

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

djangofan
Posts: 23
Joined: 04 Nov 2011 12:28

Re: Decompress from Zip and recompress with 7Zip

#2 Post by djangofan » 04 Nov 2011 13:26

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Decompress from Zip and recompress with 7Zip

#3 Post by Ed Dyreen » 05 Nov 2011 02:18

'
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:

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Decompress from Zip and recompress with 7Zip

#4 Post by darioit » 08 Nov 2011 03:17

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Decompress from Zip and recompress with 7Zip

#5 Post by darioit » 08 Nov 2011 07:36

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Decompress from Zip and recompress with 7Zip

#6 Post by darioit » 08 Nov 2011 08:14

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Decompress from Zip and recompress with 7Zip

#7 Post by darioit » 09 Nov 2011 01:31

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Decompress from Zip and recompress with 7Zip

#8 Post by Ed Dyreen » 09 Nov 2011 01:53

'
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'
      )
   )
)

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Decompress from Zip and recompress with 7Zip

#9 Post by darioit » 09 Nov 2011 04:08

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Decompress from Zip and recompress with 7Zip

#10 Post by darioit » 09 Nov 2011 04:18

something is wrong the error is "is not recognized as an internal or external command,operable program or batch file."

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Decompress from Zip and recompress with 7Zip

#11 Post by darioit » 09 Nov 2011 04:28

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'
      )
   )
)

Post Reply