gpt4 book ai didi

c# - 如何从 C# 中的文件创建内存中的结构化 ZIP 存档?

转载 作者:行者123 更新时间:2023-12-04 10:45:40 25 4
gpt4 key购买 nike

我有一些属于某个逻辑类别的文件,比如“Category_A”,还有一些属于不同逻辑类别(“Category_B”)的其他文件。我想创建一个内存中的 ZIP 对象(MemoryStream、字节数组或其他东西),以便 ZIP 对象的结果结构反射(reflect)以下方案:

ZIP
+-Category_A
| +FileA1
| +FileA2
| ...
+-Category_B
+FileB1
+FileB2
...

源文件系统中不存在目录“Category_A”和“Category_B”,文件来自不同的地方。

有没有可能用任何图书馆来实现这一点? (我的项目是在 .Net 4.0 中,升级是没有选择的)。我尝试使用 IonicZip Zip 文件和 GZipStream,但没有找到解决方案。

最佳答案

谢谢大家。与此同时,我自己也找到了答案(像往常一样,通过尝试和错误)。使用 Ionic Zip 库,您可以大致执行以下操作:

ZipFile zip = new ZipFile();

// below are the files of "Category_A" converted to byte arrays:
byte[] fileA1 = ...;
byte[] fileA2 = ...;

// the byte arrays will be added to the archive with paths "Category_A\\fileA1"
// and "Category_A\\fileA2" respectively.
// The level "Category_A" within the archive is created automatically.
zip.AddEntry("Category_A\\fileA1", fileA1);
zip.AddEntry("Category_A\\fileA2", fileA2);

// The same for "Category_B" and its files:
byte[] fileB1 = ...;
byte[] fileB2 = ...;

zip.AddEntry("Category_B\\fileB1", fileB1);
zip.AddEntry("Category_B\\fileB2", fileB2);

// save the result.
zip.Save("test.zip");

我之前尝试时的错误是我首先为类别创建了 ZipEntries,然后另外为它们的文件创建了错误,因为然后使用类别名称创建了一个空的 ZipEntry。

关于c# - 如何从 C# 中的文件创建内存中的结构化 ZIP 存档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59719211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com