Skip to content
0.2.0-dev Download ZIP
199 lines ·6.5 KB Raw
1using System.Buffers.Binary;
2using System.IO.Compression;
3using System.IO.Hashing;
4using System.Text;
5
6namespace EvilEngine.Adapters.Tests.Support;
7
8public enum ZipWriterEncryption
9{
10 None,
11 ZipCrypto,
12 Aes256
13}
14
15public sealed class ZipWriter
16{
17 private readonly List<Item> _items = [];
18
19 public bool Zip64 { get; init; }
20
21 public string Comment { get; init; } = "";
22
23 public bool DataDescriptors { get; init; }
24
25 public ZipWriter Add(string name, byte[] data, bool deflate = false,
26 ZipWriterEncryption encryption = ZipWriterEncryption.None, bool utf8Flag = false, byte[]? rawName = null)
27 {
28 _items.Add(new Item(name, data, deflate, encryption, utf8Flag, rawName));
29 return this;
30 }
31
32 public ZipWriter Add(string name, string text, bool deflate = false,
33 ZipWriterEncryption encryption = ZipWriterEncryption.None)
34 {
35 return Add(name, Encoding.UTF8.GetBytes(text), deflate, encryption);
36 }
37
38 public ZipWriter Folder(string name)
39 {
40 return Add(name.TrimEnd('/') + "/", []);
41 }
42
43 public byte[] Build()
44 {
45 using var output = new MemoryStream();
46 var central = new MemoryStream();
47 foreach (var item in _items)
48 {
49 var name = item.RawName ?? Encoding.UTF8.GetBytes(item.Name);
50 var stored = item.Deflate ? Deflate(item.Data) : item.Data;
51 var crc = item.Encryption == ZipWriterEncryption.Aes256 ? 0u : Crc32.HashToUInt32(item.Data);
52 var method = item.Encryption == ZipWriterEncryption.Aes256 ? (ushort)99 :
53 item.Deflate ? (ushort)8 : (ushort)0;
54 ushort flags = 0;
55 if (item.Encryption != ZipWriterEncryption.None)
56 flags |= 1;
57 if (item.Utf8Flag)
58 flags |= 0x800;
59 if (DataDescriptors)
60 flags |= 8;
61 var payload = item.Encryption == ZipWriterEncryption.None
62 ? stored
63 : [.. Enumerable.Repeat((byte)0xA5, 12), .. stored];
64 var extra = new MemoryStream();
65 if (Zip64)
66 {
67 U16(extra, 0x0001);
68 U16(extra, 24);
69 I64(extra, item.Data.Length);
70 I64(extra, payload.Length);
71 I64(extra, output.Position);
72 }
73
74 if (item.Encryption == ZipWriterEncryption.Aes256)
75 {
76 U16(extra, 0x9901);
77 U16(extra, 7);
78 U16(extra, 2);
79 extra.Write("AE"u8);
80 extra.WriteByte(3);
81 U16(extra, item.Deflate ? (ushort)8 : (ushort)0);
82 }
83
84 var header = output.Position;
85 output.Write("PK\u0003\u0004"u8);
86 U16(output, Zip64 ? (ushort)45 : (ushort)20);
87 U16(output, flags);
88 U16(output, method);
89 U32(output, 0);
90 U32(output, DataDescriptors ? 0 : crc);
91 U32(output, DataDescriptors ? 0 : Zip64 ? uint.MaxValue : (uint)payload.Length);
92 U32(output, DataDescriptors ? 0 : Zip64 ? uint.MaxValue : (uint)item.Data.Length);
93 U16(output, (ushort)name.Length);
94 U16(output, (ushort)extra.Length);
95 output.Write(name);
96 output.Write(extra.ToArray());
97 output.Write(payload);
98 if (DataDescriptors)
99 {
100 output.Write("PK\u0007\u0008"u8);
101 U32(output, crc);
102 U32(output, (uint)payload.Length);
103 U32(output, (uint)item.Data.Length);
104 }
105
106 central.Write("PK\u0001\u0002"u8);
107 U16(central, 0x031E);
108 U16(central, Zip64 ? (ushort)45 : (ushort)20);
109 U16(central, flags);
110 U16(central, method);
111 U32(central, 0);
112 U32(central, crc);
113 U32(central, Zip64 ? uint.MaxValue : (uint)payload.Length);
114 U32(central, Zip64 ? uint.MaxValue : (uint)item.Data.Length);
115 U16(central, (ushort)name.Length);
116 U16(central, (ushort)extra.Length);
117 U16(central, 0);
118 U16(central, 0);
119 U16(central, 0);
120 U32(central, 0);
121 U32(central, Zip64 ? uint.MaxValue : (uint)header);
122 central.Write(name);
123 central.Write(extra.ToArray());
124 }
125
126 var centralOffset = output.Position;
127 output.Write(central.ToArray());
128 var comment = Encoding.UTF8.GetBytes(Comment);
129 if (Zip64)
130 {
131 var end64 = output.Position;
132 output.Write("PK\u0006\u0006"u8);
133 I64(output, 44);
134 U16(output, 45);
135 U16(output, 45);
136 U32(output, 0);
137 U32(output, 0);
138 I64(output, _items.Count);
139 I64(output, _items.Count);
140 I64(output, central.Length);
141 I64(output, centralOffset);
142 output.Write("PK\u0006\u0007"u8);
143 U32(output, 0);
144 I64(output, end64);
145 U32(output, 1);
146 }
147
148 output.Write("PK\u0005\u0006"u8);
149 U16(output, 0);
150 U16(output, 0);
151 U16(output, Zip64 ? ushort.MaxValue : (ushort)_items.Count);
152 U16(output, Zip64 ? ushort.MaxValue : (ushort)_items.Count);
153 U32(output, Zip64 ? uint.MaxValue : (uint)central.Length);
154 U32(output, Zip64 ? uint.MaxValue : (uint)centralOffset);
155 U16(output, (ushort)comment.Length);
156 output.Write(comment);
157 return output.ToArray();
158 }
159
160 private static byte[] Deflate(byte[] data)
161 {
162 using var output = new MemoryStream();
163 using (var deflate = new DeflateStream(output, CompressionLevel.Optimal, true))
164 {
165 deflate.Write(data);
166 }
167
168 return output.ToArray();
169 }
170
171 private static void U16(Stream stream, ushort value)
172 {
173 Span<byte> bytes = stackalloc byte[2];
174 BinaryPrimitives.WriteUInt16LittleEndian(bytes, value);
175 stream.Write(bytes);
176 }
177
178 private static void U32(Stream stream, uint value)
179 {
180 Span<byte> bytes = stackalloc byte[4];
181 BinaryPrimitives.WriteUInt32LittleEndian(bytes, value);
182 stream.Write(bytes);
183 }
184
185 private static void I64(Stream stream, long value)
186 {
187 Span<byte> bytes = stackalloc byte[8];
188 BinaryPrimitives.WriteInt64LittleEndian(bytes, value);
189 stream.Write(bytes);
190 }
191
192 private sealed record Item(
193 string Name,
194 byte[] Data,
195 bool Deflate,
196 ZipWriterEncryption Encryption,
197 bool Utf8Flag,
198 byte[]? RawName);
199}