C#/.NET:创建 CAB 并向其中添加文件,无需外部库

发布于 2024-08-15 17:56:57 字数 355 浏览 15 评论 0原文

我知道有一个类似的问题但是标记的答案提供了到外部库的链接,该外部库需要在用户的计算机上安装多个依赖项。

我不需要提取或任何其他奇特的东西,例如列出文件 - 我只需要将文件夹的全部内容添加到单个文件柜中。我知道 Cabinet.dll 存在于 \Windows\System32 文件夹中,我希望有一种简单的方法与该 DLL 交互以实现我想要做的事情。

是否可以在没有外部库的情况下做到这一点?如果有人能指出我正确的方向,我将永远感激不已。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2024-08-22 17:56:57

您可以查看 WiX,更具体地说是 Microsoft.Deployment.Compression.Cab集会。它有一个很好的 API,允许您执行此操作:

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

它依赖于独立的 Microsoft.Deployment.Compression 程序集,因此您只需要这两个程序集。

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.

要走就滚别墨迹 2024-08-22 17:56:57

上周我需要为一个项目紧急创建一个CAB文件。我真正想要做的是替换现有 CAB 文件中的单个 XML 文件,保留 CAB 中的文件夹结构。我很快了解到,没有简单的 7Zip 或 WinZip 方法可以做到这一点,成功的途径是生成一个新的 CAB 文件,其中包含我想要的修改后的文件。这个从文件夹中的所有文件创建 CAB 文件的工具对我来说效果很好。它是 MakeCab 实用程序的 GUI 前端,用于创建它,如下所述: https://yieldreturnpost.wordpress.com/2016/06/22/free-tool-to-create-cab-file-from-folder/
GitHub 源代码在这里: https://github.com/sapientcoder/CabMaker

首先,我使用Expand 命令将现有 CAB 文件中的文件提取到文件夹结构中。

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

此展开命令提取了我的 CAB 文件中的所有文件,并将它们放置在 CabSourceFiles 文件夹中,保留了文件夹结构。

现在,我们准备运行 CabMaker GUI 来创建 CAB 文件。请注意,这是一个仅包含文件和文件夹的“普通”CAB 文件,而不是具有额外标头的文件,例如一组安装程序文件的一部分。

我只是下载了 CabMaker C# 代码并在 Visual Studio 2019 中运行它。 GUI 就出现了。我输入了源文件夹(展开命令中的输出文件夹)、目标文件夹(放置生成的 CAB 文件的位置)以及所需的 CAB 文件名,然后单击“生成 CAB”按钮,然后运行。生成的 CAB 文件运行良好,并成功导入到应用程序中并产生正确设置的配置。

CabMaker GUI屏幕截图

好的,那么为什么我需要制作 CAB 文件?好吧,我们正在将供应商应用程序从旧服务器迁移到新服务器,并使用应用程序的“导出”功能来捕获当前状态配置文件,我们可以将其“导入”到新服务器上的应用程序中。另一种方法是使用消费者应用程序笨重的 GUI 手动导航并对已更改的文件和文件夹名称进行数百处更改。我决定最好在保存所有设置的 CAB 内的 XML 文件中进行文件和文件夹更改。在服务器切换日,一切都进展顺利;有时我们很幸运。向 GitHub 贡献者 SapientCoder 致敬!

PS如果它对任何人有帮助,实际的应用程序是Kofax Capture 10.1,涉及的CAB文件是Kofax的导出功能创建的CAB文件,用于捕获Kofax系统的当前配置,包括Kofax批处理类,文档类,表单类型,用户和批次类别分配的用户。因此,我将尝试在此处添加 Kofax 标签,以防它可以帮助任何有需要的人找到此答案。所涉及的 XML 文件在由源 Kofax 系统生成的 CAB 文件中名为“admin.xml”。我使用 NotePad++ 查找和替换在源 Kofax 安装和目标 Kofax 安装之间更改了 admin.xml 中的一堆 UNC 文件名引用。修改后的 CAB 文件在新服务器上的目标“新”Kofax 10.1 安装上运行良好,完全没有任何问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文