How to zip multiple files using only .net api in c#

前端 未结 9 1328
灰色年华
灰色年华 2020-12-01 09:04

I like to zip multiple files which are being created dynamically in my web application. Those files should be zipped. For this, i dont want to use any third-party tools. jus

9条回答
  •  我在风中等你
    2020-12-01 09:50

    Simple zip file with flat structure:

    using System.IO;
    using System.IO.Compression;
    
    private static void CreateZipFile(IEnumerable files, string archiveName)
    {
        using (var stream = File.OpenWrite(archiveName))
        using (ZipArchive archive = new ZipArchive(stream, System.IO.Compression.ZipArchiveMode.Create))
        {
             foreach (var item in files)
             {
                 archive.CreateEntryFromFile(item.FullName, item.Name, CompressionLevel.Optimal);                       
             }
         }
    }
    

    You need to add reference to System.IO.Compression and System.IO.Compression.FileSystem

提交回复
热议问题