aboutsummaryrefslogtreecommitdiffstats
path: root/src/IOL.Helpers/InMemoryZipArchive.cs
blob: 8d3fc94c440c0a56811606e0be952745a79c4907 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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; }
		}
	}
}