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
您可能感兴趣的文章
- 09-13web前端三大主流框架
- 09-13asp.NET是前端还是后端
- 09-13asp.net还有人用吗
- 07-25ASP.NET中Onclick与OnClientClick遇到的问题
- 07-25.NET WPF 可视化树(Visual Tree)详解
- 07-25.NET MCP 文档详细指南
- 07-25.NET 中的深拷贝实现方法详解
- 07-25Asp.NET Core WebApi 配置文件详细说明
- 07-25.NET Core中获取各种路径的的方法总结
- 07-25在 .NET 中 使用 ANTLR4构建语法分析器的方法


阅读排行
推荐教程
- 07-25在 .NET 中 使用 ANTLR4构建语法分析器的方法
- 07-25ASP.NET中Onclick与OnClientClick遇到的问题
- 07-25ASP.NET Core 模型验证消息的本地化新姿势详解
- 07-25Asp.NET Core WebApi 配置文件详细说明
- 09-13asp.NET是前端还是后端
- 07-25Math.NET Numerics 开源数学库安装使用详解
- 07-25.NET Core 实现缓存的预热的方式
- 03-31详解如何在.NET代码中使用本地部署的Deepseek语言模型
- 07-25.NET根据文件的哈希值筛选重复文件的实现思路
- 07-25.NET 中的深拷贝实现方法详解




