如何使用 DotNetZip 从 zip 中提取 XML 文件 [英] How to Use DotNetZip to extract XML file from zip

查看:38
本文介绍了如何使用 DotNetZip 从 zip 中提取 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the latest version of DotNetZip, and I have a zip file with 5 XMLs on it.
I want to open the zip, read the XML files and set a String with the value of the XML.
How can I do this?

代码:

//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)

ZipEntry has an Extract() overload which extracts to a stream. (1)

在这个答案中混入 你如何得到一个字符串来自 MemoryStream?,你会得到这样的东西(完全未经测试):

Mixing in this answer to How do you get a string from a 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);
        }
    }
}

这篇关于如何使用 DotNetZip 从 zip 中提取 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆