[轉]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 Files\WinRAR\WinRAR.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();  
        }  
  
  
    }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM