在ZipArchive C#创建目录的.Net 4.5 [英] Creating Directories in a ZipArchive C# .Net 4.5

查看:761
本文介绍了在ZipArchive C#创建目录的.Net 4.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个ZipArchive是ZipArchiveEntries的集合,并添加/删除项工作得很好。 但它似乎有一个目录/嵌套的档案的概念。从理论上讲,类是从一个文件系统中,您可以在内存流完全创建归档脱钩。但是,如果你想在归档中添加一个目录结构,您必须preFIX与路径条目名称。

问:你会如何去扩展ZipArchive创造一个更好的界面,用于创建和管理目录

例如,将文件添加到目录的当前方法是创建与目录路径的项:

  VAR项= _archive.CreateEntry(目录/ entryname);
 

而这些方针的东西似乎更好的对我说:

  var目录= _archive.CreateDirectoryEntry(目录);
变种条目= _directory.CreateEntry(entryname);
 

解决方案

您可以使用类似以下,换句话说,手动创建的目录结构:

 使用(变种FS =新的FileStream(1.zip,FileMode.Create))
使用(VAR邮编=新的ZipArchive(FS,ZipArchiveMode.Create))
{
    zip.CreateEntry(12/3 /); //只是用结束/
}
 

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆