【发布时间】:2014-12-02 17:07:54
【问题描述】:
我有一个 ZipArchive,并希望访问其中的一个文件。我不知道该怎么做,但我有一个清单
List<ZipContents> importList = new List<ZipContents>();
其中有两个参数:
-
ZipArchive称为ZipFile -
String称为FileName
在ZipArchive 即importList.ZipFile 中,我需要找到一个与Zip 文件名同名的XML 文件。
目前我有这个:
foreach (var import in importList)
{
var fn = import.FileName; // This is the actual file name of the zip file
// that was added into the ZipArchive.
// I will need to access the specific XML file need in the Zip by associating with
// fn
// ToDo: Extract XML file needed
// ToDo: Begin to access its contents...
}
例如,代码正在查找名称为 test.zip 的 ZipArchive。将有一个名为 test.xml 的文件,然后我需要能够访问其内容。
就像我上面所说的,我需要能够访问该文件的内容。很抱歉,我没有支持如何执行此操作的代码,但我找不到其他任何东西...
我已经浏览了很多 ZIpArchive 文档(包括:http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx)和其他关于如何执行此操作的帖子,但我发现都是空的。有人知道如何做到这一点吗?任何帮助将非常感激。谢谢!
【问题讨论】:
-
我希望我至少能够使用
import.ZipFile.Entries["test.xml"]通过文件名访问它,但我什至无法达到目标... -
是单个xml条目还是可以有多个xml条目?
-
如果是单个 xml 条目,只需获取您的存档名称,然后对其执行 string.replace,将 .zip 替换为 .xml,然后按正常方式提取...
-
使用 (MemoryStream ms = new MemoryStream()) { using (ZipFile zip = ZipFile.Read(docloc)) { zip.Encryption = EncryptionAlgorithm.WinZipAes256; //如果存档被加密。 zip.Password = somePassword; //如果这是一个密码存档.. ZipEntry entry = zip(docloc.Replace(".zip",".xml"); entry.Extract(ms); } b = ms.ToArray; } 我把它插入了一个代码转换器,所以它可能需要一些细微的调整,但据我所知,它似乎是正确的。对于格式设置抱歉,在评论中效果不佳。
标签: c# ziparchive