Jul 5, 2011

Creating a .cab file with C#


It is Microsoft DTF that comes with Wix 3.0  Download wix3.0 and Install it, then you will have new dll  Microsoft.Deployment.Compression and Microsoft.Deployment.Compression.Cab
 in Project>>Add Refernce>>.net tab, add reference to those dll 's in your application .
In your code"Microsoft.Deployment.Compression.CompressionLevel.Max, EventHandler);" , it is not logic here not only because it's not a legal sentence expecially lack of '(', also it's to Pack files, not Unpack.
//Pack files should be like this:
CabInfo cab = new CabInfo(@"C:\Cabinet2.cab");
cab.Pack(@"C:\folder", true, Microsoft.Deployment.Compression.CompressionLevel.Max, null);
//Unpack a cab file into C:\Unpacked folder :cab.Unpack(@"C:\Unpacked");   

or



At present we do not have classes in .NET that enable us to create CAB (cabinet) files, hence we has other third party libraries and toolsets that provide their custom classes, one such is Windows Installer XML (WiX) toolset (http://wix.sourceforge.net) which is distributed with a collection of dlls as part of its SDK and best of all is that this is developed by Microsoft and we have good support for WiX users.

In your .NET project add references to the following DLLs:
Microsoft.Deployment.Compression.dll
Microsoft.Deployment.Compression.Cab.dll
And we will be using this library (Microsoft.Deployment.Compression.Cab) for creating CAB (cabinet) files.


using Microsoft.Deployment.Compression.Cab;


In our example we will see how to add individual files to the CAB file and how to add a folder (directory) to the CAB file and finally how to extract (unpack) the CAB file.





// CREATING CAB FILE BY ADDING LIST OF FILES
CabInfo cab = new CabInfo(@"C:\testarchive1.cab");
List files = new List();
files.Add(@"C:\test1.txt");
files.Add(@"C:\test2.txt");
files.Add(@"C:\test3.txt");
cab.PackFiles(null,files,null);

// CREATING CAB FILE BY ADDING FOLDER (WITH SUB-FOLDERS) USING MINIMUM COMPRESSION
cab = new CabInfo(@"C:\testarchive2.cab");
cab.Pack(@"C:\Balaji", true, Microsoft.Deployment.Compression.CompressionLevel.Min, null);

// EXTRACTING (UNPACKING) FILES FROM CAB FILE
cab = new CabInfo(@"C:\testarchive1.cab");
cab.Unpack(@"C:\ArchieveDir");

the location for dlls after installation is

C:\Program Files\Windows Installer XML v3.5\SDK



Another dll that also comes with WiX toolset SDK isMicrosoft.Deployment.Compression.Zip.dll that provides similar functionality for packing and unpacking zip files.




No comments:

Post a Comment