Pablo Galiano : Thursday, July 03, 2008 - Posts

Subscriptions

<September 2010>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

News

Subscribe to Pablo Galiano by Email

Post Categories

Thursday, July 03, 2008 - Posts

Zip and unzip with VS

 

Visual Studio ships with an assembly to compress and decompress files. This assembly is used in the Export Template feature for example.

The assembly is Microsoft.VisualStudio.Zip.9.0.dll and its located in the GAC.

The two main classes that we need to use are ZipFileCompressor and ZipFileDecompressor.

The API is really simple:

 

To compress a file:

 

    var pathRoot = @"C:\MyPath";

    var files = Directory.GetFiles(pathRoot, "*.*", SearchOption.AllDirectories);

 

    files = files.Select(file => file.Replace(pathRoot, string.Empty)).ToArray();

 

    // This doesn't follow any design guidelines, but all the logic for compressing a list of files

    // is in the constructor, so the only thing that we need to do to compress is to create an

    // instance of ZipFileCompressor.

    var comp =

        newZipFileCompressor(@"C:\Foo.zip", pathRoot, files, true);

 

To decompress a file:

 

    var decomp = newZipFileDecompressor(@"C:\Foo.zip");

 

    decomp.UncompressToFolder(@"C:\Foo", true);

 

 

Just zip it,

Pablo

posted Thursday, July 03, 2008 11:32 AM by pga with 2 Comments