I am trying to use DotNetZip in a C# ASP.NET web page to create a archive file containing multiple graphic images. I present a list of file names in a repeater. The user selects one or more files from the repeater. The code then locates the files on a different server, creates a zipfile from them and then downloads them. The file is created, but I cannot open or extract files from it using native Win10. I have to use 7-Zip to use the file.
When I open the file using 7-Zip I see this warning when I use the Info button: There are some data after the end of the payload data.
The following is the code I use. Again, I get the files from a repeater on the web page:
protected void butDownload_Click(object sender, EventArgs e)
{
string strShortFileName;
string strLongFileName;
WebRequest wReq;
WebResponse wResp;
Stream receiveStream;
string zipName;
lblErrorText.Text = "";
try
{
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
Response.Clear();
Response.BufferOutput = false;
zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
foreach (RepeaterItem ri in rptConMedia.Items)
{
if (((CheckBox)ri.FindControl("chkBoxDownload")).Checked)
{
strShortFileName = ((Label)ri.FindControl("lblFileName")).Text;
strLongFileName = ((Label)ri.FindControl("lblPath")).Text + strShortFileName;
wReq = WebRequest.Create(strLongFileName);
wResp = wReq.GetResponse();
receiveStream = wResp.GetResponseStream();
zip.AddEntry(strShortFileName, receiveStream);
}
}
if (zip.Count > 0)
{
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.Clear();
}
else
lblErrorText.Text = "You must select at least one file to download.";
}
}
catch (Exception ex)
{
lblErrorText.Text = ex.Message;
MFAErrorUtil.LogError(ex, CMConstants.ErrorLogPath, "wucConMediaDisplay:Zip");
}
}
What I have tried:
If I download to a file on the IIS server instead of using the web browser, the zip file has no problems.
I've tried using different properties and methods on the Response class (Buffer, BufferOutput, Clear, Flush, ContentEncoding) with no success.
Any guidance with this would be appreciated