如何使用DotNetZip从zip中提取XML文件
如何使用DotNetZip从zip中提取XML文件
我正在使用最新版本的DotNetZip,我有一个包含5个XML文件的压缩包。
我想打开压缩包,读取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 ? } }
谢谢
admin 更改状态以发布 2023年5月22日
ZipEntry
有一个Extract()
重载方法,可以将文件解压到流中。 (1)
结合这个回答 How do you get a string from a MemoryStream?,你可以得到像这样的东西(完全没有经过测试):
string xfile = System.IO.File.ReadAllText(strNewFilePath, System.Text.Encoding.Default); ListxmlContents; 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); } } }