You are here

Compress exe/dll/jar Script

15 posts / 0 new
Last post
John Bentley
John Bentley's picture
Offline
Last seen: 13 years 5 months ago
Developer
Joined: 2006-01-24 13:26
Compress exe/dll/jar Script

I was wondering when will we get the script that compresses exes and dlls?

arqbrulo
arqbrulo's picture
Offline
Last seen: 2 years 11 months ago
Joined: 2006-08-10 16:38
UPX/7-zip

What you are looking for is UPX for your exe's and your dll's. JAR files are just zip files (only the extension changes). Therefore, you can use 7-Zip for that. Just un zip, and re-zip using higher compression. Once you are finished change the extension from zip (or 7z) to jar.
---------------
Marge: Homer, the plant called. They said if you don't show up tomorrow don't bother showing up on Monday.
Homer: Woo-hoo. Four-day weekend.

"In three words I can sum up everything I've learned about life: it goes on." -- Robert Frost
"In three words I can sum up everything I've learned about life: baby ain't mine." -- Adam Holguin

ZachHudock
ZachHudock's picture
Offline
Last seen: 4 weeks 1 day ago
Developer
Joined: 2006-12-06 18:07
I believe he was asking

I believe he was asking about the script to compress everything that John mentioned under the Development link. Unzipping and recompressing jar files one-by-one takes a rediculous amount of time

The developer formerly known as ZGitRDun8705

arqbrulo
arqbrulo's picture
Offline
Last seen: 2 years 11 months ago
Joined: 2006-08-10 16:38
Ooohhhh!!!!

My apologies, I just have never bothered looking at the dev. page. But now I know what he means.
---------------
Marge: Homer, the plant called. They said if you don't show up tomorrow don't bother showing up on Monday.
Homer: Woo-hoo. Four-day weekend.

"In three words I can sum up everything I've learned about life: it goes on." -- Robert Frost
"In three words I can sum up everything I've learned about life: baby ain't mine." -- Adam Holguin

Ryan McCue
Ryan McCue's picture
Offline
Last seen: 13 years 4 months ago
Joined: 2006-01-06 21:27
I was planning

On asking John about that if he ever emails me back Sad
(I have a sneaking suspicion that he's on holidays again)
----
Ryan McCue
Person 1: Oh my god. You will never believe what just happened.
Person 2: What?
Person 1: I can't remember, I've forgotten.

"If you're not part of the solution, you're part of the precipitate."

Dagenham
Dagenham's picture
Offline
Last seen: 3 weeks 4 days ago
Joined: 2007-03-23 06:19
Simple batch

You can use this simple batch file for the .JAR file(s):

@echo off

rem J2Z.BAT
rem Quick'n dirty solution to repack a JAR file with 7zip.
rem The one and only parameter is the JAR file name without extension.
rem The JAR file itself will be overwritten with the new ZIP file.
rem You can use with the following command line or batch:
rem for %%a in (*.jar) do call j2z %%~na
rem to compress all .JAR files in a directory.

rem 7zip executable existense check (set up with your path)
set _7zp=7z.exe
if not exist %_7zp% goto no7zp

rem JAR file existense check
set _jar=%1.jar
if not exist %_jar% goto nojar

rem temp directory existense check
set _tmp=%temp%
if not exist %_tmp% goto notmp

rem we shall need a work directory
if exist %_tmp%\%1 rd /q /s %_tmp%\%1
md %_tmp%\%1

rem extracting JAR file to work directory
%_7zp% x %_jar% -o%_tmp%\%1\

rem repacking JAR file with 7zip
%_7zp% a -mx=9 -r -tzip %_jar% %_tmp%\%1\*.*

rem purge work directory
rd /q /s %_tmp%\%1

rem finished for now
goto end

rem no 7zip executable found
:no7zp
echo 7zip executable (%_7zp%) not found.
pause
goto end

rem no JAR file found
:nojar
echo JAR file (%_jar%) not found.
pause
goto end

rem no temp directory found
:notemp
echo Temp directory (%_tmp%) not found.
pause
goto end

rem this is the end, my friend
:end
set _7zp=
set _jar=
set _tmp=

For .EXE and .DLL files, you can use the following command:

upx *.exe *.dll

Some official recursive stuff would be a lot more more fun, of course :).

BuddhaChu
BuddhaChu's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-11-18 10:26
You could try using the FOR

You could try using the FOR command like in Ryan's UPX example and put all the commands on one line separated with the ampersand

build work dir

(begin FOR_loop) extract & repack & flush_work_dir (end_FOR_loop)

remove work dir

No need to build/remove the work dir for every jar file, (unless it's easier to code that way of course)

I'll look into this whole thing more tonight if I get some time.

Cancer Survivors -- Remember the fight, celebrate the victory!
Help control the rugrat population -- have yourself spayed or neutered!

BuddhaChu
BuddhaChu's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-11-18 10:26
Here's what I did with your

Here's what I did with your script to make it recursive and not have to use a batch to call another batch.

Take it for what it's worth since it's "unofficial". Wink

Throw Ryan's commandline for UPXing executables and you've got the start to a decent all-in-one application compressing batch file.

@echo off

:: J2Z.BAT
:: Quick'n dirty solution to repack a JAR file with 7-Zip.
:: The JAR file itself will be overwritten with the new compressed JAR file.

:: 7zip executable existence check (set your path to 7z.exe here)
set _7zp="c:\Program Files\7-Zip\7z.exe"
if not exist %_7zp% goto no7zp

:: temp directory existence check
set _tmp=%temp%
if not exist %_tmp% goto notmp

for /r %%e in (*.jar) do (if exist %_tmp%\%%~nxe (rd /q /s %_tmp%\%%~nxe) & md %_tmp%\%%~nxe) & (%_7zp% x %%e -o%_tmp%\%%~nxe\ & %_7zp% a -mx=9 -r -tzip %%e %_tmp%\%%~nxe\*.*) & (rd /q /s %_tmp%\%%~nxe)

goto end :: finished for now

:no7zp :: no 7-Zip executable found
echo 7zip executable (%_7zp%) not foun.d & pause & goto end

:notemp :: no temp directory found
echo Temp directory (%_tmp%) not found. & pause & goto end

:end :: this is the end, my friend
set _7zp=
set _tmp=

Cancer Survivors -- Remember the fight, celebrate the victory!
Help control the rugrat population -- have yourself spayed or neutered!

Dagenham
Dagenham's picture
Offline
Last seen: 3 weeks 4 days ago
Joined: 2007-03-23 06:19
Wow

Awesome, man :).
With to more lines after the core loop, you can have the universal compressor:

for /r %%e in (*.dll) do %_upx% --best --color --compress-icons=0 -v %%e
for /r %%e in (*.exe) do %_upx% --best --color --compress-icons=0 -v %%e
BuddhaChu
BuddhaChu's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-11-18 10:26
That needs some cleanup

%_upx% needs to be defined
You can combine those two lines into one (no need to loop thru the files twice)
Those aren't the portableapps.com "recommended" switches for UPX

set _upx="c:\upx\upx.exe"  :: set upx.exe location ex: c:\upx\upx.exe
for /r %%e in (*.exe,*.dll) do %_upx% "%%e" --best --compress-icons=0 --crp-ms=999999
set _upx=

You could add in the lines for "if not exists" for upx.exe like the original batch file above did for 7zip if you want it to be more fancy.

Cancer Survivors -- Remember the fight, celebrate the victory!
Help control the rugrat population -- have yourself spayed or neutered!

Bahamut
Bahamut's picture
Offline
Last seen: 11 years 1 month ago
Joined: 2006-04-07 08:44
That's a bit messy. It would

That's a bit messy. It would be nice to have upx process all dll/exe files in a directory, or (even better) have upx process everything in a single step. You could use for to create filenames instead of having it simply pass one file to upx at a time. Have for pass directory names as parts of arguments passed to upx. The output looks much better (and upx will calculate total savings, which is quite nice).

Vintage!

chezduong
Offline
Last seen: 6 months 1 week ago
Joined: 2006-12-06 04:01
For loop bug?

Sorry to dig this old thread up, but I was trying to create a little program to do this based upon the script above when I ran into a problem.

Sometimes, when I use the "for" loop, some files get inserted twice. UPX, 7-zip and optipng all try to compress the files twice when this happens. I think that this has something to do with the for loop in the batch command, because it happens with all three exe's.

Basically, the first time the file is UPX'ed and/or optipng'ed correctly as usual. A few files later, I see that the batch loop tries to recrompress one or two of the files again -- and of course it fails. This does not really bother me except that when I am re-jarring, this could take some time -- and sometimes the re-jar goes into an endless loop of re-jar's.

Has anyone else had any problems like this? I have tried and tried again and this behavior looks completely random (I am sure that it is not, but it looks random).

If this is a known problem, I would love to know how to solve it. Otherwise, Bahamut, I would love to know the batch command to do what you wrote just above.

Thanks everyone. CD

John T. Haller
John T. Haller's picture
Online
Last seen: 1 min 53 sec ago
AdminDeveloperModeratorTranslator
Joined: 2005-11-28 22:21
AppCompactor

These scripts are no longer necessary. Just use the PortableApps.com AppCompactor:
https://portableapps.com/apps/utilities/appcompactor

Sometimes, the impossible can become possible, if you're awesome!

chezduong
Offline
Last seen: 6 months 1 week ago
Joined: 2006-12-06 04:01
I do use it

I do use AppCompactor. I am doing this to learn more about dos batch scripts, autohotkey and programming in general. Call it personal development. Smile

BuddhaChu
BuddhaChu's picture
Offline
Last seen: 6 years 3 months ago
Joined: 2006-11-18 10:26
Found a typo

I'm re-doing all my scripts and I found a typo in the original JAR recompression script posted to this thread and the one I did later that uses recursion.

This line:

if not exist %_tmp% goto notmp

Needs to be changed to:

if not exist %_tmp% goto notemp

Cancer Survivors -- Remember the fight, celebrate the victory!
Help control the rugrat population -- have yourself spayed or neutered!

Log in or register to post comments