其他分享
首页 > 其他分享> > .net core5.0 ICSharpCode.SharpZipLib.Core stream 导出Zip

.net core5.0 ICSharpCode.SharpZipLib.Core stream 导出Zip

作者:互联网

using ICSharpCode.SharpZipLib.Core;

  

 public FileResult ExportAll(int gradeId)
        {

            var zipObjects = _scoreImportService.ExportAll(gradeId);

            var outputMemoryStream = new MemoryStream();
            var zipStream = new ZipOutputStream(outputMemoryStream);

            zipStream.SetLevel(3);

            foreach (var item in zipObjects)
            {
                var data = item.XlsDataTable;
                var buffer = data.SaveToMemoryStream("学生成绩");

                var inputMemoryStream = new MemoryStream(buffer.ToArray());
                var zipEntry = new ZipEntry($"{item.Name}.xls");
                zipEntry.DateTime = DateTime.Now;

                zipStream.PutNextEntry(zipEntry);
                StreamUtils.Copy(inputMemoryStream, zipStream, new byte[4096]);
                zipStream.CloseEntry();
            }

            zipStream.IsStreamOwner = false;
            zipStream.Close();

            outputMemoryStream.Position = 0;

            //return outputMemoryStream.ToArray();


            var fileName = "全部";
            if (gradeId != -1)
            {
                fileName = gradeId + "届";
            }
            return File(outputMemoryStream, "application/zip;charset=UTF-8", fileName + "学生成绩单.zip");
        }

  

标签:Core,Zip,stream,gradeId,zipEntry,zipStream,outputMemoryStream,var,new
来源: https://www.cnblogs.com/y5s78/p/16183246.html