1 var uploadSectionDir = Path.Combine("Upload", "QQ", DateTime.Now.ToString("yyyyMMdd")); 2 string uploadDir = Path.Combine(HttpRuntime.AppDomainAppPath, uploadSectionDir); 3 if (!Directory.Exists(uploadDir)) 4 { 5 Directory.CreateDirectory(uploadDir); 6 } 7 string fileName ="test.zip"; 8 string filePath = Path.Combine(uploadDir, fileName); 9 10 //生成的压缩文件为test.zip 11 using (FileStream fsOut = System.IO.File.Create(filePath)) 12 { 13 //ZipOutputStream类的构造函数需要一个流,文件流、内存流都可以,压缩后的内容会写入到这个流中。 14 using (ZipOutputStream zipStream = new ZipOutputStream(fsOut)) 15 { 16 MemoryStream ws = new AirBillBLL().ExportToExcel(ladingNoList); 17 string entryName = string.Concat(string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now), ".xls"); 18 ZipEntry newEntry = new ZipEntry(entryName); 19 newEntry.DateTime = DateTime.Now; 20 newEntry.Size = ws.Length; 21 22 //把压缩项的信息添加到ZipOutputStream中。 23 zipStream.PutNextEntry(newEntry); 24 byte[] buffer = new byte[4096]; 25 //把需要压缩文件以文件流的方式复制到ZipOutputStream中。 26 27 StreamUtils.Copy(ws, zipStream, buffer); 28 29 zipStream.CloseEntry(); 30 zipStream.IsStreamOwner = false; 31 zipStream.Finish(); 32 zipStream.Close(); 33 } 34 }
35 return File(filePath, "application/x-zip-compressed", string.Concat(string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now), ".zip"));