asp.net core实现在线生成多个文件将多个文件打包为zip返回的操作
using Aspose.Words; using Aspose.Words.Saving; using System.IO.Compression; namespace ConsoleApp4 { internal class Program { static void Main(string[] args) { var html = GetHtml(); using var memoryStream = new MemoryStream(); using var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true); var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"); for (int i = 0; i < 3; i++) { var docPath = now + "_" + i + ".docx"; var entry = zipArchive.CreateEntry(docPath, System.IO.Compression.CompressionLevel.Fastest); using var entryStream = entry.Open(); var bytes = Html2Word(html); var stream = new MemoryStream(bytes); stream.CopyTo(entryStream); } memoryStream.Position = 0; // 创建一个FileStream,并将MemoryStream的内容写入该文件 string filePath = now + ".zip"; using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { memoryStream.CopyTo(fileStream); } //如果是asp.net core接口返回,代码如下 //return File(memoryStream, "application/zip", filePath); Console.WriteLine("压缩完成"); Console.ReadKey(); } ////// 获取html代码 /// ///static string GetHtml() { var htmlData = @" Aspose测试
姓名 | 年龄 |
---|---|
小明 | 20 |
小红 | 22 |
小华 | 18 |
安卓手机解压缩出现损坏的问题
方案1 使用SharpCompress
using Aspose.Words; using Aspose.Words.Saving; using SharpCompress.Archives.Zip; using System; using System.IO; namespace ZipStu02 { internal class Program { static void Main(string[] args) { var html = GetHtml(); var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"); var archive = ZipArchive.Create(); for (int i = 0; i < 3; i++) { var docName = now + "_" + i + ".docx"; var bytes = Html2Word(html); var stream = new MemoryStream(bytes); archive.AddEntry(docName, stream); } string filePath = now + ".zip"; using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { archive.SaveTo(fileStream); } Console.WriteLine("生成成功"); Console.ReadKey(); } } }
方案2 使用aspose.zip
//var license = new Aspose.Zip.License(); //license.SetLicense("Aspose.Total.lic"); var html = GetHtml(); //Html2Word(html); var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"); var archive = new Archive(); for (int i = 0; i < 3; i++) { var docName = now + "_" + i + ".docx"; var bytes = Html2Word(html); var stream = new MemoryStream(bytes); archive.CreateEntry(docName, stream); } string filePath = now + ".zip"; using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { archive.Save(fileStream); } Console.WriteLine("生成成功"); Console.ReadKey();
参考
https://docs.aspose.com/zip/net/
https://github.com/adamhathcock/sharpcompress/wiki/API-Examples
到此这篇关于asp.net core实现在线生成多个文件将多个文件打包为zip返回的文章就介绍到这了,更多相关asp.net core在线生成多个文件内容请搜索科站长以前的文章或继续浏览下面的相关文章希望大家以后多多支持科站长!
上一篇:asp.net core 跨域配置不起作用的原因分析及解决方案
栏 目:ASP.NET
下一篇:.NET 8 高性能跨平台图像处理库 ImageSharp 详解
本文标题:asp.net core实现在线生成多个文件将多个文件打包为zip返回的操作
本文地址:https://www.fushidao.cc/wangluobiancheng/3281.html
您可能感兴趣的文章
- 03-31详解如何在.NET代码中使用本地部署的Deepseek语言模型
- 02-06.net core如何使用Nacos注册中心
- 01-28使用.NET8构建一个高效的时间日期帮助类
- 01-26.NET Core GC压缩(compact_phase)底层原理解析
- 01-24在ASP.NET中读写TXT文本文件的多种方法
- 01-24在ASP.NET中读写XML数据的多种方法
- 01-24.NET轻松实现Excel转PDF的三种方法详解
- 01-23.NET9 AOT部署方案详解
- 01-23.NET NativeAOT 用法指南
- 01-23iis部署前后端分离项目全过程(Vuet前端和.NET6后端)


阅读排行
推荐教程
- 03-31详解如何在.NET代码中使用本地部署的Deepseek语言模型
- 11-23移动互联网广告有哪些模式?
- 11-22.net 应对网站访问压力的方案总结
- 11-22详解ASP.NET提取多层嵌套json数据的方法
- 11-23网站投放广告如何达到最好的效果
- 11-22.net 应对网站访问压力的方案总结
- 11-23网站打开速度慢解决办法
- 11-23草根站长为什么喜欢做门户站
- 11-22ASP.NET MVC分页问题解决
- 11-22ASP.NET编程简单实现生成静态页面的方法