我正在使用最新版本的DotNetZip,我有一个包含5个XML的zip文件。
我想打开zip,读取XML文件并设置一个带有XML值的String。
我怎么能这样做?
代码:
//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 ?
}
}
由于
答案 0 :(得分:16)
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);
}
}
}