I have an *.ashx (handler)
with this code:
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
}
}
I use the latest version of http://dotnetzip.codeplex.com/
Can someone explain to me, why the File.ReadAllBytes does work and the StreamReader does crash when saving to the OutputStream?
The exception message is Cannot access a closed file
The problem is that you wrap the stream in a using statement. At the end of the using statement the stream is disposed.
When you call
zip.savethe library tries to access a stream which has been closed. File.ReadAllBytes will not fail because it passes the data directly.