how to make a normal zip file for users in a web application using C#?

590 views Asked by At

I have the following code to create a downloadable zip archive that works fine:

ZipFile zip = new ZipFile();
zip.AlternateEncoding = Encoding.UTF8;
zip.AlternateEncodingUsage = ZipOption.Always;
zip.AddFiles(filePaths, false, ".");
string zipName = String.Format("Files_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd"));
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();

users usually open files within a zip archive without extracting that. unfortunately I cannot open a file without extracting when using above code to create a zip archive. when trying to open a file an extra window appears shown as below: enter image description here

I tried both two namespaces: Ionic.Zip and SharpCompress and the problem appears in either one. any help would be appreciated. tks

1

There are 1 answers

2
Anton Sizikov On

Your code is correct but keep in mind that XLSX file format is a zip file.

What happened, is that WinRAR treats it as an archive and just unpacks it.

You can configure WinRAR and tell it to open xlxs files using Excel.

Go to Options -> Settings -> Viewer and select Excel as an Associated Program for *.xlsx http://acritum.com/software/manuals/winrar/html/helpviewersettings.htm