我使用的是最新版本的 DotNetZip,并且有一个包含 5 个 XML 的 zip 文件。
我想打开 zip、读取 XML 文件并使用 XML 的值设置一个字符串。
我怎样才能做到这一点?
Code:
//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 ?
}
}
Thanks
ZipEntry
has an Extract()
提取到流的重载。(1) http://dotnetzip.herobo.com/DNZHelp/html/8e64cfe0-5dfc-2682-2c10-cb840ed3bfa1.htm
混合在这个答案中如何从 MemoryStream 中获取字符串? https://stackoverflow.com/questions/78181/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);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)