3

I am writing a plugin for a .NET based CMS and I want to unpack a tree of javascript, CSS and image files when it runs for the first time. Because I want to ship just a single dll, I was going to zip up those directories and store them as a resource. Then when the plugin runs, it checks to see if the requisite files exist, and if not, it will unpack them.

I know resources are "flat", in that you can't set up a directory structure, so some form of packing is the only way I can see to do it.

The question is, firstly, is this an acceptable thing to do and secondly, are there any libraries built into .NET to allow me to do this? I am thinking I may have to use GZip rather than Zip if I want to use built-ins, but is there a preferred way to do what I'm trying to achieve?


Just to add something to the question - I'm aware that a common scheme is to use .tar.gz, which I'm currently doing at the moment. But for some reason this strikes me as sub-optimal, since I have to import external libraries in order to work like this. I figured this might be a common enough thing to do that there'd be a one or two-liner way to do it natively.

Iain Fraser
  • 6,578
  • 8
  • 43
  • 68
  • 1
    "Answers in the form of links to articles are fine"... no, they are not :-) Answers on Stack Overflow should always be more than a link. – Eric J. Oct 30 '12 at 00:00
  • True. I was being nice because I figured my question might have been irritatingly fundamental - just I haven't worked with resources in this way before. - There we go, edited :) – Iain Fraser Oct 30 '12 at 00:02
  • 1
    not really an answer but a hint in a similar post: http://stackoverflow.com/questions/10677405/how-to-handle-unzipping-zipfile-with-paths-that-are-too-long-duplicate – Jester Oct 30 '12 at 00:27
  • Make sure you have write access to file system at runtime (If it would be me your code will not not have write access to images/css locations). – Alexei Levenkov Oct 30 '12 at 00:54
  • Thanks for the tip Alexei. Yes, my plugin writes to the Assets directory of the CMS, which is where you put files like the ones I intend to extract. – Iain Fraser Oct 30 '12 at 00:56

1 Answers1

3

There are a couple of ways to package and compress groups of files shipped with .net http://msdn.microsoft.com/en-us/library/system.io.packaging.aspx (release in .net 3.0 i think) and http://msdn.microsoft.com/en-us/library/system.io.compression.aspx (new in .Net 4.5)

There are also a couple of free open source libraries you can use (Sharp Zip Lib: http://www.icsharpcode.net/opensource/sharpziplib/ and DotNetZip lib http://dotnetzip.codeplex.com/). There are probably others. I've used them both as well as the Packaging apis. I favor the DotNetZip for its ease of use, but you can be productive with any of them pretty quickly.

It makes sense to compress the resources - that's smart use of space - but your resources don't have to directly map to how/where you are going to extract them (since you write the code to extract and write the resources you can store them flat if it makes more sense).

Jason Haley
  • 3,770
  • 18
  • 22
  • Thanks Jason, I'm actually looking at sharpziplib at the moment :). System.IO.Packaging might be just what I'm looking for also. I'll try it out and let you know how I go. – Iain Fraser Oct 30 '12 at 00:34
  • I currently use the System.IO.Packaging on a project and it works good enough. The new .net 4.5 stuff looks even better but I can't upgrade to it yet. Good luck. – Jason Haley Oct 30 '12 at 00:37