在 ZipArchive C# .Net 4.5 中创建目录 [英] Creating Directories in a ZipArchive C# .Net 4.5
问题描述
ZipArchive 是 ZipArchiveEntries 的集合,添加/删除条目"效果很好.但似乎没有目录/嵌套档案"的概念.理论上,该类与文件系统分离,因为您可以完全在内存流中创建存档.但是,如果您希望在存档中添加目录结构,则必须在条目名称前加上路径.
A ZipArchive is a collection of ZipArchiveEntries, and adding/removing "Entries" works nicely. But it appears there is no notion of directories / nested "Archives". In theory, the class is decoupled from a file system, in that you can create the archive completely in a memory stream. But if you wish to add a directory structure within the archive, you must prefix the entry name with a path.
问题:您将如何扩展 ZipArchive 以创建更好的界面来创建和管理目录?
Question: How would you go about extending ZipArchive to create a better interface for creating and managing directories?
例如,当前向目录中添加文件的方法是使用目录路径创建条目:
For example, the current method of adding a file to a directory is to create the entry with the directory path:
var entry = _archive.CreateEntry("directory/entryname");
而我觉得这方面的东西更好:
whereas something along these lines seems nicer to me:
var directory = _archive.CreateDirectoryEntry("directory");
var entry = _directory.CreateEntry("entryname");
推荐答案
您可以使用类似以下的内容,换句话说,手动创建目录结构:
You can use something like the following, in other words, create the directory structure by hand:
using (var fs = new FileStream("1.zip", FileMode.Create))
using (var zip = new ZipArchive(fs, ZipArchiveMode.Create))
{
zip.CreateEntry("12/3/"); // just end with "/"
}
这篇关于在 ZipArchive C# .Net 4.5 中创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!