Quantcast
Channel: whileloop » code
Viewing all articles
Browse latest Browse all 10

use CAB files in C# and .NET

$
0
0

An often overlooked element of working in C# and .NET is the ability to work with CAB files. These are very useful compressed files, which can help you immensely when you are deploying applications and need to move a large volume of files. They are EXTREMELY simple to use. Firstly, make sure you reference the library in your project (Microsoft.Deployment.Compression.Cab) and then you can easily use them in on a couple of lines. My example is actually overly verbose in the number of lines it uses in order to maintain clarity.

 
//Create cab files 
var pathToContainingFolder = @"C:\Desktop";
var cabFile = Path.Combine(pathToContainingFolder, + @"cabinet.cab");
var cabToPack = new CabInfo(cabFile);
var sourceDir = @"C:\Desktop\stuff";
cabToPack.Pack(sourceDir, true, Microsoft.Deployment.Compression.CompressionLevel.Min, null);

And there we have a compressed CAB file ready to use.

On the other end, extraction is equally straight-forward:

// extract the CAB file to the specified directory
var destDir = @"C:\Desktop\cabDemo";
EnsureDirectoryExists(destDir);
var cabToUnpack = new CabInfo(cabFile);
cabToUnpack.Unpack(destDir);

the above sample uses my little helper EnsureDirectoryExists(string dir):

private static void EnsureDirectoryExists(string dir) {	
	if (!Directory.Exists(dir)) {
		Directory.CreateDirectory(dir);
	}
}


Viewing all articles
Browse latest Browse all 10

Trending Articles