当前位置: 代码网 > it编程>编程语言>Asp.net > asp.net core实现在线生成多个文件将多个文件打包为zip返回的操作

asp.net core实现在线生成多个文件将多个文件打包为zip返回的操作

2024年11月25日 Asp.net 我要评论
using aspose.words;using aspose.words.saving;using system.io.compression;namespace consoleapp4{ i
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在线生成多个文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com