webform生成rar文件并下载

本文介绍了一个在ASP.NET中实现RAR文件压缩和下载的方法,通过调用WinRAR命令行工具来完成文件压缩,并实现了将压缩后的RAR文件提供给用户下载的功能。

 protected void btnRar_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Server.MapPath("/");
                string message = Rar(path + @"/uploads", @"pic/*.jpg", "", "tupian");
                string filePath = path + @"/uploads/tupian.rar";
                System.IO.FileInfo file = new System.IO.FileInfo(filePath);
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码   
                Response.AddHeader("Content-length", file.Length.ToString());
                //Response.ContentType = "appliction/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();


            }
            catch (Exception ex)
            {
                Page.RegisterStartupScript("key", "<script> alert('" + ex.Message + "');</script>");
            }
           
        }
        string Rar(string workDir, string sourceDir, string destDir, string destFileName)
        {
            string error = string.Empty;
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/Shell/Open/Command");
            string rarExeFile = regKey.GetValue("").ToString().Substring(1, regKey.GetValue("").ToString().Length - 7);
            regKey.Close();
            System.Diagnostics.ProcessStartInfo pInfo = new System.Diagnostics.ProcessStartInfo();
            pInfo.FileName = rarExeFile;
            pInfo.Arguments = string.Format("a  -rr {0}.rar {1}", destFileName, sourceDir);
            pInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            pInfo.WorkingDirectory = workDir;
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = pInfo;
            process.Start();
            error = "压缩成功";
            return error;

        }

==============================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunjay117

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值