我正在尝试下载并提取C#中的zip文件,特别是DotNetZip。
当我运行此代码时......
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(reportUrl);
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Stream stream = response.GetResponseStream();
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
ms.Seek(0, 0);
ZipInputStream zip = new ZipInputStream(ms);
zip.Seek(0, 0);
ZipEntry e = zip.GetNextEntry();
string s = e.FileName;
MemoryStream ms2 = new MemoryStream();
e.Extract(ms2);
执行Extract方法后,我得到......
$exception {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}
有什么想法?谢谢!
答案 0 :(得分:3)
很难说为什么你的代码不起作用。我首先要简化它并确保我正确处理所有可支配的资源,例如流:
class Program
{
static void Main()
{
var url = "http://downloads.sourceforge.net/project/junit/junit/3.8.1/junit3.8.1.zip";
using (var client = new WebClient())
using (var zip = ZipFile.Read(client.DownloadData(url)))
{
foreach (var entry in zip)
{
entry.Extract(".");
}
}
}
}
请务必查看使用DotNetZip库的many useful examples文档。