ASP.NET 打包下载文件

2018-06-23 22:52:09来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

 

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

 

  1 using ICSharpCode.SharpZipLib.Zip;
  2 
  3 public partial class _Default : System.Web.UI.Page
  4 {
  5     protected void Page_Load(object sender, EventArgs e)
  6     {
  7         // 打包下载某文件夹里的所有文件
  8 
  9         //需要打包的文件夹
 10         string path = "UploadImage/Images/";
 11         string serverPath = Server.MapPath(path);
 12 
 13         //创建临时文件夹
 14         string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");
 15         string tempFolder = Path.Combine(serverPath, tempName);
 16         Directory.CreateDirectory(tempFolder);
 17 
 18         //遍历文件夹中所有的文件到临时文件夹中
 19         DirectoryInfo folder = new DirectoryInfo(serverPath);
 20         foreach (FileInfo file in folder.GetFiles())
 21         {
 22             string filename = file.Name;
 23             File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename);
 24         }
 25 
 26         //压缩文件
 27         compressFiles(tempFolder, tempFolder + "\\\\" + tempName + ".rar");
 28         //下载文件
 29         // DownloadRAR(tempFolder + "\\\\" + tempName + ".rar", "这里下载文件重命名后的名称");
 30 
 31 
 32         //某些文件打包下载
 33         DownLodeSum();
 34 
 35     }
 36 
 37     /// <summary>
 38     /// 某些文件打包下载
 39     /// </summary>
 40     public void DownLodeSum()
 41     {
 42         //临时文件夹所在目录
 43         string path = "UploadImage/";
 44         string serverPath = Server.MapPath(path);
 45 
 46         //创建临时文件夹
 47         string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");
 48         string tempFolder = Path.Combine(serverPath, tempName);
 49         Directory.CreateDirectory(tempFolder);
 50 
 51         //复制需要压缩的文件到临时文件夹中
 52         File.Copy(Server.MapPath("UploadImage/Images/bg-b.png"), Server.MapPath(path + tempName + "/bg-b.png"));
 53         File.Copy(Server.MapPath("UploadImage/Images/bg-j.png"), Server.MapPath(path + tempName + "/bg-j.png"));
 54         File.Copy(Server.MapPath("UploadImage/Images/icon-cart.png"), Server.MapPath(path + tempName + "/icon-cart.png"));
 55 
 56         //压缩文件
 57         compressFiles(tempFolder, tempFolder + "\\\\" + tempName + ".rar");
 58         //下载文件
 59         DownloadRAR(tempFolder + "\\\\" + tempName + ".rar", "这里下载文件重命名后的名称");
 60 
 61     }
 62 
 63     /// <summary>
 64     /// 压缩文件
 65     /// </summary>
 66     /// <param name="dir">文件目录</param>
 67     /// <param name="zipfilename">zip文件名</param>
 68     public static void compressFiles(string dir, string zipfilename)
 69     {
 70         if (!Directory.Exists(dir))
 71         {
 72             return;
 73         }
 74         try
 75         {
 76             string[] filenames = Directory.GetFiles(dir);
 77             using (ZipOutputStream s = new ZipOutputStream(File.Create(zipfilename)))
 78             {
 79 
 80                 s.SetLevel(9); // 0 - store only to 9 - means best compression
 81 
 82                 byte[] buffer = new byte[4096];
 83 
 84                 foreach (string file in filenames)
 85                 {
 86                     ZipEntry entry = new ZipEntry(Path.GetFileName(file));
 87                     entry.DateTime = DateTime.Now;
 88                     s.PutNextEntry(entry);
 89                     using (FileStream fs = File.OpenRead(file))
 90                     {
 91                         int sourceBytes;
 92                         do
 93                         {
 94                             sourceBytes = fs.Read(buffer, 0, buffer.Length);
 95                             s.Write(buffer, 0, sourceBytes);
 96                         } while (sourceBytes > 0);
 97                     }
 98                 }
 99                 s.Finish();
100                 s.Close();
101             }
102         }
103         catch
104         {
105 
106         }
107     }
108 
109     /// <summary>
110     /// 下载生成的RAR文件
111     /// </summary>
112     private void DownloadRAR(string file, string name)
113     {
114         FileInfo fileInfo = new FileInfo(file);
115         Response.Clear();
116         Response.ClearContent();
117         Response.ClearHeaders();
118         Response.AddHeader("Content-Disposition", "attachment;filename=" + name + ".rar");
119         Response.AddHeader("Content-Length", fileInfo.Length.ToString());
120         Response.AddHeader("Content-Transfer-Encoding", "binary");
121         Response.ContentType = "application/octet-stream";
122         Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
123         Response.WriteFile(fileInfo.FullName);
124         Response.Flush();
125         string tempPath = file.Substring(0, file.LastIndexOf("\\\\"));
126         //删除临时目录下的所有文件
127         DeleteFiles(tempPath);
128         //删除空目录
129         Directory.Delete(tempPath);
130         Response.End();
131     }
132 
133     /// <summary>
134     /// 删除临时目录下的所有文件
135     /// </summary>
136     /// <param name="tempPath">临时目录路径</param>
137     private void DeleteFiles(string tempPath)
138     {
139         DirectoryInfo directory = new DirectoryInfo(tempPath);
140         try
141         {
142             foreach (FileInfo file in directory.GetFiles())
143             {
144                 if (file.Attributes.ToString().IndexOf("ReadOnly") != -1)
145                 {
146                     file.Attributes = FileAttributes.Normal;
147                 }
148                 File.Delete(file.FullName);
149             }
150         }
151         catch (Exception)
152         {
153             throw;
154         }
155     }
156 }

 点击下载源码

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET验证控件一

下一篇:MVC中Action之间传值