实用技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > ASP.NET > 实用技巧 > asp.net core在线生成多个文件

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

作者:假装我不帅

遇到安卓手机解压缩文件损坏问题时,可以考虑两种解决方案,方案一是使用SharpCompress库,它是一个开源项目,能够提供强大的压缩与解压功能,支持多种文件格式,方案二是采用aspose.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();
        }
        /// <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在线生成多个文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文