zoukankan      html  css  js  c++  java
  • [转]C#如何把文件夹压缩打包然后下载

    public partial class _Default2 : System.Web.UI.Page{  
            protected void Page_Load(object sender, EventArgs e)  
            {  
      
            }  
            protected void Button1_Click(object sender, EventArgs e)  
            {  
                string Path = "c:\de";  
                string resultPath = string.Empty;  
                bool rel =false;  
                TimeSpan nowTimeSpan=new TimeSpan();  
                resultPath=YaSuo(out rel, out nowTimeSpan);  
                ResponseFile(resultPath);  
            }  
      
            /// <summary>  
            /// 压缩文件  
            /// </summary>  
            /// <returns>返回压缩后的路径</returns>  
            public string YaSuo(out bool bo, out TimeSpan times)  
            {  
                string rarurlPath = string.Empty;  
                bo = false;  
                //压缩文件  
                string yasuoPathSave = "c:\de\TZ.rar";  
                string yasuoPath = "c:\de\temp";  
                System.Diagnostics.Process pro = new System.Diagnostics.Process();  
                pro.StartInfo.FileName = @"C:Program FilesWinRARWinRAR.exe";//WinRAR所在路径  
                //pro.StartInfo.Arguments = "a " + yasuoPathSave + " " + yasuoPath + " -r ";//dir是你的目录名   
                pro.StartInfo.Arguments = string.Format("a {0} {1} -r",yasuoPathSave,yasuoPath);  
       
                pro.Start();  
                times = pro.TotalProcessorTime;  
                bo = pro.WaitForExit(60000);//设定一分钟  
                if (!bo)  
                    pro.Kill();  
                pro.Close();  
                pro.Dispose();  
                rarurlPath = yasuoPathSave;  
                return rarurlPath;  
            }  
      
      
            protected void ResponseFile(string filename)  
            {  
      
                FileInfo file = new FileInfo(filename);//创建一个文件对象  
                Response.Clear();//清除所有缓存区的内容  
                Response.Charset = "GB2312";//定义输出字符集  
                Response.ContentEncoding = Encoding.Default;//输出内容的编码为默认编码  
                Response.AddHeader("Content-Disposition","attachment;filename="+file.Name);  
                //添加头信息。为“文件下载/另存为”指定默认文件名称  
                Response.AddHeader("Content-Length",file.Length.ToString());  
                //添加头文件,指定文件的大小,让浏览器显示文件下载的速度   
                Response.WriteFile(file.FullName);// 把文件流发送到客户端  
                Response.End();  
            }  
      
      
        }  
  • 相关阅读:
    MySQL练习
    [转]mysql和redis的区别
    python框架面试题联系
    国内外免费接收短信验证码
    ubuntu环境下docker的安装与操作
    Django商城项目笔记No.18商品部分-数据表创建
    Django商城项目笔记No.17用户部分-用户中心用户地址管理
    Django商城项目笔记No.16用户部分-用户中心收货地址
    Django商城项目笔记No.15用户部分-用户中心邮箱验证
    Django商城项目笔记No.14用户部分-用户中心邮箱绑定
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/6021018.html
Copyright © 2011-2022 走看看