diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2021-05-25 01:12:38 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2021-05-25 01:12:38 +0200 |
| commit | 379083120112a0a6d322531a1d5ff454c4411f2c (patch) | |
| tree | 36a16b8f6850494e460ab32ad1839c1326d68d73 /src/IOL.Helpers/InMemoryZipArchive.cs | |
| parent | 45f1dff4e830d6594b2669d82c67a25263b693c8 (diff) | |
| download | dotnet-helpers-379083120112a0a6d322531a1d5ff454c4411f2c.tar.xz dotnet-helpers-379083120112a0a6d322531a1d5ff454c4411f2c.zip | |
Add InMemoryZipArchive
Diffstat (limited to 'src/IOL.Helpers/InMemoryZipArchive.cs')
| -rw-r--r-- | src/IOL.Helpers/InMemoryZipArchive.cs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/IOL.Helpers/InMemoryZipArchive.cs b/src/IOL.Helpers/InMemoryZipArchive.cs new file mode 100644 index 0000000..8d3fc94 --- /dev/null +++ b/src/IOL.Helpers/InMemoryZipArchive.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; + +namespace IOL.Helpers +{ + public static class InMemoryZipArchive + { + public static byte[] Create(IEnumerable<InMemoryFile> files, string unixPermissionString = "664") { + using var archiveStream = new MemoryStream(); + using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true)) { + foreach (var file in files) { + var zipArchiveEntry = archive.CreateEntry(file.FileName, CompressionLevel.Fastest); + zipArchiveEntry.ExternalAttributes |= Convert.ToInt32(unixPermissionString, 8) << 16; + using var zipStream = zipArchiveEntry.Open(); + zipStream.Write(file.Content, 0, file.Content.Length); + } + } + + return archiveStream.ToArray(); + } + + public class InMemoryFile + { + public string FileName { get; set; } + public byte[] Content { get; set; } + } + } +} |
