簡體   English   中英

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

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

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

謝謝

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM