Increasing the compression ratio of the XAP files

by XAML Team | Comments 2

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

Valeri Hristov
Team Lead,
Platform Team

2 Comments

tomo
On my production XAP which is 3.471.666 I managed to make it 3.282.664 so the difference is small.
It's possible to make it around 2.400.000 but unfortunatelly it seems that LZMA cannot be decompressed and it doesn't work. Op system is WinXP.
Valeri Hristov
The Silverlight 4 Tools already use pretty good compression of the produced XAP files, e.g. this technique is no longer relevant (it was when the current version of Silverlight was 2).

You could try our assembly minifier if you are using few Telerik controls in your application:
http://www.telerik.com/products/silverlight/getting-started/assembly-minifier.aspx

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by XAML Team - or - read latest articles in Developer Tools