本文介绍了提取内存使用C#DotNetZip失败的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试下载并解压在C#中的压缩文件,具体DotNetZip。

I'm trying to download 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 the Extract method executes, I get...

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

有什么想法?谢谢!

推荐答案

这很难说,为什么你的代码不能正常工作。

It's difficult to say why your code doesn't work. I would start by simplifying it and ensuring that I am properly disposing all disposable resources such as streams:

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库许多有用的例子的文档。

Make sure you checkout the documentation for many useful examples of using the DotNetZip library.

这篇关于提取内存使用C#DotNetZip失败的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!