在C#中在服务器上打包压缩文件,并下载

来源:互联网 发布:演示demo制作软件 编辑:程序博客网 时间:2024/05/19 00:13

突然项目中需要用到这个功能,整了好长时间终于出来了,希望能够对像我一样的小白有所帮助,里面有的代码是可以变动的,可以变通 


#region 打包下载数据



        /// <summary>
        /// 打包下载rar文件
        /// </summary>
        public void DownLoad()
        {
            try
            {
                BPM_V4_STAEntities ef = new BPM_V4_STAEntities();
                string path = ef.Xm_System_Master.Where((c) => c.id == "160").Select(c => new { value = c.value }).ToString();
                if (File.Exists(path + "Data.rar"))
                {
                    File.Delete(path + "Data.rar");
                }
                RAR(path, path, "Data.rar");
                FileInfo DownloadFile = new FileInfo(path + "\\Data.rar");
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = false;
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8) + ";charset=GB2312");
                HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
                HttpContext.Current.Response.Flush();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 利用 WinRAR 进行压缩
        /// </summary>
        /// <param name="fileName">将要被压缩的文件夹(绝对路径)</param>
        /// <param name="rarPath">压缩后的 .rar 的存放目录(绝对路径)</param>
        /// <param name="rarName">压缩文件的名称(包括后缀)</param>
        /// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
        public bool RAR(string filePath, string rarPath, string rarName)
        {
            bool flag = false;
            string rarexe;       //WinRAR.exe 的完整路径
            RegistryKey regkey; //注册表键
            Object regvalue;     //键值
            string cmd;          //WinRAR 命令参数
            ProcessStartInfo startinfo;
            Process process;
            try
            {
                regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WINRAR.exe");
                regvalue = regkey.GetValue(""); // 键值为 "d:\Program Files\WinRAR\WinRAR.exe" "%1"
                rarexe = regvalue.ToString();
                regkey.Close();
                // rarexe = rarexe.Substring(1, rarexe.Length - 7); // d:\Program Files\WinRAR\WinRAR.exe


                Directory.CreateDirectory(filePath);
                //压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
                cmd = string.Format("a {0} {1} -r",
                                    rarName,
                                    filePath);
                startinfo = new ProcessStartInfo();
                startinfo.FileName = rarexe;
                startinfo.UseShellExecute = false;
                startinfo.Arguments = cmd;                          //设置命令参数
                startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口


                startinfo.WorkingDirectory = rarPath;
                process = new Process();
                process.StartInfo = startinfo;
                process.Start();
                process.WaitForExit(); //无限期等待进程 winrar.exe 退出
                if (process.HasExited)
                {
                    flag = true;
                }
                process.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            return flag;
        }


        #endregion
0 0
原创粉丝点击