-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathZipArchiveExtensions.cs
More file actions
37 lines (31 loc) · 1.33 KB
/
ZipArchiveExtensions.cs
File metadata and controls
37 lines (31 loc) · 1.33 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
using System.Collections.Generic;
using System.Threading.Tasks;
namespace System.IO.Compression;
public static class ZipArchiveExtensions
{
/// <summary>
/// Creates a new text file in <paramref name="zip"/> and writes the <paramref name="lines"/> into it.
/// </summary>
public static async Task CreateTextEntryAsync(this ZipArchive zip, string entryName, IEnumerable<string?>? lines)
{
await using var writer = new StreamWriter(await zip.CreateEntry(entryName).OpenAsync());
if (lines == null) return;
foreach (var line in lines)
{
await writer.WriteLineAsync(line ?? string.Empty);
}
}
/// <summary>
/// Creates a new text file in <paramref name="zip"/> and writes the <paramref name="text"/> into it.
/// </summary>
public static Task CreateTextEntryAsync(this ZipArchive zip, string entryName, string text) =>
zip.CreateTextEntryAsync(entryName, [text]);
/// <summary>
/// Creates a new binary file in <paramref name="zip"/> and writes the <paramref name="data"/> into it.
/// </summary>
public static async Task CreateBinaryEntryAsync(this ZipArchive zip, string entryName, ReadOnlyMemory<byte> data)
{
await using var stream = await zip.CreateEntry(entryName).OpenAsync();
await stream.WriteAsync(data);
}
}