-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArchiveExtensions.cs
More file actions
165 lines (93 loc) · 4.7 KB
/
ArchiveExtensions.cs
File metadata and controls
165 lines (93 loc) · 4.7 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
using System;
using System.IO;
using System.Linq;
namespace Gsemac.IO.Compression {
public static class ArchiveExtensions {
public static bool AddFile(this IArchive archive, string filePath, bool overwrite = true) {
string entryName = PathUtilities.GetFileName(filePath);
return archive.AddFile(filePath, entryName, overwrite);
}
public static bool AddFile(this IArchive archive, string filePath, string entryName, bool overwrite = true) {
if (!overwrite && archive.ContainsEntry(entryName))
return false;
Stream stream = File.OpenRead(filePath);
try {
archive.AddEntry(stream, entryName);
}
catch (Exception) {
// If we fail to add the entry, make sure we close the stream we opened.
stream.Close();
throw;
}
return true;
}
public static void AddAllFiles(this IArchive archive, string directoryPath, bool overwrite = true) {
foreach (string filePath in Directory.EnumerateFiles(directoryPath, "*", SearchOption.AllDirectories))
archive.AddFile(filePath, PathUtilities.GetRelativePath(filePath, directoryPath), overwrite);
}
public static IArchiveEntry AddEntry(this IArchive archive, Stream stream, string entryName) {
return archive.AddEntry(stream, entryName, ArchiveEntryOptions.Default);
}
public static bool ContainsEntry(this IArchive archive, string entryName) {
return archive.GetEntry(entryName) is object;
}
public static bool ContainsEntry(this IArchive archive, IArchiveEntry entry) {
return archive.GetEntries().Any(e => e.Equals(entry));
}
public static bool DeleteEntry(this IArchive archive, string entryName) {
IArchiveEntry entry = archive.GetEntry(entryName);
if (entry is object)
archive.DeleteEntry(entry);
return entry is object;
}
public static void ExtractEntry(this IArchive archive, IArchiveEntry entry, string filePath) {
if (entry is null)
throw new ArgumentNullException(nameof(entry));
if (filePath is null)
throw new ArgumentNullException(nameof(filePath));
// Some implementations do not create the file path's directory automatically.
string directoryPath = Path.GetDirectoryName(filePath);
bool createdDirectory = false;
if (!string.IsNullOrWhiteSpace(directoryPath) && !Directory.Exists(directoryPath)) {
Directory.CreateDirectory(directoryPath);
createdDirectory = true;
}
try {
using (FileStream fs = File.OpenWrite(filePath))
archive.ExtractEntry(entry, fs);
}
finally {
// Delete the output directory if it is empty (we weren't able to extract the file).
if (createdDirectory && !Directory.EnumerateFileSystemEntries(directoryPath).Any())
Directory.Delete(directoryPath);
}
}
public static void ExtractEntry(this IArchive archive, string entryName, string filePath) {
IArchiveEntry entry = archive.GetEntry(entryName);
archive.ExtractEntry(entry, filePath);
}
public static void ExtractEntry(this IArchive archive, string entryName, Stream outputStream) {
IArchiveEntry entry = archive.GetEntry(entryName);
archive.ExtractEntry(entry, outputStream);
}
public static void ExtractAllEntries(this IArchive archive, string directoryPath) {
bool createdDirectory = false;
if (!string.IsNullOrWhiteSpace(directoryPath) && !Directory.Exists(directoryPath)) {
Directory.CreateDirectory(directoryPath);
createdDirectory = true;
}
try {
foreach (IArchiveEntry entry in archive.GetEntries())
archive.ExtractEntry(entry, Path.Combine(directoryPath, PathUtilities.NormalizeDirectorySeparators(entry.Name)));
}
finally {
// Delete the output directory if it is empty (we weren't able to extract the files).
if (createdDirectory && !Directory.EnumerateFileSystemEntries(directoryPath).Any())
Directory.Delete(directoryPath);
}
}
public static void ExtractAllEntries(this IArchive archive) {
archive.ExtractAllEntries(Directory.GetCurrentDirectory());
}
}
}