[英]How to work with a ZipArchive in C#
I have a ZipArchive and am looking to access a file inside. 我有一个ZipArchive,正在寻找访问其中的文件的方法。 I am not sure how I would do this, but I have a list
我不确定该怎么做,但是我有一个清单
List<ZipContents> importList = new List<ZipContents>();
Which has two parameters: 其中有两个参数:
ZipArchive
which is called ZipFile
ZipArchive
,称为ZipFile
String
which is called FileName
FileName
String
Inside the ZipArchive
which is importList.ZipFile
I need to find an XML file that has the same name as the Zip file name. 在名为
importList.ZipFile
的ZipArchive
内部,我需要找到一个与Zip文件名相同名称的XML文件。
Currently I have this: 目前我有这个:
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...
}
So for example the code is looking into the ZipArchive with the name test.zip
. 因此,例如,代码正在使用名为
test.zip
的ZipArchive进行查找。 there will be a file called test.xml
that I will then need to be able to access its contents. 将存在一个名为
test.xml
的文件,然后我将需要该文件才能访问其内容。
Like I said above I need to be able to access the contents of that file. 就像我上面说的,我需要能够访问该文件的内容。 I am sorry I have no code to support how to do this, but I have not been able to find anything else...
很抱歉,我没有代码来支持执行此操作,但是我找不到其他任何东西...
I have looked through a lot of the ZIpArchive documentation (including: http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx ) and other posts on SO on how to do this, but I have come up empty. 我浏览了很多ZIpArchive文档(包括: http : //msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx )和其他文章关于如何执行此操作,但是我已经空了。 Would anyone have an idea on how to do this?
有人会对此有想法吗? Any help would be much appreciated.
任何帮助将非常感激。 Thanks!
谢谢!
You need to extract the archive to a directory (may as well use temp since I assume you don't want to keep these): 您需要将存档解压缩到目录中(最好使用temp,因为我假设您不想保留这些文件):
archive.ExtractToDirectory("path string");
//Get the directory info for the directory you just extracted to
DirectoryInfo di = new DirectoryInfo("path string");
//find the xml file you want
FileInfo fi = di.GetFiles(string.Format("{0}.xml", archiveName)).FirstOrDefault();
//if the file was found, do your thing
if(fi != null)
{
//Do your file stuff here.
}
//delete the extracted directory
di.Delete();
Edit: To do the same thing just unpacking the file you care about: 编辑:要做同样的事情只是解压缩您关心的文件:
//find your file
ZipArchiveEntry entry = archive
.Entries
.FirstOrDefault(e =>
e.Name == string.Format("{0}.xml", archiveName));
if(entry != null)
{
//unpack your file
entry.ExtractToFile("path to extract to");
//do your file stuff here
}
//delete file if you want
The MSDN you linked does a rather good job explaining how to access the files. 您链接的MSDN在解释如何访问文件方面做得很好。 Here it is applied to your example.
在这里,它适用于您的示例。
// iterate over the list items
foreach (var import in importList)
{
var fn = import.FileName;
// iterate over the actual archives
foreach (ZipArchiveEntry entry in import.ZipFile.Entries)
{
// only grab files that end in .xml
if (entry.FullName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
{
// this extracts the file
entry.ExtractToFile(Path.Combine(@"C:\extract", entry.FullName));
// this opens the file as a stream
using(var stream = new StreamReader(entry.Open())){
// this opens file as xml stream
using(var xml = XmlReader.Create(stream){
// do some xml access on an arbitrary node
xml.MoveToContent();
xml.ReadToDescendant("my-node");
var item = xml.ReadElementContentAsString();
}
}
}
}
}
The following will extract a single xml
file called file.xml
and read it to an XDocument
object: 下面将提取一个名为
file.xml
xml
文件,并将其读取到XDocument
对象:
var xmlEntry = importList.SelectMany(y => y.Entries)
.FirstOrDefault(entry => entry.Name.Equals("file.xml",
StringComparison.OrdinalIgnoreCase));
if (xmlEntry == null)
{
return;
}
// Open a stream to the underlying ZipArchiveEntry
using (XDocument xml = XDocument.Load(xmlEntry.Open()))
{
// Do stuff with XML
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.