简体   繁体   English

C#/.NET:在没有外部库的情况下创建 CAB 并向其中添加文件

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

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.我知道cabinet.dll 存在于\\Windows\\System32 文件夹中,我希望有一种与此DLL 交互的简单方法来实现我想要做的事情。

Is it possible to do this without an external library?没有外部库可以做到这一点吗? If anyone could point me in the right direction I'd be eternally grateful.如果有人能指出我正确的方向,我将永远感激不尽。

You may take a look at WiX and more specifically the Microsoft.Deployment.Compression.Cab assembly.您可以查看WiX ,更具体地说是Microsoft.Deployment.Compression.Cab程序集。 It has a nice API that allows you to do this:它有一个很好的 API 可以让你做到这一点:

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.它依赖于独立的Microsoft.Deployment.Compression程序集,因此您只需要这两个程序集。

I needed to create a CAB file in a hurry for a project last week.上周我需要为一个项目匆忙创建一个 CAB 文件。 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.真正想做的是替换现有 CAB 文件中的单个 XML 文件,保留 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.我很快了解到,没有简单的 7Zip 或 WinZip 方法可以做到这一点,成功的途径是生成一个新的 CAB 文件,其中包含我想要的修改后的文件。 This tool to create a CAB file from all files in a folder worked beautifully for me.这个从文件夹中的所有文件创建 CAB 文件的工具对我来说效果很好。 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它是创建 MakeCab 实用程序的 GUI 前端,在此处进行了描述: https ://yieldreturnpost.wordpress.com/2016/06/22/free-tool-to-create-cab-file-from-folder/ GitHub 源码在这里: 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 命令将现有 CAB 文件中的文件提取到文件夹结构中。

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.这个扩展命令提取了我的 CAB 文件中的所有文件并将它们放在文件夹 CabSourceFiles 中,保留了文件夹结构。

Now, we are ready to run the CabMaker GUI to create our CAB file.现在,我们已准备好运行 CabMaker GUI 来创建我们的 CAB 文件。 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.请注意,这是一个仅包含文件和文件夹的“普通”CAB 文件,而不是具有额外标题的文件,例如一组安装程序文件的一部分。

I simply downloaded the CabMaker C# code and ran it in Visual Studio 2019. Up came the GUI.我只是下载了 CabMaker C# 代码并在 Visual Studio 2019 中运行它。 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.我输入了我的 Source 文件夹(Expand 命令中的输出文件夹)和我的 Target 文件夹(放置生成的 CAB 文件的位置)和我想要的 CAB 文件名,然后单击“制作 CAB”按钮,它就运行了。 The resulting CAB file worked beautifully, and imported successfully into the app and resulted in a correctly set configuration.生成的 CAB 文件运行良好,并成功导入到应用程序中,并产生了正确设置的配置。

CabMaker GUI 屏幕截图

OK, so why did I need to make a CAB file?好的,那么为什么我需要制作一个 CAB 文件呢? 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.另一种方法是使用消费者应用程序笨拙的 GUI 手动导航并对已更改的文件和文件夹名称进行数百次更改。 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.我决定最好在保存所有设置的 CAB 中的 XML 文件中更改文件和文件夹。 It all worked out beautifully on server cutover day;在服务器切换日,这一切都非常顺利; sometimes we get lucky.有时我们很幸运。 Major kudos to GitHub contributor SapientCoder!对 GitHub 贡献者 SapientCoder 的主要荣誉!

PS 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. PS 以防万一它对任何人有帮助,实际的应用程序是 Kofax Capture 10.1,所涉及的 CAB 文件是 Kofax 导出功能创建的 CAB 文件,用于捕获 Kofax 系统的当前配置,包括 Kofax 批处理类、文档类、表单类型、用户和批次类分配的用户。 So I will try to add a Kofax tag here in case it helps anyone with that need find this answer.因此,我将尝试在此处添加一个 Kofax 标签,以防它可以帮助有需要的任何人找到此答案。 The XML file involved is named "admin.xml" inside the CAB file that was produced by the source Kofax system.所涉及的 XML 文件在源 Kofax 系统生成的 CAB 文件中命名为“admin.xml”。 I changed a bunch of UNC filename references in admin.xml between the source and target Kofax installations, using NotePad++ find and replace.我在源和目标 Kofax 安装之间更改了 admin.xml 中的一堆 UNC 文件名引用,使用 NotePad++ 查找和替换。 The modified CAB file worked beautifully on the target "new" Kofax 10.1 installation on a new server, no hiccups or issues at all.修改后的 CAB 文件在新服务器上的目标“新”Kofax 10.1 安装上运行良好,完全没有打嗝或问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM