Telerik blogs

Today I was hacking around with a Silverlight application and decided to check if it is possible to recompress its XAP file in order to save some bytes. As you might already know, the XAP files are just renamed ZIP files and you can open and view their contents with almost any archiver.

I created a simple batch file that extracts an archive to a temp folder, deletes it and then compresses the extracted files into a new archive with the same name as the original (I am using the open source archiver 7-Zip, which was installed in its default location):

@if exist %temp%\%2 rd /s /q %temp%\%2
@"c:\program files\7-zip\7z.exe" x -r -o%temp%\%2\ %2 *.* 
@del /q %2
@"c:\program files\7-zip\7z.exe" a -r -tzip -mx%1 %2 %temp%\%2\*.*

If you name the file recompress.bat, you could use it the following way:

recompress N SilverlightApplication1.xap

where N specifies the desired compression ratio and could be 1, 3, 5, 7 or 9. Note that in order to use the command line above, you should copy the batch file to the folder, where the application XAP file is located, usually <Project Root>\ClientBin.

For testing I used our online demo, which has a 3.2MB XAP file:

Compression Ratio

1 and 3

5

7

9 (Max)

Compression Speed

Very Fast

Fast

Slow

Slower

Archive Size

2253kB

2184kB

2164kB

2155kB

The batch file operation reduced the original XAP file size with more than a megabyte with any compression ratio! Since the time to recompress with most ratios was less than a second, I decided to create a simple post-build command to process my application XAP file after each build. To use it copy (or move) the recompress.bat file into your project root folder and enter the following text in the “Post-build event command line” text box in the Build Events property pages of your Silverlight application:

pushd clientbin
..\recompress 3 $(projectname).xap
popd

I hope this helps :)


About the Author

Valeri Hristov

Team Lead,
Platform Team

Comments

Comments are disabled in preview mode.