gpt4 book ai didi

c# - 如何使用 DotNetZip 从 zip 中提取 XML 文件

转载 作者:数据小太阳 更新时间:2023-10-29 01:48:55 27 4
gpt4 key购买 nike

我使用的是最新版本的 DotNetZip,我有一个包含 5 个 XML 的 zip 文件。
我想打开 zip,读取 XML 文件并设置一个包含 XML 值的字符串。
我该怎么做?

代码:

//thats my old way of doing it.But I needed the path, now I want to read from the memory
string xfile = System.IO.File.ReadAllText(strNewFilePath, System.Text.Encoding.Default);

using (ZipFile zip = ZipFile.Read(this.uplZip.PostedFile.InputStream))
{
foreach (ZipEntry theEntry in zip)
{
//What should I use here, Extract ?
}
}

谢谢

最佳答案

ZipEntry 有一个 Extract() 重载,可以提取到流中。 (1)

在这个答案中混合 How do you get a string from a MemoryStream? ,你会得到这样的东西(完全未经测试):

string xfile = System.IO.File.ReadAllText(strNewFilePath, System.Text.Encoding.Default);
List<string> xmlContents;

using (ZipFile zip = ZipFile.Read(this.uplZip.PostedFile.InputStream))
{
foreach (ZipEntry theEntry in zip)
{
using (var ms = new MemoryStream())
{
theEntry.Extract(ms);

// The StreamReader will read from the current
// position of the MemoryStream which is currently
// set at the end of the string we just wrote to it.
// We need to set the position to 0 in order to read
// from the beginning.
ms.Position = 0;
var sr = new StreamReader(ms);
var myStr = sr.ReadToEnd();
xmlContents.Add(myStr);
}
}
}

关于c# - 如何使用 DotNetZip 从 zip 中提取 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13887538/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com