Extracting zip file into memory with C # DotNetZip error

I am trying to load and extract a zip file in C #, specifically DotNetZip.

When I run this code ...

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

After executing the Extract method, I get ...

  $exception {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException} 

Any thoughts? Thanks!

Here's what the object looks like before the method runs

+4
source share
1 answer

It is hard to say why your code is not working. I would start by simplifying and ensuring that I properly manage all available resources, such as threads:

 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("."); } } } } 

Be sure to check the documentation for many useful examples of using the DotNetZip library.

+3
source

All Articles