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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Dtf.Compression.Zip
{
using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
public partial class ZipEngine
{
/// <summary>
/// Extracts files from a zip archive or archive chain.
/// </summary>
/// <param name="streamContext">A context interface to handle opening
/// and closing of archive and file streams.</param>
/// <param name="fileFilter">An optional predicate that can determine
/// which files to process.</param>
/// <exception cref="ArchiveException">The archive provided
/// by the stream context is not valid.</exception>
/// <remarks>
/// The <paramref name="fileFilter"/> predicate takes an internal file
/// path and returns true to include the file or false to exclude it.
/// </remarks>
public override void Unpack(
IUnpackStreamContext streamContext,
Predicate<string> fileFilter)
{
if (streamContext == null)
{
throw new ArgumentNullException("streamContext");
}
lock (this)
{
IList<ZipFileHeader> allHeaders = this.GetCentralDirectory(streamContext);
if (allHeaders == null)
{
throw new ZipException("Zip central directory not found.");
}
IList<ZipFileHeader> headers = new List<ZipFileHeader>(allHeaders.Count);
foreach (ZipFileHeader header in allHeaders)
{
if (!header.IsDirectory &&
(fileFilter == null || fileFilter(header.fileName)))
{
headers.Add(header);
}
}
this.ResetProgressData();
// Count the total number of files and bytes to be compressed.
this.totalFiles = headers.Count;
foreach (ZipFileHeader header in headers)
{
long compressedSize;
long uncompressedSize;
long localHeaderOffset;
int archiveNumber;
uint crc;
header.GetZip64Fields(
out compressedSize,
out uncompressedSize,
out localHeaderOffset,
out archiveNumber,
out crc);
this.totalFileBytes += uncompressedSize;
if (archiveNumber >= this.totalArchives)
{
this.totalArchives = (short) (archiveNumber + 1);
}
}
this.currentArchiveNumber = -1;
this.currentFileNumber = -1;
Stream archiveStream = null;
try
{
foreach (ZipFileHeader header in headers)
{
this.currentFileNumber++;
this.UnpackOneFile(streamContext, header, ref archiveStream);
}
}
finally
{
if (archiveStream != null)
{
streamContext.CloseArchiveReadStream(
0, String.Empty, archiveStream);
this.currentArchiveNumber--;
this.OnProgress(ArchiveProgressType.FinishArchive);
}
}
}
}
/// <summary>
/// Unpacks a single file from an archive or archive chain.
/// </summary>
private void UnpackOneFile(
IUnpackStreamContext streamContext,
ZipFileHeader header,
ref Stream archiveStream)
{
ZipFileInfo fileInfo = null;
Stream fileStream = null;
try
{
Converter<Stream, Stream> compressionStreamCreator;
if (!ZipEngine.decompressionStreamCreators.TryGetValue(
header.compressionMethod, out compressionStreamCreator))
{
// Silently skip files of an unsupported compression method.
return;
}
long compressedSize;
long uncompressedSize;
long localHeaderOffset;
int archiveNumber;
uint crc;
header.GetZip64Fields(
out compressedSize,
out uncompressedSize,
out localHeaderOffset,
out archiveNumber,
out crc);
if (this.currentArchiveNumber != archiveNumber + 1)
{
if (archiveStream != null)
{
streamContext.CloseArchiveReadStream(
this.currentArchiveNumber,
String.Empty,
archiveStream);
archiveStream = null;
this.OnProgress(ArchiveProgressType.FinishArchive);
this.currentArchiveName = null;
}
this.currentArchiveNumber = (short) (archiveNumber + 1);
this.currentArchiveBytesProcessed = 0;
this.currentArchiveTotalBytes = 0;
archiveStream = this.OpenArchive(
streamContext, this.currentArchiveNumber);
FileStream archiveFileStream = archiveStream as FileStream;
this.currentArchiveName = (archiveFileStream != null ?
Path.GetFileName(archiveFileStream.Name) : null);
this.currentArchiveTotalBytes = archiveStream.Length;
this.currentArchiveNumber--;
this.OnProgress(ArchiveProgressType.StartArchive);
this.currentArchiveNumber++;
}
archiveStream.Seek(localHeaderOffset, SeekOrigin.Begin);
ZipFileHeader localHeader = new ZipFileHeader();
if (!localHeader.Read(archiveStream, false) ||
!ZipEngine.AreFilePathsEqual(localHeader.fileName, header.fileName))
{
string msg = "Could not read file: " + header.fileName;
throw new ZipException(msg);
}
fileInfo = header.ToZipFileInfo();
fileStream = streamContext.OpenFileWriteStream(
fileInfo.FullName,
fileInfo.Length,
fileInfo.LastWriteTime);
if (fileStream != null)
{
this.currentFileName = header.fileName;
this.currentFileBytesProcessed = 0;
this.currentFileTotalBytes = fileInfo.Length;
this.currentArchiveNumber--;
this.OnProgress(ArchiveProgressType.StartFile);
this.currentArchiveNumber++;
this.UnpackFileBytes(
streamContext,
fileInfo.FullName,
fileInfo.CompressedLength,
fileInfo.Length,
header.crc32,
fileStream,
compressionStreamCreator,
ref archiveStream);
}
}
finally
{
if (fileStream != null)
{
streamContext.CloseFileWriteStream(
fileInfo.FullName,
fileStream,
fileInfo.Attributes,
fileInfo.LastWriteTime);
this.currentArchiveNumber--;
this.OnProgress(ArchiveProgressType.FinishFile);
this.currentArchiveNumber++;
}
}
}
/// <summary>
/// Compares two internal file paths while ignoring case and slash differences.
/// </summary>
/// <param name="path1">The first path to compare.</param>
/// <param name="path2">The second path to compare.</param>
/// <returns>True if the paths are equivalent.</returns>
private static bool AreFilePathsEqual(string path1, string path2)
{
path1 = path1.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
path2 = path2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
return String.Compare(path1, path2, StringComparison.OrdinalIgnoreCase) == 0;
}
private Stream OpenArchive(IUnpackStreamContext streamContext, int archiveNumber)
{
Stream archiveStream = streamContext.OpenArchiveReadStream(
archiveNumber, String.Empty, this);
if (archiveStream == null && archiveNumber != 0)
{
archiveStream = streamContext.OpenArchiveReadStream(
0, String.Empty, this);
}
if (archiveStream == null)
{
throw new FileNotFoundException("Archive stream not provided.");
}
return archiveStream;
}
/// <summary>
/// Decompresses bytes for one file from an archive or archive chain,
/// checking the crc at the end.
/// </summary>
private void UnpackFileBytes(
IUnpackStreamContext streamContext,
string fileName,
long compressedSize,
long uncompressedSize,
uint crc,
Stream fileStream,
Converter<Stream, Stream> compressionStreamCreator,
ref Stream archiveStream)
{
CrcStream crcStream = new CrcStream(fileStream);
ConcatStream concatStream = new ConcatStream(
delegate(ConcatStream s)
{
this.currentArchiveBytesProcessed = s.Source.Position;
streamContext.CloseArchiveReadStream(
this.currentArchiveNumber,
String.Empty,
s.Source);
this.currentArchiveNumber--;
this.OnProgress(ArchiveProgressType.FinishArchive);
this.currentArchiveNumber += 2;
this.currentArchiveName = null;
this.currentArchiveBytesProcessed = 0;
this.currentArchiveTotalBytes = 0;
s.Source = this.OpenArchive(streamContext, this.currentArchiveNumber);
FileStream archiveFileStream = s.Source as FileStream;
this.currentArchiveName = (archiveFileStream != null ?
Path.GetFileName(archiveFileStream.Name) : null);
this.currentArchiveTotalBytes = s.Source.Length;
this.currentArchiveNumber--;
this.OnProgress(ArchiveProgressType.StartArchive);
this.currentArchiveNumber++;
});
concatStream.Source = archiveStream;
concatStream.SetLength(compressedSize);
Stream decompressionStream = compressionStreamCreator(concatStream);
try
{
byte[] buf = new byte[4096];
long bytesRemaining = uncompressedSize;
int counter = 0;
while (bytesRemaining > 0)
{
int count = (int) Math.Min(buf.Length, bytesRemaining);
count = decompressionStream.Read(buf, 0, count);
crcStream.Write(buf, 0, count);
bytesRemaining -= count;
this.fileBytesProcessed += count;
this.currentFileBytesProcessed += count;
this.currentArchiveBytesProcessed = concatStream.Source.Position;
if (++counter % 16 == 0) // Report every 64K
{
this.currentArchiveNumber--;
this.OnProgress(ArchiveProgressType.PartialFile);
this.currentArchiveNumber++;
}
}
}
finally
{
archiveStream = concatStream.Source;
}
crcStream.Flush();
if (crcStream.Crc != crc)
{
throw new ZipException("CRC check failed for file: " + fileName);
}
}
}
}
|