Answered by:
Under zip having multiple folders with documents

Question
-
User1997618745 posted
Dear Team,
I wanted to download the nested folders (not more then two level) with files from the server in the zip format.
What would be the good approach for this.
Thanks,
Tuesday, August 21, 2018 10:42 AM
Answers
-
User283571144 posted
Hi Selvakumar Rathinam,
I wanted to download the nested folders (not more then two level) with files from the server in the zip format.According to your description, I suggst you could try to use DotNetZip library to help us compress the file folder.
You could directly install it from Nuget Package:
https://www.nuget.org/packages/DotNetZip
Then you could use ZipFile.AddDirectory to compree the file folder.
At last, you could use Response.WriteFile to download the file to client side.
More details, you could refer to below codes:
using System.IO; using Ionic.Zip; protected void Button1_Click(object sender, EventArgs e) { using(ZipFile zip=new ZipFile()) { zip.AddDirectory(Server.MapPath("~/App_Data/"));// ~/App_Data/ is your folder in the server zip.Save(Server.MapPath("~/zipa.zip"));// ~/zipa.zip is the folder of your zip } string strFilePath = Server.MapPath("~") + "/zipa.zip"; FileInfo fileInfo = new FileInfo(strFilePath); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileInfo.Name)); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); Response.ContentType = "application/x-bittorrent"; Response.WriteFile(fileInfo.FullName); Response.End(); }
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 23, 2018 2:17 AM
All replies
-
User753101303 posted
Hi,
Try https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile.createfromdirectory?view=netframework-4.7.2 which should zip the full tree from the starting point you give.
My understanding is that you mean you never have more than 2 levels (not that you may have more levels but want to zip only the first 2 levels).
Tuesday, August 21, 2018 1:05 PM -
User283571144 posted
Hi Selvakumar Rathinam,
I wanted to download the nested folders (not more then two level) with files from the server in the zip format.According to your description, I suggst you could try to use DotNetZip library to help us compress the file folder.
You could directly install it from Nuget Package:
https://www.nuget.org/packages/DotNetZip
Then you could use ZipFile.AddDirectory to compree the file folder.
At last, you could use Response.WriteFile to download the file to client side.
More details, you could refer to below codes:
using System.IO; using Ionic.Zip; protected void Button1_Click(object sender, EventArgs e) { using(ZipFile zip=new ZipFile()) { zip.AddDirectory(Server.MapPath("~/App_Data/"));// ~/App_Data/ is your folder in the server zip.Save(Server.MapPath("~/zipa.zip"));// ~/zipa.zip is the folder of your zip } string strFilePath = Server.MapPath("~") + "/zipa.zip"; FileInfo fileInfo = new FileInfo(strFilePath); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileInfo.Name)); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); Response.ContentType = "application/x-bittorrent"; Response.WriteFile(fileInfo.FullName); Response.End(); }
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 23, 2018 2:17 AM