-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathZipCompression.cs
More file actions
222 lines (203 loc) · 8.81 KB
/
ZipCompression.cs
File metadata and controls
222 lines (203 loc) · 8.81 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using BytecodeApi.Data;
using BytecodeApi.Extensions;
using System.IO.Compression;
namespace BytecodeApi.IO;
/// <summary>
/// Class to create and decompress ZIP archive files from <see cref="Blob" /> objects.
/// </summary>
public static class ZipCompression
{
/// <summary>
/// Creates a ZIP archive from a single <see cref="Blob" /> object and returns a <see cref="byte" />[] representing the compressed archive.
/// </summary>
/// <param name="blob">The <see cref="Blob" /> object to create the ZIP archive from.</param>
/// <returns>
/// A <see cref="byte" />[] representing the compressed ZIP archive with the specified <see cref="Blob" /> object.
/// </returns>
public static byte[] Compress(Blob blob)
{
Check.ArgumentNull(blob);
using MemoryStream memoryStream = new();
Compress(blob, memoryStream);
return memoryStream.ToArray();
}
/// <summary>
/// Creates a ZIP archive from a single <see cref="Blob" /> object and writes the compressed archive to <paramref name="stream" />.
/// </summary>
/// <param name="blob">The <see cref="Blob" /> object to create the ZIP archive from.</param>
/// <param name="stream">The <see cref="Stream" /> to write the compressed archive to.</param>
public static void Compress(Blob blob, Stream stream)
{
Compress(blob, stream, false);
}
/// <summary>
/// Creates a ZIP archive from a single <see cref="Blob" /> object and writes the compressed archive to <paramref name="stream" />.
/// </summary>
/// <param name="blob">The <see cref="Blob" /> object to create the ZIP archive from.</param>
/// <param name="stream">The <see cref="Stream" /> to write the compressed archive to.</param>
/// <param name="leaveOpen">A <see cref="bool" /> value indicating whether to leave <paramref name="stream" /> open.</param>
public static void Compress(Blob blob, Stream stream, bool leaveOpen)
{
Check.ArgumentNull(blob);
Check.ArgumentNull(stream);
using ZipArchive archive = new(stream, ZipArchiveMode.Create, leaveOpen);
archive.CreateEntry(blob.Name, blob.Content);
}
/// <summary>
/// Creates a ZIP archive from the specified collection of <see cref="Blob" /> objects and returns a <see cref="byte" />[] representing the compressed archive.
/// </summary>
/// <param name="blobs">A collection of <see cref="Blob" /> objects to create the ZIP archive from.</param>
/// <returns>
/// A <see cref="byte" />[] representing the compressed ZIP archive with all <see cref="Blob" /> objects from the specified collection.
/// </returns>
public static byte[] Compress(BlobCollection blobs)
{
Check.ArgumentNull(blobs);
using MemoryStream memoryStream = new();
Compress(blobs, memoryStream);
return memoryStream.ToArray();
}
/// <summary>
/// Creates a ZIP archive from the specified collection of <see cref="Blob" /> objects and writes the compressed archive to <paramref name="stream" />.
/// </summary>
/// <param name="blobs">A collection of <see cref="Blob" /> objects to create the ZIP archive from.</param>
/// <param name="stream">The <see cref="Stream" /> to write the compressed archive to.</param>
public static void Compress(BlobCollection blobs, Stream stream)
{
Compress(blobs, stream, false);
}
/// <summary>
/// Creates a ZIP archive from the specified collection of <see cref="Blob" /> objects and writes the compressed archive to <paramref name="stream" />.
/// </summary>
/// <param name="blobs">A collection of <see cref="Blob" /> objects to create the ZIP archive from.</param>
/// <param name="stream">The <see cref="Stream" /> to write the compressed archive to.</param>
/// <param name="leaveOpen">A <see cref="bool" /> value indicating whether to leave <paramref name="stream" /> open.</param>
public static void Compress(BlobCollection blobs, Stream stream, bool leaveOpen)
{
Check.ArgumentNull(blobs);
Check.ArgumentNull(stream);
using ZipArchive archive = new(stream, ZipArchiveMode.Create, leaveOpen);
foreach (Blob blob in blobs)
{
archive.CreateEntry(blob.Name, blob.Content);
}
}
/// <summary>
/// Creates a ZIP archive from the specified <see cref="BlobTree" /> and returns a <see cref="byte" />[] representing the compressed archive.
/// </summary>
/// <param name="blobs">A tree of <see cref="Blob" /> objects to create the ZIP archive from.</param>
/// <returns>
/// A <see cref="byte" />[] representing the compressed ZIP archive with all <see cref="Blob" /> objects from the specified tree.
/// </returns>
public static byte[] Compress(BlobTree blobs)
{
Check.ArgumentNull(blobs);
using MemoryStream memoryStream = new();
Compress(blobs, memoryStream);
return memoryStream.ToArray();
}
/// <summary>
/// Creates a ZIP archive from the specified <see cref="BlobTree" /> and writes the compressed archive to <paramref name="stream" />.
/// </summary>
/// <param name="blobs">A tree of <see cref="Blob" /> objects to create the ZIP archive from.</param>
/// <param name="stream">The <see cref="Stream" /> to write the compressed archive to.</param>
public static void Compress(BlobTree blobs, Stream stream)
{
Compress(blobs, stream, false);
}
/// <summary>
/// Creates a ZIP archive from the specified <see cref="BlobTree" /> and writes the compressed archive to <paramref name="stream" />.
/// </summary>
/// <param name="blobs">A tree of <see cref="Blob" /> objects to create the ZIP archive from.</param>
/// <param name="stream">The <see cref="Stream" /> to write the compressed archive to.</param>
/// <param name="leaveOpen">A <see cref="bool" /> value indicating whether to leave <paramref name="stream" /> open.</param>
public static void Compress(BlobTree blobs, Stream stream, bool leaveOpen)
{
Check.ArgumentNull(blobs);
Check.ArgumentNull(stream);
using ZipArchive archive = new(stream, ZipArchiveMode.Create, leaveOpen);
CreateEntries("", blobs.Root);
void CreateEntries(string path, BlobTreeNode node)
{
foreach (BlobTreeNode childNode in node.Nodes)
{
CreateEntries(Path.Combine(path, childNode.Name), childNode);
}
foreach (Blob blob in node.Blobs)
{
archive.CreateEntry(Path.Combine(path, blob.Name), blob.Content);
}
}
}
/// <summary>
/// Creates a <see cref="BlobTree" /> from the ZIP archive read from the specified file.
/// </summary>
/// <param name="path">A <see cref="string" /> representing the path to a ZIP file.</param>
/// <returns>
/// The <see cref="BlobTree" /> this method creates.
/// </returns>
public static BlobTree Decompress(string path)
{
Check.ArgumentNull(path);
Check.FileNotFound(path);
using FileStream file = File.OpenRead(path);
return Decompress(file);
}
/// <summary>
/// Creates a <see cref="BlobTree" /> from the ZIP archive in the specified <see cref="byte" />[].
/// </summary>
/// <param name="file">A <see cref="byte" />[] that represents a ZIP archive.</param>
/// <returns>
/// The <see cref="BlobTree" /> this method creates.
/// </returns>
public static BlobTree Decompress(byte[] file)
{
Check.ArgumentNull(file);
using MemoryStream memoryStream = new(file);
return Decompress(memoryStream);
}
/// <summary>
/// Creates a <see cref="BlobTree" /> from the ZIP archive read from the specified <see cref="Stream" />.
/// </summary>
/// <param name="stream">The <see cref="Stream" /> from which to read the ZIP archive from.</param>
/// <returns>
/// The <see cref="BlobTree" /> this method creates.
/// </returns>
public static BlobTree Decompress(Stream stream)
{
return Decompress(stream, false);
}
/// <summary>
/// Creates a <see cref="BlobTree" /> from the ZIP archive read from the specified <see cref="Stream" />.
/// </summary>
/// <param name="stream">The <see cref="Stream" /> from which to read the ZIP archive from.</param>
/// <param name="leaveOpen">A <see cref="bool" /> value indicating whether to leave <paramref name="stream" /> open.</param>
/// <returns>
/// The <see cref="BlobTree" /> this method creates.
/// </returns>
public static BlobTree Decompress(Stream stream, bool leaveOpen)
{
Check.ArgumentNull(stream);
BlobTree tree = new();
using ZipArchive archive = new(stream, ZipArchiveMode.Read, leaveOpen);
foreach (ZipArchiveEntry entry in archive.Entries)
{
BlobTreeNode node = tree.Root;
foreach (string pathPart in entry.FullName.TrimEndString(entry.Name, StringComparison.OrdinalIgnoreCase, true).TrimEnd('\\', '/').Split('\\', StringSplitOptions.RemoveEmptyEntries))
{
if (node.Node(pathPart, true) is BlobTreeNode childNode)
{
node = childNode;
}
else
{
BlobTreeNode newChildNode = new(pathPart);
node.Nodes.Add(newChildNode);
node = newChildNode;
}
}
node.Blobs.Add(new(entry.Name, entry.GetContent()));
}
return tree;
}
}