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();
}
/// <summary>
/// 获取html代码
/// </summary>
/// <returns></returns>
static string gethtml()
{
var htmldata = @"
<!doctype html>
<html lang=""zh"">
<head>
<meta charset=""utf-8"">
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
<title>aspose测试</title>
<style>
table {
border: 1px solid #000;
}
</style>
</head>
<body>
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
<tr>
<td>小明</td>
<td>20</td>
</tr>
<tr>
<td>小红</td>
<td>22</td>
</tr>
<tr>
<td>小华</td>
<td>18</td>
</tr>
</table>
</body>
</html>
";
return htmldata;
}
static byte[] html2word(string htmlcontent)
{
//如果有正版授权请写入
//var memorystream = new memorystream(convert.frombase64string(""));
//var license = new aspose.words.license();
//license.setlicense(memorystream);
var doc = new aspose.words.document();
doc.removeallchildren();
aspose.words.documentbuilder builder = new documentbuilder(doc);
builder.inserthtml(htmlcontent);
//var now = datetime.now.tostring("yyyy_mm_dd_hh_mm_ss");
//var docpath = now + ".docx";
//doc.save(docpath);
var resultmemorystream = new memorystream();
doc.save(resultmemorystream, saveoptions.createsaveoptions(saveformat.docx));
return resultmemorystream.toarray();
}
}
}

安卓手机解压缩出现损坏的问题
方案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在线生成多个文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论