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

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

 更新時間:2024年11月04日 15:13:08   作者:假裝我不帥  
遇到安卓手機解壓縮文件損壞問題時,可以考慮兩種解決方案,方案一是使用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;
            // 創(chuàng)建一個FileStream,并將MemoryStream的內(nèi)容寫入該文件  
            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)
        {
            //如果有正版授權(quán)請寫入
            //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();
        }
    }
}

安卓手機解壓縮出現(xià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實現(xiàn)在線生成多個文件將多個文件打包為zip返回的文章就介紹到這了,更多相關(guān)asp.net core在線生成多個文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

余江县| 中阳县| 德安县| 三门县| 巴彦淖尔市| 璧山县| 宁南县| 芜湖市| 潞西市| 团风县| 喀喇| 台东县| 蓝山县| 根河市| 墨玉县| 义马市| 青铜峡市| 江津市| 石泉县| 西乌珠穆沁旗| 张家界市| 响水县| 高安市| 枣庄市| 咸宁市| 巴中市| 黎平县| 龙州县| 峡江县| 罗山县| 襄樊市| 淮滨县| 长葛市| 宜兴市| 科技| 共和县| 鲁甸县| 青神县| 武城县| 晋州市| 临湘市|