使用插件压缩文件并提供下载

2018-06-22 07:31:54来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

直接上网上拿来的方法:

        /// <summary>
        /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)
        /// </summary>
        /// <param name="dirPath">被压缩的文件夹夹路径</param>
        /// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param>
        /// <param name="err">出错信息</param>
        /// <returns>是否压缩成功</returns>
        public static bool ZipFile(string dirPath, string zipFilePath, out string err)
        {
            //如果服务器存在同名文件,则删除
            if (File.Exists(zipFilePath))
            {
                try
                {
                    File.Delete(zipFilePath);
                }
                catch { }
            }
            err = "";
            if (dirPath == string.Empty)
            {
                err = "要压缩的文件夹不能为空!";
                return false;
            }
            if (!Directory.Exists(dirPath))
            {
                err = "要压缩的文件夹不存在!";
                return false;
            }
            //压缩文件名为空时使用文件夹名+.zip
            if (zipFilePath == string.Empty)
            {
                if (dirPath.EndsWith("\\"))
                {
                    dirPath = dirPath.Substring(0, dirPath.Length - 1);
                }
                zipFilePath = dirPath + ".zip";
            }

            try
            {
                string[] filenames = Directory.GetFiles(dirPath);
                using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
                {
                    s.SetLevel(9);
                    byte[] buffer = new byte[4096];
                    foreach (string file in filenames)
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }
                    s.Finish();
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
            return true;
        }

直接上打包下载代码

                string temp_path = HttpContext.Current.Request.MapPath(".\\query\\");
                String path = temp_path + DateTime.Now.ToString("yyyyMMddHHmmss");
                string newPath = path + "\\";
                Directory.CreateDirectory(path);

                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    DataToCSV.SaveCSV(ds.Tables[i], newPath);
                }

                String result = String.Empty;
                Boolean flag = DataToCSV.ZipFile(newPath, temp_path + "MesData.zip", out result);
                if (flag)
                {
                    //删除生成文件夹
                    Directory.Delete(path, true);
                    //提供下载
                    FileInfo fileInfo = new FileInfo(temp_path + "MesData.zip");
                    Response.Clear();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.AddHeader("Content-Disposition", "attachment;filename=MesData.zip");
                    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    Response.AddHeader("Content-Transfer-Encoding", "binary");
                    Response.ContentType = "application/octet-stream";
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                    Response.WriteFile(fileInfo.FullName);
                    Response.Flush();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    //删除副本//File.Delete(filePath);
                    Response.Close();
                }
                else
                {
                    //提示信息
                }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET MVC 长连接(服务器推)完整实现

下一篇:C# 常用公共方法