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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
// 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;
using System.Globalization;
public partial class ZipEngine
{
/// <summary>
/// Creates a zip archive or chain of zip archives.
/// </summary>
/// <param name="streamContext">A context interface to handle opening
/// and closing of archive and file streams.</param>
/// <param name="files">An array of file lists. Each list is
/// compressed into one stream in the archive.</param>
/// <param name="maxArchiveSize">The maximum number of bytes for one archive
/// before the contents are chained to the next archive, or zero for unlimited
/// archive size.</param>
/// <exception cref="ArchiveException">The archive could not be
/// created.</exception>
/// <remarks>
/// The stream context implementation may provide a mapping from the file
/// paths within the archive to the external file paths.
/// </remarks>
public override void Pack(
IPackStreamContext streamContext,
IEnumerable<string> files,
long maxArchiveSize)
{
if (streamContext == null)
{
throw new ArgumentNullException("streamContext");
}
if (files == null)
{
throw new ArgumentNullException("files");
}
lock (this)
{
Stream archiveStream = null;
try
{
this.ResetProgressData();
this.totalArchives = 1;
object forceZip64Value = streamContext.GetOption("forceZip64", null);
bool forceZip64 = Convert.ToBoolean(
forceZip64Value, CultureInfo.InvariantCulture);
// Count the total number of files and bytes to be compressed.
foreach (string file in files)
{
FileAttributes attributes;
DateTime lastWriteTime;
Stream fileStream = streamContext.OpenFileReadStream(
file,
out attributes,
out lastWriteTime);
if (fileStream != null)
{
this.totalFileBytes += fileStream.Length;
this.totalFiles++;
streamContext.CloseFileReadStream(file, fileStream);
}
}
List<ZipFileHeader> fileHeaders = new List<ZipFileHeader>();
this.currentFileNumber = -1;
if (this.currentArchiveName == null)
{
this.mainArchiveName = streamContext.GetArchiveName(0);
this.currentArchiveName = this.mainArchiveName;
if (String.IsNullOrEmpty(this.currentArchiveName))
{
throw new FileNotFoundException("No name provided for archive.");
}
}
this.OnProgress(ArchiveProgressType.StartArchive);
// Compress files one by one, saving header info for each.
foreach (string file in files)
{
ZipFileHeader fileHeader = this.PackOneFile(
streamContext,
file,
maxArchiveSize,
forceZip64,
ref archiveStream);
if (fileHeader != null)
{
fileHeaders.Add(fileHeader);
}
this.currentArchiveTotalBytes = (archiveStream != null ?
archiveStream.Position : 0);
this.currentArchiveBytesProcessed = this.currentArchiveTotalBytes;
}
bool zip64 = forceZip64 || this.totalFiles > UInt16.MaxValue;
// Write the central directory composed of all the file headers.
uint centralDirStartArchiveNumber = 0;
long centralDirStartPosition = 0;
long centralDirSize = 0;
for (int i = 0; i < fileHeaders.Count; i++)
{
ZipFileHeader fileHeader = fileHeaders[i];
int headerSize = fileHeader.GetSize(true);
centralDirSize += headerSize;
this.CheckArchiveWriteStream(
streamContext,
maxArchiveSize,
headerSize,
ref archiveStream);
if (i == 0)
{
centralDirStartArchiveNumber = (uint) this.currentArchiveNumber;
centralDirStartPosition = archiveStream.Position;
}
fileHeader.Write(archiveStream, true);
if (fileHeader.zip64)
{
zip64 = true;
}
}
this.currentArchiveTotalBytes =
(archiveStream != null ? archiveStream.Position : 0);
this.currentArchiveBytesProcessed = this.currentArchiveTotalBytes;
ZipEndOfCentralDirectory eocd = new ZipEndOfCentralDirectory();
eocd.dirStartDiskNumber = centralDirStartArchiveNumber;
eocd.entriesOnDisk = fileHeaders.Count;
eocd.totalEntries = fileHeaders.Count;
eocd.dirSize = centralDirSize;
eocd.dirOffset = centralDirStartPosition;
eocd.comment = this.comment;
Zip64EndOfCentralDirectoryLocator eocdl =
new Zip64EndOfCentralDirectoryLocator();
int maxFooterSize = eocd.GetSize(false);
if (archiveStream != null && (zip64 || archiveStream.Position >
((long) UInt32.MaxValue) - eocd.GetSize(false)))
{
maxFooterSize += eocd.GetSize(true) + (int)
Zip64EndOfCentralDirectoryLocator.EOCDL64_SIZE;
zip64 = true;
}
this.CheckArchiveWriteStream(
streamContext,
maxArchiveSize,
maxFooterSize,
ref archiveStream);
eocd.diskNumber = (uint) this.currentArchiveNumber;
if (zip64)
{
eocd.versionMadeBy = 45;
eocd.versionNeeded = 45;
eocd.zip64 = true;
eocdl.dirOffset = archiveStream.Position;
eocdl.dirStartDiskNumber = (uint) this.currentArchiveNumber;
eocdl.totalDisks = (uint) this.currentArchiveNumber + 1;
eocd.Write(archiveStream);
eocdl.Write(archiveStream);
eocd.dirOffset = UInt32.MaxValue;
eocd.dirStartDiskNumber = UInt16.MaxValue;
}
eocd.zip64 = false;
eocd.Write(archiveStream);
this.currentArchiveTotalBytes = archiveStream.Position;
this.currentArchiveBytesProcessed = this.currentArchiveTotalBytes;
}
finally
{
if (archiveStream != null)
{
streamContext.CloseArchiveWriteStream(
this.currentArchiveNumber, this.mainArchiveName, archiveStream);
this.OnProgress(ArchiveProgressType.FinishArchive);
}
}
}
}
/// <summary>
/// Moves to the next archive in the sequence if necessary.
/// </summary>
private void CheckArchiveWriteStream(
IPackStreamContext streamContext,
long maxArchiveSize,
long requiredSize,
ref Stream archiveStream)
{
if (archiveStream != null &&
archiveStream.Length > 0 && maxArchiveSize > 0)
{
long sizeRemaining = maxArchiveSize - archiveStream.Length;
if (sizeRemaining < requiredSize)
{
string nextArchiveName = streamContext.GetArchiveName(
this.currentArchiveNumber + 1);
if (String.IsNullOrEmpty(nextArchiveName))
{
throw new FileNotFoundException("No name provided for archive #" +
this.currentArchiveNumber + 1);
}
this.currentArchiveTotalBytes = archiveStream.Position;
this.currentArchiveBytesProcessed = this.currentArchiveTotalBytes;
streamContext.CloseArchiveWriteStream(
this.currentArchiveNumber,
nextArchiveName,
archiveStream);
archiveStream = null;
this.OnProgress(ArchiveProgressType.FinishArchive);
this.currentArchiveNumber++;
this.totalArchives++;
this.currentArchiveBytesProcessed = 0;
this.currentArchiveTotalBytes = 0;
}
}
if (archiveStream == null)
{
if (this.currentArchiveNumber > 0)
{
this.OnProgress(ArchiveProgressType.StartArchive);
}
archiveStream = streamContext.OpenArchiveWriteStream(
this.currentArchiveNumber, this.mainArchiveName, true, this);
if (archiveStream == null)
{
throw new FileNotFoundException("Stream not provided for archive #" +
this.currentArchiveNumber);
}
}
}
/// <summary>
/// Adds one file to a zip archive in the process of being created.
/// </summary>
private ZipFileHeader PackOneFile(
IPackStreamContext streamContext,
string file,
long maxArchiveSize,
bool forceZip64,
ref Stream archiveStream)
{
Stream fileStream = null;
int headerArchiveNumber = 0;
try
{
// TODO: call GetOption to get compression method for the specific file
ZipCompressionMethod compressionMethod = ZipCompressionMethod.Deflate;
if (this.CompressionLevel == WixToolset.Dtf.Compression.CompressionLevel.None)
{
compressionMethod = ZipCompressionMethod.Store;
}
Converter<Stream, Stream> compressionStreamCreator;
if (!ZipEngine.compressionStreamCreators.TryGetValue(
compressionMethod, out compressionStreamCreator))
{
return null;
}
FileAttributes attributes;
DateTime lastWriteTime;
fileStream = streamContext.OpenFileReadStream(
file, out attributes, out lastWriteTime);
if (fileStream == null)
{
return null;
}
this.currentFileName = file;
this.currentFileNumber++;
this.currentFileTotalBytes = fileStream.Length;
this.currentFileBytesProcessed = 0;
this.OnProgress(ArchiveProgressType.StartFile);
ZipFileInfo fileInfo = new ZipFileInfo(
file,
this.currentArchiveNumber,
attributes,
lastWriteTime,
fileStream.Length,
0,
compressionMethod);
bool zip64 = forceZip64 || fileStream.Length >= (long) UInt32.MaxValue;
ZipFileHeader fileHeader = new ZipFileHeader(fileInfo, zip64);
this.CheckArchiveWriteStream(
streamContext,
maxArchiveSize,
fileHeader.GetSize(false),
ref archiveStream);
long headerPosition = archiveStream.Position;
fileHeader.Write(archiveStream, false);
headerArchiveNumber = this.currentArchiveNumber;
uint crc;
long bytesWritten = this.PackFileBytes(
streamContext,
fileStream,
maxArchiveSize,
compressionStreamCreator,
ref archiveStream,
out crc);
fileHeader.Update(
bytesWritten,
fileStream.Length,
crc,
headerPosition,
headerArchiveNumber);
streamContext.CloseFileReadStream(file, fileStream);
fileStream = null;
// Go back and rewrite the updated file header.
if (this.currentArchiveNumber == headerArchiveNumber)
{
long fileEndPosition = archiveStream.Position;
archiveStream.Seek(headerPosition, SeekOrigin.Begin);
fileHeader.Write(archiveStream, false);
archiveStream.Seek(fileEndPosition, SeekOrigin.Begin);
}
else
{
// The file spanned archives, so temporarily reopen
// the archive where it started.
string headerArchiveName = streamContext.GetArchiveName(
headerArchiveNumber + 1);
Stream headerStream = null;
try
{
headerStream = streamContext.OpenArchiveWriteStream(
headerArchiveNumber, headerArchiveName, false, this);
headerStream.Seek(headerPosition, SeekOrigin.Begin);
fileHeader.Write(headerStream, false);
}
finally
{
if (headerStream != null)
{
streamContext.CloseArchiveWriteStream(
headerArchiveNumber, headerArchiveName, headerStream);
}
}
}
this.OnProgress(ArchiveProgressType.FinishFile);
return fileHeader;
}
finally
{
if (fileStream != null)
{
streamContext.CloseFileReadStream(
this.currentFileName, fileStream);
}
}
}
/// <summary>
/// Writes compressed bytes of one file to the archive,
/// keeping track of the CRC and number of bytes written.
/// </summary>
private long PackFileBytes(
IPackStreamContext streamContext,
Stream fileStream,
long maxArchiveSize,
Converter<Stream, Stream> compressionStreamCreator,
ref Stream archiveStream,
out uint crc)
{
long writeStartPosition = archiveStream.Position;
long bytesWritten = 0;
CrcStream fileCrcStream = new CrcStream(fileStream);
ConcatStream concatStream = new ConcatStream(
delegate(ConcatStream s)
{
Stream sourceStream = s.Source;
bytesWritten += sourceStream.Position - writeStartPosition;
this.CheckArchiveWriteStream(
streamContext,
maxArchiveSize,
1,
ref sourceStream);
writeStartPosition = sourceStream.Position;
s.Source = sourceStream;
});
concatStream.Source = archiveStream;
if (maxArchiveSize > 0)
{
concatStream.SetLength(maxArchiveSize);
}
Stream compressionStream = compressionStreamCreator(concatStream);
try
{
byte[] buf = new byte[4096];
long bytesRemaining = fileStream.Length;
int counter = 0;
while (bytesRemaining > 0)
{
int count = (int) Math.Min(
bytesRemaining, (long) buf.Length);
count = fileCrcStream.Read(buf, 0, count);
if (count <= 0)
{
throw new ZipException(
"Failed to read file: " + this.currentFileName);
}
compressionStream.Write(buf, 0, count);
bytesRemaining -= count;
this.fileBytesProcessed += count;
this.currentFileBytesProcessed += count;
this.currentArchiveTotalBytes = concatStream.Source.Position;
this.currentArchiveBytesProcessed = this.currentArchiveTotalBytes;
if (++counter % 16 == 0) // Report every 64K
{
this.OnProgress(ArchiveProgressType.PartialFile);
}
}
if (compressionStream is DeflateStream)
{
compressionStream.Close();
}
else
{
compressionStream.Flush();
}
}
finally
{
archiveStream = concatStream.Source;
}
bytesWritten += archiveStream.Position - writeStartPosition;
crc = fileCrcStream.Crc;
return bytesWritten;
}
}
}
|