最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net core實(shí)現(xiàn)在線(xiàn)生成多個(gè)文件將多個(gè)文件打包為zip返回的操作

 更新時(shí)間:2024年11月04日 15:13:08   作者:假裝我不帥  
遇到安卓手機(jī)解壓縮文件損壞問(wèn)題時(shí),可以考慮兩種解決方案,方案一是使用SharpCompress庫(kù),它是一個(gè)開(kāi)源項(xiàng)目,能夠提供強(qiáng)大的壓縮與解壓功能,支持多種文件格式,方案二是采用aspose.zip庫(kù),這兩種方法都能有效解決文件損壞的問(wèn)題
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;
            // 創(chuàng)建一個(gè)FileStream,并將MemoryStream的內(nèi)容寫(xiě)入該文件  
            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測(cè)試</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)
        {
            //如果有正版授權(quán)請(qǐng)寫(xiě)入
            //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();
        }
    }
}

安卓手機(jī)解壓縮出現(xiàn)損壞的問(wèn)題

方案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

到此這篇關(guān)于asp.net core實(shí)現(xiàn)在線(xiàn)生成多個(gè)文件將多個(gè)文件打包為zip返回的文章就介紹到這了,更多相關(guān)asp.net core在線(xiàn)生成多個(gè)文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

上虞市| 平谷区| 大竹县| 海口市| 嘉峪关市| 福清市| 昆山市| 徐闻县| 赫章县| 新巴尔虎左旗| 岳西县| 江孜县| 宁国市| 新郑市| 合川市| 东阿县| 山西省| 伊川县| 德州市| 庄浪县| 泌阳县| 香河县| 桦川县| 湄潭县| 汤原县| 信丰县| 兴义市| 宁强县| 黄骅市| 长沙县| 西盟| 巴东县| 吴堡县| 紫阳县| 铜鼓县| 曲水县| 军事| 崇信县| 任丘市| 灌阳县| 历史|