Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Here is my code,

C#
string[] filePaths = Directory.GetFiles(@"D:\MP_Upload");
            string[] FName = new string[filePaths.Length];
            int j=0;
            foreach (string path in filePaths)
             {
                 FName[j] = Path.GetFileName(path);
                 j++;
            }
            using (ZipFile zip=new ZipFile())
            {
                for (int i = 0; i < FName.Length; i++)
                {
                    string filename = @"C:\Users\user\Downloads\" + FName[i];
                    string filePath = filePaths[i]; //Server.MapPath(@"D:\MP_Upload" + filename);
                  zip.AddFile(filePath, FName[i]);
                }
                Response.Clear();
                Response.BufferOutput = false;
                Response.AddHeader("Content-Disposition", "attachment; filename=chart4d(1).pdf");
                Response.ContentType = "application/zip";
                zip.Save(Response.OutputStream);
                Response.End();
            }


the problem with this code is that only one file get download as normal file.There is total 6 files in MP_upload folder.I want all this files to downloaded in zip format.
Posted

1 solution

Use this code.

C#
string SourceFolderPath = System.IO.Path.Combine(FolderPath, "Initial");

  Response.Clear();
  Response.ContentType = "application/zip";
  Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", nameFolder + ".zip"));

  bool recurseDirectories = true;
  using (ZipFile zip = new ZipFile())
  {
      zip.AddSelectedFiles("*", SourceFolderPath, string.Empty, recurseDirectories);
      zip.Save(Response.OutputStream);
  }
  Response.End();



Explained on below link

http://stackoverflow.com/questions/22564831/download-files-and-folders-as-a-zip-file-using-c-sharp-doesnt-work-in-mac[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900