public byte[] ZipFile(List <B_Attachment> atts, string zipedFile, int blockSize, string IsMonth) { C_System Sys = new C_System(); try { //文件临时存储地址 string tmpFilePath = System.IO.Path.GetTempPath() + "\\" + Guid.NewGuid().ToString(); //压缩包地址 string tmpZipFilePath = tmpFilePath + "\\" + zipedFile + ".zip"; using (Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile(Encoding.GetEncoding("GB2312"))) { foreach (var itemKey in atts) { //根据参数判断月报流程查询和指标流程查询 string path = ""; if (IsMonth == "1") { B_MonthlyReport month = B_MonthlyreportOperator.Instance.GetMonthlyreport(itemKey.BusinessID); Sys = C_SystemOperator.Instance.GetSystem(month.SystemID); } else if (IsMonth == "2") { B_TargetPlan target = B_TargetplanOperator.Instance.GetTargetPlanByID(itemKey.BusinessID); Sys = C_SystemOperator.Instance.GetSystem(target.SystemID); } //获取系统名称 var folderName = string.Format("{0}", Sys.SystemName); List <string> files = new List <string>(); //生成压缩包里文件夹。且判断其中是否有相同名称的文件名称 string newPath = Path.Combine(tmpZipFilePath, folderName); if (!Directory.Exists(newPath)) { //不相同时创建一个该系统名称的文件夹。 if (path != newPath && !path.Contains(newPath)) { Directory.CreateDirectory(newPath); path += newPath; zipFile.AddDirectory(newPath, folderName); } } //有相同名称时,将文件流更新到文件夹里 zipFile.UpdateDirectory(newPath, folderName); //从附件表中取到文件存放路径 string fileName = itemKey.FileName; //生成存放文件的路径 string filePath = Path.Combine(newPath, fileName); var urlRonte = WebConfigurationManager.AppSettings["UploadFilePath"]; string url = Path.Combine(urlRonte, itemKey.Url); //读取文件流 FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read); try { byte[] bte = new byte[fs.Length]; //将FileStream转换成byte流 fs.Read(bte, 0, (int)fs.Length); File.WriteAllBytes(filePath, bte); zipFile.AddFile(filePath, folderName); files.Add(fileName); } catch (Exception) { } finally { if (fs != null) { //关闭资源 fs.Close(); } } } MemoryStream stream = new MemoryStream(); zipFile.Save(stream); return(stream.ToArray()); } } catch (Exception ex) { throw new Exception(ex.Message); } }