C# IonicZip/DotNetZip无法访问已关闭的文件。context.Response.OutputStream

C# IonicZip/DotNetZip无法访问已关闭的文件。context.Response.OutputStream,c#,stream,ashx,dotnetzip,C#,Stream,Ashx,Dotnetzip,我有一个*.ashx(处理程序) 使用此代码: public void ProcessRequest (HttpContext context) { context.Response.ContentType = "application/zip"; context.Response.AddHeader("Content-Disposition", "attachment; filename=catalog" + DateTime.Now.ToString("yyyy-MM-dd"

我有一个
*.ashx
(处理程序) 使用此代码:

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "application/zip";
    context.Response.AddHeader("Content-Disposition", "attachment; filename=catalog" + DateTime.Now.ToString("yyyy-MM-dd") + ".zip");

    // magic happends here to get a DataTable called 'dt'

    using (ZipFile zip = new ZipFile(Encoding.UTF8))
    {
        foreach (DataRow dr in dt.Rows)
        {
            string barCode = "C:/tmp/bc/" + dr["ProductCode"] + ".gif";

            if (File.Exists(barCode))
            {
                if (!zip.EntryFileNames.Contains("bc" + dr["ProductCode"] + ".gif"))
                {
                    try
                    {
                        // this option does not work
                        using (StreamReader sr = new StreamReader(barCode))
                        {
                            if (sr.BaseStream.CanRead)
                                zip.AddEntry("bc" + dr["ProductCode"] + ".gif", sr.BaseStream);
                        }
                        // but the next line does work... WHY?
                        zip.AddEntry("bc" + dr["ProductCode"] + ".gif", File.ReadAllBytes(barCode));
                    }
                    catch (Exception ex)
                    {
                        // never hits the catch
                        context.Response.Write(ex.Message);
                    }
                }
            }
        }
        zip.Save(context.Response.OutputStream); // here is the exception if I use the first option
    }
}
我使用的是最新版本的 有人能给我解释一下,为什么
文件.ReadAllBytes
可以工作,而
流阅读器在保存到
输出流时会崩溃?

异常消息是无法访问关闭的文件

问题是您将流包装在using语句中。在using语句的末尾,流被释放

调用
zip.save
时,库会尝试访问已关闭的流。File.ReadAllBytes不会失败,因为它直接传递数据

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/zip";
context.Response.AddHeader("Content-Disposition", "attachment; filename=catalog" + DateTime.Now.ToString("yyyy-MM-dd") + ".zip");

    using (ZipFile zip = new ZipFile(Encoding.UTF8))
    {
        foreach (DataRow dr in dt.Rows)
        {
            string barCode = "C:/tmp/bc/" + dr["ProductCode"] + ".gif";

            if (File.Exists(barCode))
            {
                if (!zip.EntryFileNames.Contains("bc" + dr["ProductCode"] + ".gif"))
                {
                    try
                    {
                        // The file stream is opened here
                        using (StreamReader sr = new StreamReader(barCode))
                        {
                            if (sr.BaseStream.CanRead)
                                zip.AddEntry("bc" + dr["ProductCode"] + ".gif", sr.BaseStream);
                        }
                        // The file stream is closed here
                    }
                    catch (Exception ex)
                    {
                        // never hits the catch
                        context.Response.Write(ex.Message);
                    }
                }
            }
        }

        // The closed file streams are accessed here
        zip.Save(context.Response.OutputStream);
    }
}