zoukankan      html  css  js  c++  java
  • 使用插件压缩文件并提供下载

    直接上网上拿来的方法:

            /// <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
                    {
                        //提示信息
                    }
  • 相关阅读:
    Python中pymssql 的使用操作
    Python连接mysql数据库及简单增删改查操作示例代码
    Python之模块和包的创建与使用
    SQL SERVER将多行数据合并成一行
    SQL Server根据子查询更新语句update
    在VMware虚拟机中安装windows server2016步骤详解(带图解)
    http请求错误合集
    第三方图标库(三)iconMoon
    第三方图标库学习(二)Font Awesome
    MapperScannerConfigurer的原理
  • 原文地址:https://www.cnblogs.com/shuibing/p/5692847.html
Copyright © 2011-2022 走看看