C#/.NET: Creating a CAB and adding files to it without an external library
Asked Answered
G

2

5

I'm aware there is a similar question but the marked answer provides a link to an external library which requires several dependencies to be installed on the user's machine.

I don't need extraction or anything else fancy like listing files - I only need to add the entire contents of a folder to a single cabinet file. I know cabinet.dll exists in the \Windows\System32 folder and I'm hoping there's a simple way to interact with this DLL to achieve what I'm trying to do.

Is it possible to do this without an external library? If anyone could point me in the right direction I'd be eternally grateful.

Guardafui answered 21/12, 2009 at 11:41 Comment(0)
S
10

You may take a look at WiX and more specifically the Microsoft.Deployment.Compression.Cab assembly. It has a nice API that allows you to do this:

CabInfo cab = new CabInfo(@"c:\Work\some.cab"); 
cab.Pack(@"C:\Work\folder");

It's dependent on the Microsoft.Deployment.Compression assembly which is standalone so you will need only those two assemblies.

Sonatina answered 21/12, 2009 at 14:45 Comment(7)
I took a look at WiX but it won't install because I'm using VS2010 Beta 2. I was hoping to be able to ship something that doesn't require dependencies such as WiX to be installed.Guardafui
Just extract the two assemblies I mentioned from the MSI using LessMSIerables (blogs.pingpoet.com/overflow/archive/2005/06/02/2449.aspx) and ship them only.Sonatina
...you don't need to have WiX installed.Sonatina
oh, ok don't mind me I'm a .net newbie :) I'll try it out asap.Guardafui
Thanks, it worked and I guess I don't mind distrubuting the two dependant assemblies.Guardafui
@DarinDimitrov, do you know if it's possible to get WIX to run in a medium trust asp.net hosting environment? Related: #8723691Mayers
Wix seems like it's available as a plain zip file now! wix.codeplex.com/releasesChuddar
U
0

I needed to create a CAB file in a hurry for a project last week. What I really wanted to do was to replace a single XML file inside an existing CAB file, preserving the folder structure in the CAB. I soon learned that there was no simple 7Zip or WinZip way to do this in place, and that the success path was to generate a new CAB file with the modified files I wanted in it. This tool to create a CAB file from all files in a folder worked beautifully for me. It is a GUI front end to the MakeCab utility that creates It is described here: https://yieldreturnpost.wordpress.com/2016/06/22/free-tool-to-create-cab-file-from-folder/ The GitHub source is here: https://github.com/sapientcoder/CabMaker

First, I used the Expand command to extract the files from the existing CAB file to a folder structure.

expand -F:* \\MyServer\ServerMigration2020\ExportedConfigurationSourceFiles\MyOriginalCABFile.cab \\MyServer\CaptureSV\ServerMigration2020\Cabfiles\CabSourceFiles 

This Expand command extracted all the files inside my CAB file and placed them in the folder CabSourceFiles, preserving the folder structure.

Now, we are ready to run the CabMaker GUI to create our CAB file. Note that this is a "plain" CAB file of just files and folders, not one that has extra headers and such as part of a set of installer files.

I simply downloaded the CabMaker C# code and ran it in Visual Studio 2019. Up came the GUI. I entered my Source folder (the output folder in the Expand command), and my Target folder (where the resulting CAB file gets placed), and my desired CAB file name, and clicked the "Make CAB" button, and it ran. The resulting CAB file worked beautifully, and imported successfully into the app and resulted in a correctly set configuration.

CabMaker GUI screen shot

OK, so why did I need to make a CAB file? Well, we were migrating a vendor app from an old server to a new server, and using the app's "export" function to capture current state configuration files that we could "import" into the app on the new server. the alternative was to use the clunky GUI of the consumer app to manually navigate around and make a couple hundred changes to file and folder names that had changed. I decided it would be better to make the file and folder changes in the XML file within the CAB that held all the settings. It all worked out beautifully on server cutover day; sometimes we get lucky. Major kudos to GitHub contributor SapientCoder!

P.S. In case it helps anyone, the actual app was Kofax Capture 10.1, and the CAB file involved was the CAB file created by Kofax's export function to capture the current configuration of the Kofax system, including Kofax batch classes, document classes, form types, users, and batch class assigned users. So I will try to add a Kofax tag here in case it helps anyone with that need find this answer. The XML file involved is named "admin.xml" inside the CAB file that was produced by the source Kofax system. I changed a bunch of UNC filename references in admin.xml between the source and target Kofax installations, using NotePad++ find and replace. The modified CAB file worked beautifully on the target "new" Kofax 10.1 installation on a new server, no hiccups or issues at all.

Unorganized answered 14/1, 2020 at 4:14 Comment(1)
Had I seen the answer by Darin Dimitrov first, I probably would have tried that. I already use WiX and WixSharp, and appreciate both excellent tools. WixSharp, aka Wix#, is a C# based front end for the Windows Installer XML toolkit, aka the WiX toolkit, and is useful for building MSI installer files using C# syntax instead of coding your installer in XML. See infoq.com/articles/WixSharp and github.com/oleg-shilo/wixsharp for more info.Unorganized

© 2022 - 2024 — McMap. All rights reserved.