c#下载压缩文件

来源:互联网 发布:侧面导航栏网站源码 编辑:程序博客网 时间:2024/06/03 22:08
//DLL
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;


 public void DownloadZipFile(string id)
        {
            string zipFilePath = Property.GetFileUrlString("免冠照片temp", Property.FileEnum.zip);
            if (!Directory.Exists(Server.MapPath("~/Template/Template/Zip")))
            {
                Directory.CreateDirectory(Server.MapPath("~/Template/Template/Zip"));
            }


            try
            {




                List<string> filenames = new List<string>();
                string filesPath = string.Empty;
                foreach (var item in id.Split(','))
                {


                    var result = _pictureAppServices.FirstOrDefault(p => p.Id.ToString() == item);
                    if (result != null)
                    {
                        filesPath = result.Url;
                    }
                    filenames.Add(Server.MapPath("~/" + filesPath));
                    // filenames.Add(); ;
                }




                //生成的压缩文件为test.zip
                using (FileStream fsOut = System.IO.File.Create(Server.MapPath(zipFilePath+".zip")))
                {
                    //ZipOutputStream类的构造函数需要一个流,文件流、内存流都可以,压缩后的内容会写入到这个流中。
                    using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
                    {
                        foreach (string file in filenames)
                        {
                            FileInfo fi = new FileInfo(file);
                            string entryName = System.IO.Path.GetFileName(file);
                            //ZipEntry类代表了一个压缩包中的一个项,可以是一个文件,也可以是一个目录。
                            ZipEntry newEntry = new ZipEntry(entryName);
                            newEntry.DateTime = fi.LastWriteTime;
                            newEntry.Size = fi.Length;
                            //把压缩项的信息添加到ZipOutputStream中。
                            zipStream.PutNextEntry(newEntry);
                            byte[] buffer = new byte[4096];
                            //把需要压缩文件以文件流的方式复制到ZipOutputStream中。


                            using (FileStream streamReader = System.IO.File.OpenRead(file))
                            {
                                StreamUtils.Copy(streamReader, zipStream, buffer);
                            }
                            zipStream.CloseEntry();
                        }


                        //使用流操作时一定要设置IsStreamOwner为false。否则很容易发生在文件流关闭后的异常。
                        zipStream.IsStreamOwner = false;
                        zipStream.Finish();
                        zipStream.Close();
                    }
                }


                // filenames = Directory.GetFiles(filesPath);
                //using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(zipFilePath)))
                //{


                //    s.SetLevel(9); // 压缩级别 0-9
                //    //s.Password = "123"; //Zip压缩文件密码
                //    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 = System.IO.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)
            {
                Console.WriteLine("Exception during processing {0}", ex);
            }
      return File(Server.MapPath(zipFilePath), "application/zip", "免冠照片.zip");
        }
原创粉丝点击