Pablo Galiano : Zip and unzip with VS

Subscriptions

<December 2008>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

News

Subscribe to Pablo Galiano by Email

Post Categories

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 on Thursday, July 03, 2008 11:32 AM by pga

What do you think?

(required) 
required 
optional
required