ASP.NET打包下载文件

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

使用的类库为:ICSharpCode.SharpZipLib.dll

一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异:

1usingICSharpCode.SharpZipLib.Zip;23publicpartialclass_Default : System.Web.UI.Page4{5PRotectedvoidPage_Load(objectsender, EventArgs e)6{7//打包下载某文件夹里的所有文件89//需要打包的文件夹10stringpath ="UploadImage/Images/";11stringserverPath =Server.MapPath(path);1213//创建临时文件夹14stringtempName = DateTime.Now.ToString("yyyyMMddHHMMss");15stringtempFolder =Path.Combine(serverPath, tempName);16Directory.CreateDirectory(tempFolder);1718//遍历文件夹中所有的文件到临时文件夹中19DirectoryInfo folder =newDirectoryInfo(serverPath);20foreach(FileInfo fileinfolder.GetFiles())21{22stringfilename =file.Name;23File.Copy(serverPath +"/"+ filename, tempFolder +"/"+filename);24}2526//压缩文件27compressFiles(tempFolder, tempFolder +"\\\\"+ tempName +".rar");28//下载文件29//DownloadRAR(tempFolder + "\\\\" + tempName + ".rar", "这里下载文件重命名后的名称");303132//某些文件打包下载33DownLodeSum();3435}3637///<summary>38///某些文件打包下载39///</summary>40publicvoidDownLodeSum()41{42//临时文件夹所在目录43stringpath ="UploadImage/";44stringserverPath =Server.MapPath(path);4546//创建临时文件夹47stringtempName = DateTime.Now.ToString("yyyyMMddHHMMss");48stringtempFolder =Path.Combine(serverPath, tempName);49Directory.CreateDirectory(tempFolder);5051//复制需要压缩的文件到临时文件夹中52File.Copy(Server.MapPath("UploadImage/Images/bg-b.png"), Server.MapPath(path + tempName +"/bg-b.png"));53File.Copy(Server.MapPath("UploadImage/Images/bg-j.png"), Server.MapPath(path + tempName +"/bg-j.png"));54File.Copy(Server.MapPath("UploadImage/Images/icon-cart.png"), Server.MapPath(path + tempName +"/icon-cart.png"));5556//压缩文件57compressFiles(tempFolder, tempFolder +"\\\\"+ tempName +".rar");58//下载文件59DownloadRAR(tempFolder +"\\\\"+ tempName +".rar","这里下载文件重命名后的名称");6061}6263///<summary>64///压缩文件65///</summary>66///<param name="dir">文件目录</param>67///<param name="zipfilename">zip文件名</param>68publicstaticvoidcompressFiles(stringdir,stringzipfilename)69{70if(!Directory.Exists(dir))71{72return;73}74try75{76string[] filenames =Directory.GetFiles(dir);77using(ZipOutputStream s =newZipOutputStream(File.Create(zipfilename)))78{7980s.SetLevel(9);//0 - store only to 9 - means best compression8182byte[] buffer =newbyte[4096];8384foreach(stringfileinfilenames)85{86ZipEntry entry =newZipEntry(Path.GetFileName(file));87entry.DateTime =DateTime.Now;88s.PutNextEntry(entry);89using(FileStream fs =File.OpenRead(file))90{91intsourceBytes;92do93{94sourceBytes = fs.Read(buffer,0, buffer.Length);95s.Write(buffer,0, sourceBytes);96}while(sourceBytes >0);97}98}99s.Finish();100s.Close();101}102}103catch104{105106}107}108109///<summary>110///下载生成的RAR文件111///</summary>112privatevoidDownloadRAR(stringfile,stringname)113{114FileInfo fileInfo =newFileInfo(file);115Response.Clear();116Response.ClearContent();117Response.ClearHeaders();118Response.AddHeader("Content-Disposition","attachment;filename="+ name +".rar");119Response.AddHeader("Content-Length", fileInfo.Length.ToString());120Response.AddHeader("Content-Transfer-Encoding","binary");121Response.ContentType ="application/octet-stream";122Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");123Response.WriteFile(fileInfo.FullName);124Response.Flush();125stringtempPath = file.Substring(0, file.LastIndexOf("\\\\"));126//删除临时目录下的所有文件127DeleteFiles(tempPath);128//删除空目录129Directory.Delete(tempPath);130Response.End();131}132133///<summary>134///删除临时目录下的所有文件135///</summary>136///<param name="tempPath">临时目录路径</param>137privatevoidDeleteFiles(stringtempPath)138{139DirectoryInfo directory =newDirectoryInfo(tempPath);140try141{142foreach(FileInfo fileindirectory.GetFiles())143{144if(file.Attributes.ToString().IndexOf("ReadOnly") != -1)145{146file.Attributes =FileAttributes.Normal;147}148File.Delete(file.FullName);149}150}151catch(Exception)152{153throw;154}155}156}

点击下载源码

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航