-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArchiveFactoryExtensions.cs
More file actions
137 lines (84 loc) · 4.36 KB
/
ArchiveFactoryExtensions.cs
File metadata and controls
137 lines (84 loc) · 4.36 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
using System;
using System.IO;
namespace Gsemac.IO.Compression {
public static class ArchiveFactoryExtensions {
// Public members
public static IArchive Open(this IArchiveFactory archiveFactory, Stream stream) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
if (stream is null)
throw new ArgumentNullException(nameof(stream));
return archiveFactory.Open(stream, GetArchiveOptionsFromStream(stream));
}
public static IArchive Open(this IArchiveFactory archiveFactory, Stream stream, IArchiveOptions options) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
if (stream is null)
throw new ArgumentNullException(nameof(stream));
if (options is null)
options = GetArchiveOptionsFromStream(stream);
stream = FileFormatFactory.Default.FromStream(stream, out IFileFormat archiveFormat);
return archiveFactory.Open(stream, archiveFormat, archiveOptions: options);
}
public static IArchive Open(this IArchiveFactory archiveFactory, Stream stream, IFileFormat format) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
if (stream is null)
throw new ArgumentNullException(nameof(stream));
if (format is null)
stream = FileFormatFactory.Default.FromStream(stream, out format);
return archiveFactory.Open(stream, format, ArchiveOptions.Default);
}
public static IArchive Open(this IArchiveFactory archiveFactory, string filePath) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
return archiveFactory.Open(filePath, options: null);
}
public static IArchive Open(this IArchiveFactory archiveFactory, string filePath, FileAccess fileAccess) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
return archiveFactory.Open(filePath, new ArchiveOptions() {
FileAccess = fileAccess,
});
}
public static IArchive Open(this IArchiveFactory archiveFactory, string filePath, IArchiveOptions options) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
return archiveFactory.Open(filePath, null, options);
}
public static IArchive Open(this IArchiveFactory archiveFactory, string filePath, IFileFormat format, IArchiveOptions options) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
if (format is null)
format = FileFormatFactory.Default.FromFileExtension(filePath);
FileAccess fileAccess = options?.FileAccess ?? FileAccess.ReadWrite;
FileMode fileMode = fileAccess == FileAccess.Read ? FileMode.Open : FileMode.OpenOrCreate;
FileStream fileStream = new FileStream(filePath, fileMode, fileAccess);
try {
return archiveFactory.Open(fileStream, format, options);
}
catch {
fileStream.Dispose();
throw;
}
}
public static IArchive OpenRead(this IArchiveFactory archiveFactory, string filePath) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
return archiveFactory.Open(filePath, FileAccess.Read);
}
public static IArchive OpenWrite(this IArchiveFactory archiveFactory, string filePath) {
if (archiveFactory is null)
throw new ArgumentNullException(nameof(archiveFactory));
return archiveFactory.Open(filePath, FileAccess.Write);
}
// Private members
private static IArchiveOptions GetArchiveOptionsFromStream(Stream stream) {
if (stream is null)
throw new ArgumentNullException(nameof(stream));
return new ArchiveOptions() {
FileAccess = StreamUtilities.GetFileAccessFromStream(stream),
};
}
}
}