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

前端 未结 2 957
后悔当初
后悔当初 2020-12-21 00:35

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 machin

相关标签:
2条回答
  • 2020-12-21 01:27

    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.

    0 讨论(0)
  • 2020-12-21 01:28

    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.

    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.

    0 讨论(0)
提交回复
热议问题