6

我正在使用最新版本的 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 ?
    }
}

谢谢

4

1 回答 1

16

ZipEntry具有Extract()提取到流的重载。(1)

混入如何从 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);
        }
    }
}
于 2012-12-14T23:26:29.140 回答