asp.net用zip方法批量导出txt

2018-06-22 07:47:24来源:未知 阅读 ()

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

首先:

引用 ICSharpCode.SharpZipLib.dll,百度下载

然后引用命名空间:

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

我自己的代码:

                      //取数据dt   
 
                      string path = Server.MapPath("~\\tempzt\\");
                      if (dt.Rows.Count > 0)
                    {
                        System.Text.StringBuilder sb = new System.Text.StringBuilder();
                        string wjmc =  "xxxxxxxxxxxx".txt";

                        for (int n = 0; n < dt.Rows.Count; n++)
                        {
                            for (int m = 0; m < dt.Columns.Count; m++)
                            {
                                sb.Append(dt.Rows[n][m].ToString());

                                sb.Append("\t");
                            }
                            sb.Append("\r\n");
                        }
                        txt_export(sb, wjmc, path);
                    }
                }
            }
            //zip下载
            string[] files = Directory.GetFiles(path);
            if (files.Length > 0)
            {
                string name = "xxxxxxxxxxxx.zip";
                ZipFileMain(files, path + name, 9);
                DownLoadZip(path + name, name);
            }
            else
            {
                Response.Write("<script>alert('没有目标文件!请先写入')</script>");
            }
public void txt_export(StringBuilder sb, string wjmc, string path)
        {
            //zip下载
            string txtPath = path + wjmc;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (File.Exists(txtPath))
            {
                File.Delete(txtPath);
            }

            StreamWriter sw = new StreamWriter(txtPath, false, System.Text.Encoding.Default);
            sw.Write(sb.ToString());
            sw.Close();
            //zip下载
        }

        //zip下载
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="fileName">要压缩的所有文件(完全路径)</param>
        /// <param name="name">压缩后文件路径</param>
        /// <param name="Level">压缩级别</param>
        public void ZipFileMain(string[] filenames, string name, int Level)
        {
            if (File.Exists(name))
            {
                File.Delete(name);
            }
            ZipOutputStream s = new ZipOutputStream(File.Create(name));
            Crc32 crc = new Crc32();
            //压缩级别
            s.SetLevel(Level); // 0 - store only to 9 - means best compression
            try
            {
                foreach (string file in filenames)
                {
                    if (!Path.GetExtension(file).Equals(".txt"))
                    {
                        continue;
                    }
                    //打开压缩文件
                    FileStream fs = File.OpenRead(file);//文件地址
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    //建立压缩实体
                    ZipEntry entry = new ZipEntry(Path.GetFileName(file));//原文件名
                                                                          //时间
                    entry.DateTime = DateTime.Now;
                    //空间大小
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                    File.Delete(file);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                s.Finish();
                s.Close();
            }
        }

        public void DownLoadZip(string fileName,string name)
        {
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + name + "");
            Response.TransmitFile(fileName);
        }

标签:

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

上一篇:.net中DBHelper(Oracle版)

下一篇:Linq to SQL 中将数字转换为字符串