-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArchiveUtilities.cs
72 lines (42 loc) · 2.18 KB
/
ArchiveUtilities.cs
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
using System.IO;
namespace Gsemac.IO.Compression {
public static class ArchiveUtilities {
// Public members
public static IArchive Open(string filePath, IFileFormat archiveFormat, IArchiveOptions archiveOptions = null) {
return ArchiveFactory.Default.Open(filePath, archiveFormat, archiveOptions);
}
public static IArchive Open(string filePath, IArchiveOptions archiveOptions = null) {
return ArchiveFactory.Default.Open(filePath, archiveOptions);
}
public static IArchive Open(string filePath, FileAccess fileAccess) {
return ArchiveFactory.Default.Open(filePath, fileAccess);
}
public static IArchive OpenRead(string filePath) {
return ArchiveFactory.Default.OpenRead(filePath);
}
public static IArchive OpenWrite(string filePath) {
return ArchiveFactory.Default.OpenWrite(filePath);
}
public static void Extract(string filePath) {
Extract(filePath, extractToNewFolder: true);
}
public static void Extract(string filePath, string directoryPath) {
using (IArchive archive = Open(filePath, FileAccess.Read))
archive.ExtractAllEntries(directoryPath);
}
public static void Extract(string filePath, bool extractToNewFolder) {
string outputPath = Path.GetDirectoryName(filePath);
if (extractToNewFolder)
outputPath = Path.Combine(outputPath, PathUtilities.GetFileNameWithoutExtension(filePath));
Extract(filePath, outputPath);
}
public static void ExtractFile(string archiveFilePath, string filePathInArchive, string outputFilePath) {
string directoryPath = Path.GetDirectoryName(outputFilePath);
if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
using (IArchive archive = OpenRead(archiveFilePath))
using (FileStream outputStream = File.OpenWrite(outputFilePath))
archive.ExtractEntry(archive.GetEntry(filePathInArchive), outputStream);
}
}
}