c#如何把文件夹压缩打包然后下载

来源:互联网 发布:汉朝历史 知乎 编辑:程序博客网 时间:2024/04/25 13:06
 6197人阅读 评论(4) 收藏 举报
[csharp] view plain copy
  1.   
[csharp] view plain copy
  1. public partial class _Default2 : System.Web.UI.Page{  
  2.         protected void Page_Load(object sender, EventArgs e)  
  3.         {  
  4.   
  5.         }  
  6.         protected void Button1_Click(object sender, EventArgs e)  
  7.         {  
  8.             string Path = "c:\\de";  
  9.             string resultPath = string.Empty;  
  10.             bool rel =false;  
  11.             TimeSpan nowTimeSpan=new TimeSpan();  
  12.             resultPath=YaSuo(out rel, out nowTimeSpan);  
  13.             ResponseFile(resultPath);  
  14.         }  
  15.   
  16.         /// <summary>  
  17.         /// 压缩文件  
  18.         /// </summary>  
  19.         /// <returns>返回压缩后的路径</returns>  
  20.         public string YaSuo(out bool bo, out TimeSpan times)  
  21.         {  
  22.             string rarurlPath = string.Empty;  
  23.             bo = false;  
  24.             //压缩文件  
  25.             string yasuoPathSave = "c:\\de\\TZ.rar";  
  26.             string yasuoPath = "c:\\de\\temp";  
  27.             System.Diagnostics.Process pro = new System.Diagnostics.Process();  
  28.             pro.StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe";//WinRAR所在路径  
  29.             //pro.StartInfo.Arguments = "a " + yasuoPathSave + " " + yasuoPath + " -r ";//dir是你的目录名   
  30.             pro.StartInfo.Arguments = string.Format("a {0} {1} -r",yasuoPathSave,yasuoPath);  
  31.    
  32.             pro.Start();  
  33.             times = pro.TotalProcessorTime;  
  34.             bo = pro.WaitForExit(60000);//设定一分钟  
  35.             if (!bo)  
  36.                 pro.Kill();  
  37.             pro.Close();  
  38.             pro.Dispose();  
  39.             rarurlPath = yasuoPathSave;  
  40.             return rarurlPath;  
  41.         }  
  42.   
  43.   
  44.         protected void ResponseFile(string filename)  
  45.         {  
  46.   
  47.             FileInfo file = new FileInfo(filename);//创建一个文件对象  
  48.             Response.Clear();//清除所有缓存区的内容  
  49.             Response.Charset = "GB2312";//定义输出字符集  
  50.             Response.ContentEncoding = Encoding.Default;//输出内容的编码为默认编码  
  51.             Response.AddHeader("Content-Disposition","attachment;filename="+file.Name);  
  52.             //添加头信息。为“文件下载/另存为”指定默认文件名称  
  53.             Response.AddHeader("Content-Length",file.Length.ToString());  
  54.             //添加头文件,指定文件的大小,让浏览器显示文件下载的速度   
  55.             Response.WriteFile(file.FullName);// 把文件流发送到客户端  
  56.             Response.End();  
  57.         }  
  58.   
  59.   
  60.     }  

0 0
原创粉丝点击