简体   繁体   English

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

[英]How to Use DotNetZip to extract XML file from zip

I'm using the latest version of DotNetZip, and I have a zip file with 5 XMLs on it. 我正在使用最新版本的DotNetZip,我有一个包含5个XML的zip文件。
I want to open the zip, read the XML files and set a String with the value of the XML. 我想打开zip,读取XML文件并使用XML的值设置String。
How can I do this? 我怎样才能做到这一点?

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() overload which extracts to a stream. ZipEntry有一个Extract()重载,它提取到一个流。 (1) (1)

Mixing in this answer to How do you get a string from a MemoryStream? 在这个答案中混合如何从MemoryStream中获取字符串? , you'd get something like this (completely untested): ,你会得到这样的东西(完全未经测试):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM