development:csharp:create_zip

Create zip archive in C# .NET Core 5

using SaveFileDialog sfd = new() { Filter = "Zip archive|*.zip", Title = "Save zip file", AddExtension = true, DefaultExt = ".zip" };
  sfd.ShowDialog();
  if (sfd.FileName != "")
  {
    using var memoryStream = new MemoryStream();
    using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
    {
      using var entryStreamFileDB = archive.CreateEntry("file.ext").Open();
      using StreamWriter streamWriterDB = new(entryStreamFileDB);
      streamWriterDB.Write("some text");
      streamWriterDB.Close();
 
      archive.CreateEntryFromFile(@"c:\tmp\some_file_on_disk.ext", "file1.ext");
    }
 
    using var fileStream = new FileStream(sfd.FileName, FileMode.Create);
    memoryStream.Seek(0, SeekOrigin.Begin);
    memoryStream.CopyTo(fileStream);
  }
  • development/csharp/create_zip.txt
  • Last modified: 2021/09/14 15:07
  • by 127.0.0.1