Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Silverlight 4 – Displaying a Bunch of Files from a Zip File

Blogs

Mike Taulty's Blog

Elsewhere

One of the things I often find when publishing blog posts is that I want to display a bunch of files. For instance – in talking about WPF or Silverlight you might be trying to show something that involves a XAML file and a couple of code files and it can be a bit painful if you have to do that with 3 separate lumps of embedded text inside of a blog post.

I was thinking about that problem a bit more generally and I wondered how I could do something such that I’d have a control that I could just drop into a blog post in order to display a number of files. Almost like a mini-explorer view across a set of files.

I figured that I could use Silverlight for this and then I pondered a little on how I’d want to file-set packaged in order that it’d be as little trouble as possible to create one of these plug-ins. I decided on Zip.

There are a few zip libraries out there for .NET but I settled on using the DotNetZip project from CodePlex which I brought down in source form and hacked to work with Silverlight.

When I say “hacked” I do mean hacked. There’s one key sticking point in using that library in that it looks to rely on a particular IBM437 codepade which seems to be prevalent in zip files. However, Silverlight supports UTF8 and Unicode and so what I ended up doing was hacking the library so that it only deals with UTF8. This may well mean that my code doesn’t deal with a bunch of ZIP files. I’m hoping that the original library writers might try and do a Silverlight version and then maybe I can just reference that rather than hacking around in code that I didn’t want to get too involved in :-)

Because of using DotNetZip, the rest of the code here is licensed under the Microsoft Public License so please bear that in mind.

Anyway, I took DotNetZip and turned it into a little Silverlight application. The idea is that you put the XAP for this onto a website next to some zip files. By default it looks for Content.zip but that can be changed by using initParams when the application is run as in the example below which points the application at its own XAP file;

image

Here’s the application in use to display its own source code;

A note – the application is rubbish at displaying anything other than a text file right now so take care if you click on a binary file as the app’s likely to blow up.

I built this with Silverlight 4 and Visual Studio 2010 Beta 2. I also tried to be a little more structured than for some of the code that gets thrown onto this blog :-) and I also used MEF in order to plug things together which worked out pretty well for me. I also make use of the Silverlight Toolkit for theming support and one or two other pieces.

I could use MEF in order to add different viewers based on file extensions – wouldn’t be too difficult to use the PackageCatalog and download additional plug-ins that knew how to view image files etc. Equally, syntax-colouring would be another area for add-ins to plug in.

Along the way I encountered a few hitches that had to be worked around but nothing too serious. I had one or two disagreement with the TreeView in that I didn’t seem to be able to;

  1. Nicely databind its SelectedItem
  2. Nicely databind its ItemsSource in that you have to jump through a hoop or two creating TabItem that other items controls don’t ask you to do

but, here’s the source-code for conventional download if you want to take a look at it outside of the application above.


Posted Mon, Jan 11 2010 3:48 AM by mtaulty
Filed under: ,

Comments

Philipp Sumi wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Mon, Jan 11 2010 4:51 AM

Nice sample! I'm currently working on a complete abstraction of file systems (http://vfs.codeplex.com), and ZIP file support is on my list, too. VFS might allow the same by either putting a ZipFileSystem provider directly into the SL application (and processing the file in SL), or putting the file system provider on the server and accessing it remotely (which would allow browsing the ZIP's contents and downloading individual files)

I'm sure your sample will be a good starting point when getting at implementing the ZIP support - thanks!

uberVU - social comments wrote Social comments and analytics for this post
on Mon, Jan 11 2010 7:30 AM

This post was mentioned on Twitter by mtaulty: blog: viewing the contents of a ZIP file on the web server from a Silverlight ( 4 ) application http://bit.ly/7S3z4y

Morten wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Mon, Jan 11 2010 3:02 PM

I described something similar, but a lot more generic and without the need for a 3rd party library. It's basically 50 lines of code and supports most zip files and file contents.

See my blogpost here: www.sharpgis.net/.../REALLY-small-unzip-utility-for-Silverlight.aspx

Mike Taulty's Blog wrote Silverlight 4 Beta & MEF – Experimenting with an alternative Programming Model
on Mon, Jan 11 2010 4:43 PM

One of the things that seems hard-baked into MEF if you watch the Silverlight 4 and MEF screencasts that

progg.ru wrote Silverlight 4 : отображение списка файлов в Zip-архиве
on Tue, Jan 12 2010 12:46 AM

Thank you for submitting this cool story - Trackback from progg.ru

mtaulty wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Tue, Jan 12 2010 3:40 AM

Morten - thanks for the link. I'd _wondered_ whether it was possible to use Silverlight's ZIP reading capabilities to avoid having to use an external library but I'd stopped at the point where I realised I couldn't get the file names but, clearly, you made that work with a lot less code than I'm bringing in here in the form of the ZIP library.

I might revisit the code and pick up your code rather than the ZIP stuff - better solution if I can make that work out.

Thanks for the info!

Mike.

Graeme wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Tue, Jan 12 2010 6:07 AM

Bit off-topic, but how can you make a SL app stay full screen? As soon as I click another window on a different monitor full-screen mode stops. Very irritating when watching your videos.

vikas wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Tue, Jan 12 2010 6:40 AM

Thanks this will be very helpful .

Blog de Oskar Alvarez wrote Enlaces Interesantes Silverlight/WPF 13-01-2010
on Tue, Jan 12 2010 10:38 PM

Los del día Extensibility Series – WPF & Silverlight Design-Time Code Sharing –

Jax wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Tue, Jan 12 2010 10:40 PM
mtaulty wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Wed, Jan 13 2010 1:22 AM

Jax - thanks, I took a lok at SLSharpLib but avoided it because it's under GPL.

Mike.

mtaulty wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Wed, Jan 13 2010 1:26 AM

Graeme - on the "full screen" question. Silverlight applications running full-screen are limited around their keyboard/mouse interactions for security reasons. So, the limitation you're seeing with the full screen application is by design.

A Silverlight 4 application can go full-screen without those security limitations if that application marks itself as requiring elevation at installation time ( i.e. it's a trusted application ) and in that case the security sandbox is relaxed and one of the things that enables is full-screen apps without those security restrictions.

Mike.

Arun Mahendrakar wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Fri, Jan 15 2010 2:57 AM

Hi Mike,

I've written a similar application - XapReflector and blogged about it here:

weblogs.asp.net/.../xap-reflector-silverlight-4.aspx

May be useful to someone.

Arun

Ola Karlsson wrote re: Silverlight 4 – Displaying a Bunch of Files from a Zip File
on Mon, Jan 25 2010 4:56 AM

I've not actually tried it but for comparing, here's another blogpost with code, to list the content of a zip archive blogs.msdn.com/.../reading-zip-files-from-silverlight.aspx

Cheers,

Ola