C# 如何在C中使用ZipArchive#

C# 如何在C中使用ZipArchive#,c#,ziparchive,C#,Ziparchive,我有一个ZipArchive,正在查看里面的文件。我不知道该怎么做,但我有一个清单 List<ZipContents> importList = new List<ZipContents>(); 例如,代码正在查看名为test.zip的ZipArchive。将有一个名为test.xml的文件,我需要能够访问它的内容 正如我上面所说,我需要能够访问该文件的内容。很抱歉,我没有代码来支持如何做到这一点,但我一直无法找到任何其他 我已经浏览了很多ZIpArchive文档(包括

我有一个ZipArchive,正在查看里面的文件。我不知道该怎么做,但我有一个清单

List<ZipContents> importList = new List<ZipContents>();
例如,代码正在查看名为
test.zip
的ZipArchive。将有一个名为
test.xml
的文件,我需要能够访问它的内容

正如我上面所说,我需要能够访问该文件的内容。很抱歉,我没有代码来支持如何做到这一点,但我一直无法找到任何其他


我已经浏览了很多ZIpArchive文档(包括:)和其他关于如何做到这一点的帖子,但结果都是空的。有人知道怎么做吗?任何帮助都将不胜感激。谢谢

您需要将归档文件解压缩到一个目录(也可以使用temp,因为我假设您不想保留这些文件):

编辑:要执行相同的操作,只需解压缩您关心的文件:

//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

您链接的MSDN很好地解释了如何访问这些文件。这里它被应用到你的例子中

// 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();
                }
            }
        }
    }
}

下面将提取一个名为
file.xml
xml
文件,并将其读取到一个对象:


我希望我至少能够使用
import.ZipFile.Entries[“test.xml”]
通过文件名访问它,但我甚至无法找到足够的目标…它是一个xml条目还是可以有多个xml条目?如果它是一个xml条目,只需获取存档名称,并对其执行string.replace,将.zip替换为.xml,然后按照正常方式提取…使用(MemoryStream ms=new MemoryStream()){使用(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;}我将其插入了一个代码转换器中,因此可能需要稍作调整,但据我所知,它似乎是正确的。很抱歉格式化,在注释中效果不太好。感谢您在
archive.extractodirectory(“路径字符串”)中发布
什么是
存档
。那是我的
导入.ZipFile
吗?那将是你的ZipArchive目标如果你不想解包整件事,你实际上也可以解包单个文件,但它有点复杂。除了
di.Delete()
我一直在那里收到一个错误,说目录不是空的。还有什么方法可以删除目录吗?改用
di.delete(true);
//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
// 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();
                }
            }
        }
    }
}
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
}