Asp.net 实现选择文件批量下载

一、引用ICSharpCode.SharpZipLib.dlls下载地址  http://www.icsharpcode.net/OpenSource/SharpZipLib/

 

二、获取含有要打包文件名信息

 

格式解释:DirFullPath:目录,在哪个文件夹里选择文件

SearchPattern:搜索条件:比如*.doc 选择所有doc格式的文件

 fncountent:文件名列表 只单纯的名字 如:ee.txt 只是ee名字s

 public static List<string> GetDirFiles(string DirFullPath, string SearchPattern, List<string> fncountent)

        {

            if (Directory.Exists(DirFullPath) == true)

            {

                List<string> list = new List<string>();

                //获取当前目录下指定文件类型的文件列表

                string[] stringList = Directory.GetFiles(DirFullPath, SearchPattern);

 

                foreach (string str in stringList)

                {

                    string fileName;

                    fileName = System.IO.Path.GetFileNameWithoutExtension(str);

                    foreach (var s in fncountent)

                    {

                        if (fileName.Contains(s))

                        {

                            if (list.Where(a => a == fileName).Count() == 0)

                            {

                                list.Add(fileName);

                            }

                        }

                    }

 

                }

                return list;

            }

            else

            {

                return null;

            }

        }

 三、压缩文件文件方法

  // <summary> 

        /// 压缩指定文件生成ZIP文件 

        /// </summary> 

        /// <param name="topDirName">顶层文件夹名称 \Storage Card\PDADataExchange\send\xml\</param> 

        /// <param name="fileNamesToZip">待压缩文件列表  file名 ddd.txt</param> 

        /// <param name="ZipedFileName">ZIP文件  \Storage Card\PDADataExchange\send\zip\version.zip</param> 

        /// <param name="CompressionLevel">压缩比  7</param> 

        /// <param name="password">密码 ""</param> 

        /// <param name="comment">压缩文件注释文字  ""</param> 

        public static void ZipFile(string topDirName, string[] fileNamesToZip, string ZipedFileName, int CompressionLevel, string password, string comment)

        {

            using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Open(ZipedFileName, FileMode.Create)))

            {

                if (password != null && password.Length > 0)

                    s.Password = password;

 

                if (comment != null && comment.Length > 0)

                    s.SetComment(comment);

 

                s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression 

                foreach (string file in fileNamesToZip)

                {

                    using (FileStream fs = File.OpenRead(Path.Combine(topDirName, file)))   //打开待压缩文件 

                    {

                        byte[] buffer = new byte[fs.Length];

                        fs.Read(buffer, 0, buffer.Length);      //读取文件流 

                        ZipEntry entry = new ZipEntry(file);    //新建实例 

                        entry.DateTime = DateTime.Now;

                        entry.Size = fs.Length;

                        fs.Close();

                        s.PutNextEntry(entry);

                        s.Write(buffer, 0, buffer.Length);

                    }

                }

                s.Finish();

                s.Close();

            }

        }

四、返回文件方法

   protected void ResponseFile(string filename)

        {

 

            FileInfo file = new FileInfo(filename);//创建一个文件对象

            Response.Clear();//清除所有缓存区的内容

            Response.Charset = "GB2312";//定义输出字符集

            Response.ContentEncoding = Encoding.Default;//输出内容的编码为默认编码

            Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);

            //添加头信息。为“文件下载/另存为”指定默认文件名称

            Response.AddHeader("Content-Length", file.Length.ToString());

            //添加头文件,指定文件的大小,让浏览器显示文件下载的速度

            Response.WriteFile(file.FullName);// 把文件流发送到客户端

            Response.End();

        }

五、具体实现和调用

   //压缩后的文件名

        

var newlist = new List<string>();

     //条件要查找的文件s

            var path = XMLTools.getXmlValue("UpLoad", "Path");

            var filenames = CheckService.GetDirFiles(path, "*.dat", newlist);

            var zipname = DateTime.Now.ToString("yyyyMMdd");

            var zipdir = path + zipname + ".zip";

            if (!File.Exists(zipdir) == true)

            {

                File.Delete(zipdir);

            }

            //创建压缩文件

            FileStream myFs = new FileStream(zipdir, FileMode.Create);

            StreamWriter mySw = new StreamWriter(myFs);

            mySw.Close();

            myFs.Close();

            //把文件压缩到压缩文件

            var listY = new List<string>();

            foreach (var f in filenames)

            {

                //var fn = path + f + ".dat";

                 listY.Add(f + ".dat");

            }

            var fileNamesToZip = listY.ToArray();

            ZipFile(path, fileNamesToZip, zipdir, 7, null, "系统压缩");

            ResponseFile(zipdir);

 

 

posted @ 2012-03-30 11:53  cotty  阅读(980)  评论(1编辑  收藏  举报