| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Collections; |
| | | 4 | | using ICSharpCode.SharpZipLib.Checksum; |
| | | 5 | | using ICSharpCode.SharpZipLib.Zip.Compression; |
| | | 6 | | using ICSharpCode.SharpZipLib.Zip.Compression.Streams; |
| | | 7 | | |
| | | 8 | | namespace ICSharpCode.SharpZipLib.Zip |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// This is a DeflaterOutputStream that writes the files into a zip |
| | | 12 | | /// archive one after another. It has a special method to start a new |
| | | 13 | | /// zip entry. The zip entries contains information about the file name |
| | | 14 | | /// size, compressed size, CRC, etc. |
| | | 15 | | /// |
| | | 16 | | /// It includes support for Stored and Deflated entries. |
| | | 17 | | /// This class is not thread safe. |
| | | 18 | | /// <br/> |
| | | 19 | | /// <br/>Author of the original java version : Jochen Hoenicke |
| | | 20 | | /// </summary> |
| | | 21 | | /// <example> This sample shows how to create a zip file |
| | | 22 | | /// <code> |
| | | 23 | | /// using System; |
| | | 24 | | /// using System.IO; |
| | | 25 | | /// |
| | | 26 | | /// using ICSharpCode.SharpZipLib.Core; |
| | | 27 | | /// using ICSharpCode.SharpZipLib.Zip; |
| | | 28 | | /// |
| | | 29 | | /// class MainClass |
| | | 30 | | /// { |
| | | 31 | | /// public static void Main(string[] args) |
| | | 32 | | /// { |
| | | 33 | | /// string[] filenames = Directory.GetFiles(args[0]); |
| | | 34 | | /// byte[] buffer = new byte[4096]; |
| | | 35 | | /// |
| | | 36 | | /// using ( ZipOutputStream s = new ZipOutputStream(File.Create(args[1])) ) { |
| | | 37 | | /// |
| | | 38 | | /// s.SetLevel(9); // 0 - store only to 9 - means best compression |
| | | 39 | | /// |
| | | 40 | | /// foreach (string file in filenames) { |
| | | 41 | | /// ZipEntry entry = new ZipEntry(file); |
| | | 42 | | /// s.PutNextEntry(entry); |
| | | 43 | | /// |
| | | 44 | | /// using (FileStream fs = File.OpenRead(file)) { |
| | | 45 | | /// StreamUtils.Copy(fs, s, buffer); |
| | | 46 | | /// } |
| | | 47 | | /// } |
| | | 48 | | /// } |
| | | 49 | | /// } |
| | | 50 | | /// } |
| | | 51 | | /// </code> |
| | | 52 | | /// </example> |
| | | 53 | | public class ZipOutputStream : DeflaterOutputStream |
| | | 54 | | { |
| | | 55 | | #region Constructors |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates a new Zip output stream, writing a zip archive. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="baseOutputStream"> |
| | | 60 | | /// The output stream to which the archive contents are written. |
| | | 61 | | /// </param> |
| | | 62 | | public ZipOutputStream(Stream baseOutputStream) |
| | 87 | 63 | | : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true)) |
| | | 64 | | { |
| | 87 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Creates a new Zip output stream, writing a zip archive. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="baseOutputStream">The output stream to which the archive contents are written.</param> |
| | | 71 | | /// <param name="bufferSize">Size of the buffer to use.</param> |
| | | 72 | | public ZipOutputStream(Stream baseOutputStream, int bufferSize) |
| | 0 | 73 | | : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), bufferSize) |
| | | 74 | | { |
| | 0 | 75 | | } |
| | | 76 | | #endregion |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets a flag value of true if the central header has been added for this archive; false if it has not been added. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <remarks>No further entries can be added once this has been done.</remarks> |
| | | 82 | | public bool IsFinished { |
| | | 83 | | get { |
| | 1 | 84 | | return entries == null; |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Set the zip file comment. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="comment"> |
| | | 92 | | /// The comment text for the entire archive. |
| | | 93 | | /// </param> |
| | | 94 | | /// <exception name ="ArgumentOutOfRangeException"> |
| | | 95 | | /// The converted comment is longer than 0xffff bytes. |
| | | 96 | | /// </exception> |
| | | 97 | | public void SetComment(string comment) |
| | | 98 | | { |
| | | 99 | | // TODO: Its not yet clear how to handle unicode comments here. |
| | 8 | 100 | | byte[] commentBytes = ZipConstants.ConvertToArray(comment); |
| | 8 | 101 | | if (commentBytes.Length > 0xffff) { |
| | 1 | 102 | | throw new ArgumentOutOfRangeException(nameof(comment)); |
| | | 103 | | } |
| | 7 | 104 | | zipComment = commentBytes; |
| | 7 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Sets the compression level. The new level will be activated |
| | | 109 | | /// immediately. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="level">The new compression level (1 to 9).</param> |
| | | 112 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 113 | | /// Level specified is not supported. |
| | | 114 | | /// </exception> |
| | | 115 | | /// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Deflater"/> |
| | | 116 | | public void SetLevel(int level) |
| | | 117 | | { |
| | 55 | 118 | | deflater_.SetLevel(level); |
| | 55 | 119 | | defaultCompressionLevel = level; |
| | 55 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Get the current deflater compression level |
| | | 124 | | /// </summary> |
| | | 125 | | /// <returns>The current compression level</returns> |
| | | 126 | | public int GetLevel() |
| | | 127 | | { |
| | 3 | 128 | | return deflater_.GetLevel(); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Get / set a value indicating how Zip64 Extension usage is determined when adding entries. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <remarks>Older archivers may not understand Zip64 extensions. |
| | | 135 | | /// If backwards compatability is an issue be careful when adding <see cref="ZipEntry.Size">entries</see> to an arch |
| | | 136 | | /// Setting this property to off is workable but less desirable as in those circumstances adding a file |
| | | 137 | | /// larger then 4GB will fail.</remarks> |
| | | 138 | | public UseZip64 UseZip64 { |
| | 0 | 139 | | get { return useZip64_; } |
| | 14 | 140 | | set { useZip64_ = value; } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Write an unsigned short in little endian byte order. |
| | | 145 | | /// </summary> |
| | | 146 | | private void WriteLeShort(int value) |
| | | 147 | | { |
| | | 148 | | unchecked { |
| | 6369 | 149 | | baseOutputStream_.WriteByte((byte)(value & 0xff)); |
| | 6369 | 150 | | baseOutputStream_.WriteByte((byte)((value >> 8) & 0xff)); |
| | | 151 | | } |
| | 6369 | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Write an int in little endian byte order. |
| | | 156 | | /// </summary> |
| | | 157 | | private void WriteLeInt(int value) |
| | | 158 | | { |
| | | 159 | | unchecked { |
| | 2286 | 160 | | WriteLeShort(value); |
| | 2286 | 161 | | WriteLeShort(value >> 16); |
| | | 162 | | } |
| | 2286 | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Write an int in little endian byte order. |
| | | 167 | | /// </summary> |
| | | 168 | | private void WriteLeLong(long value) |
| | | 169 | | { |
| | | 170 | | unchecked { |
| | 268 | 171 | | WriteLeInt((int)value); |
| | 268 | 172 | | WriteLeInt((int)(value >> 32)); |
| | | 173 | | } |
| | 268 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Starts a new Zip entry. It automatically closes the previous |
| | | 178 | | /// entry if present. |
| | | 179 | | /// All entry elements bar name are optional, but must be correct if present. |
| | | 180 | | /// If the compression method is stored and the output is not patchable |
| | | 181 | | /// the compression for that entry is automatically changed to deflate level 0 |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="entry"> |
| | | 184 | | /// the entry. |
| | | 185 | | /// </param> |
| | | 186 | | /// <exception cref="System.ArgumentNullException"> |
| | | 187 | | /// if entry passed is null. |
| | | 188 | | /// </exception> |
| | | 189 | | /// <exception cref="System.IO.IOException"> |
| | | 190 | | /// if an I/O error occured. |
| | | 191 | | /// </exception> |
| | | 192 | | /// <exception cref="System.InvalidOperationException"> |
| | | 193 | | /// if stream was finished |
| | | 194 | | /// </exception> |
| | | 195 | | /// <exception cref="ZipException"> |
| | | 196 | | /// Too many entries in the Zip file<br/> |
| | | 197 | | /// Entry name is too long<br/> |
| | | 198 | | /// Finish has already been called<br/> |
| | | 199 | | /// </exception> |
| | | 200 | | public void PutNextEntry(ZipEntry entry) |
| | | 201 | | { |
| | 130 | 202 | | if (entry == null) { |
| | 0 | 203 | | throw new ArgumentNullException(nameof(entry)); |
| | | 204 | | } |
| | | 205 | | |
| | 130 | 206 | | if (entries == null) { |
| | 1 | 207 | | throw new InvalidOperationException("ZipOutputStream was finished"); |
| | | 208 | | } |
| | | 209 | | |
| | 129 | 210 | | if (curEntry != null) { |
| | 48 | 211 | | CloseEntry(); |
| | | 212 | | } |
| | | 213 | | |
| | 129 | 214 | | if (entries.Count == int.MaxValue) { |
| | 0 | 215 | | throw new ZipException("Too many entries for Zip file"); |
| | | 216 | | } |
| | | 217 | | |
| | 129 | 218 | | CompressionMethod method = entry.CompressionMethod; |
| | 129 | 219 | | int compressionLevel = defaultCompressionLevel; |
| | | 220 | | |
| | | 221 | | // Clear flags that the library manages internally |
| | 129 | 222 | | entry.Flags &= (int)GeneralBitFlags.UnicodeText; |
| | 129 | 223 | | patchEntryHeader = false; |
| | | 224 | | |
| | | 225 | | bool headerInfoAvailable; |
| | | 226 | | |
| | | 227 | | // No need to compress - definitely no data. |
| | 129 | 228 | | if (entry.Size == 0) { |
| | 1 | 229 | | entry.CompressedSize = entry.Size; |
| | 1 | 230 | | entry.Crc = 0; |
| | 1 | 231 | | method = CompressionMethod.Stored; |
| | 1 | 232 | | headerInfoAvailable = true; |
| | 1 | 233 | | } else { |
| | 128 | 234 | | headerInfoAvailable = (entry.Size >= 0) && entry.HasCrc && entry.CompressedSize >= 0; |
| | | 235 | | |
| | | 236 | | // Switch to deflation if storing isnt possible. |
| | 128 | 237 | | if (method == CompressionMethod.Stored) { |
| | 13 | 238 | | if (!headerInfoAvailable) { |
| | 13 | 239 | | if (!CanPatchEntries) { |
| | | 240 | | // Can't patch entries so storing is not possible. |
| | 5 | 241 | | method = CompressionMethod.Deflated; |
| | 5 | 242 | | compressionLevel = 0; |
| | | 243 | | } |
| | 5 | 244 | | } else // entry.size must be > 0 |
| | | 245 | | { |
| | 0 | 246 | | entry.CompressedSize = entry.Size; |
| | 0 | 247 | | headerInfoAvailable = entry.HasCrc; |
| | | 248 | | } |
| | | 249 | | } |
| | | 250 | | } |
| | | 251 | | |
| | 129 | 252 | | if (headerInfoAvailable == false) { |
| | 128 | 253 | | if (CanPatchEntries == false) { |
| | | 254 | | // Only way to record size and compressed size is to append a data descriptor |
| | | 255 | | // after compressed data. |
| | | 256 | | |
| | | 257 | | // Stored entries of this form have already been converted to deflating. |
| | 32 | 258 | | entry.Flags |= 8; |
| | 32 | 259 | | } else { |
| | 96 | 260 | | patchEntryHeader = true; |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | 129 | 264 | | if (Password != null) { |
| | 33 | 265 | | entry.IsCrypted = true; |
| | 33 | 266 | | if (entry.Crc < 0) { |
| | | 267 | | // Need to append a data descriptor as the crc isnt available for use |
| | | 268 | | // with encryption, the date is used instead. Setting the flag |
| | | 269 | | // indicates this to the decompressor. |
| | 29 | 270 | | entry.Flags |= 8; |
| | | 271 | | } |
| | | 272 | | } |
| | | 273 | | |
| | 129 | 274 | | entry.Offset = offset; |
| | 129 | 275 | | entry.CompressionMethod = (CompressionMethod)method; |
| | | 276 | | |
| | 129 | 277 | | curMethod = method; |
| | 129 | 278 | | sizePatchPos = -1; |
| | | 279 | | |
| | 129 | 280 | | if ((useZip64_ == UseZip64.On) || ((entry.Size < 0) && (useZip64_ == UseZip64.Dynamic))) { |
| | 121 | 281 | | entry.ForceZip64(); |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | // Write the local file header |
| | 129 | 285 | | WriteLeInt(ZipConstants.LocalHeaderSignature); |
| | | 286 | | |
| | 129 | 287 | | WriteLeShort(entry.Version); |
| | 129 | 288 | | WriteLeShort(entry.Flags); |
| | 129 | 289 | | WriteLeShort((byte)entry.CompressionMethodForHeader); |
| | 129 | 290 | | WriteLeInt((int)entry.DosTime); |
| | | 291 | | |
| | | 292 | | // TODO: Refactor header writing. Its done in several places. |
| | 129 | 293 | | if (headerInfoAvailable) { |
| | 1 | 294 | | WriteLeInt((int)entry.Crc); |
| | 1 | 295 | | if (entry.LocalHeaderRequiresZip64) { |
| | 1 | 296 | | WriteLeInt(-1); |
| | 1 | 297 | | WriteLeInt(-1); |
| | 1 | 298 | | } else { |
| | 0 | 299 | | WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.Compressed |
| | 0 | 300 | | WriteLeInt((int)entry.Size); |
| | | 301 | | } |
| | 0 | 302 | | } else { |
| | 128 | 303 | | if (patchEntryHeader) { |
| | 96 | 304 | | crcPatchPos = baseOutputStream_.Position; |
| | | 305 | | } |
| | 128 | 306 | | WriteLeInt(0); // Crc |
| | | 307 | | |
| | 128 | 308 | | if (patchEntryHeader) { |
| | 96 | 309 | | sizePatchPos = baseOutputStream_.Position; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | // For local header both sizes appear in Zip64 Extended Information |
| | 128 | 313 | | if (entry.LocalHeaderRequiresZip64 || patchEntryHeader) { |
| | 125 | 314 | | WriteLeInt(-1); |
| | 125 | 315 | | WriteLeInt(-1); |
| | 125 | 316 | | } else { |
| | 3 | 317 | | WriteLeInt(0); // Compressed size |
| | 3 | 318 | | WriteLeInt(0); // Uncompressed size |
| | | 319 | | } |
| | | 320 | | } |
| | | 321 | | |
| | 129 | 322 | | byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); |
| | | 323 | | |
| | 129 | 324 | | if (name.Length > 0xFFFF) { |
| | 0 | 325 | | throw new ZipException("Entry name too long."); |
| | | 326 | | } |
| | | 327 | | |
| | 129 | 328 | | var ed = new ZipExtraData(entry.ExtraData); |
| | | 329 | | |
| | 129 | 330 | | if (entry.LocalHeaderRequiresZip64) { |
| | 122 | 331 | | ed.StartNewEntry(); |
| | 122 | 332 | | if (headerInfoAvailable) { |
| | 1 | 333 | | ed.AddLeLong(entry.Size); |
| | 1 | 334 | | ed.AddLeLong(entry.CompressedSize); |
| | 1 | 335 | | } else { |
| | 121 | 336 | | ed.AddLeLong(-1); |
| | 121 | 337 | | ed.AddLeLong(-1); |
| | | 338 | | } |
| | 122 | 339 | | ed.AddNewEntry(1); |
| | | 340 | | |
| | 122 | 341 | | if (!ed.Find(1)) { |
| | 0 | 342 | | throw new ZipException("Internal error cant find extra data"); |
| | | 343 | | } |
| | | 344 | | |
| | 122 | 345 | | if (patchEntryHeader) { |
| | 92 | 346 | | sizePatchPos = ed.CurrentReadIndex; |
| | | 347 | | } |
| | 92 | 348 | | } else { |
| | 7 | 349 | | ed.Delete(1); |
| | | 350 | | } |
| | | 351 | | |
| | 129 | 352 | | if (entry.AESKeySize > 0) { |
| | 0 | 353 | | AddExtraDataAES(entry, ed); |
| | | 354 | | } |
| | 129 | 355 | | byte[] extra = ed.GetEntryData(); |
| | | 356 | | |
| | 129 | 357 | | WriteLeShort(name.Length); |
| | 129 | 358 | | WriteLeShort(extra.Length); |
| | | 359 | | |
| | 129 | 360 | | if (name.Length > 0) { |
| | 129 | 361 | | baseOutputStream_.Write(name, 0, name.Length); |
| | | 362 | | } |
| | | 363 | | |
| | 128 | 364 | | if (entry.LocalHeaderRequiresZip64 && patchEntryHeader) { |
| | 91 | 365 | | sizePatchPos += baseOutputStream_.Position; |
| | | 366 | | } |
| | | 367 | | |
| | 128 | 368 | | if (extra.Length > 0) { |
| | 121 | 369 | | baseOutputStream_.Write(extra, 0, extra.Length); |
| | | 370 | | } |
| | | 371 | | |
| | 128 | 372 | | offset += ZipConstants.LocalHeaderBaseSize + name.Length + extra.Length; |
| | | 373 | | // Fix offsetOfCentraldir for AES |
| | 128 | 374 | | if (entry.AESKeySize > 0) |
| | 0 | 375 | | offset += entry.AESOverheadSize; |
| | | 376 | | |
| | | 377 | | // Activate the entry. |
| | 128 | 378 | | curEntry = entry; |
| | 128 | 379 | | crc.Reset(); |
| | 128 | 380 | | if (method == CompressionMethod.Deflated) { |
| | 119 | 381 | | deflater_.Reset(); |
| | 119 | 382 | | deflater_.SetLevel(compressionLevel); |
| | | 383 | | } |
| | 128 | 384 | | size = 0; |
| | | 385 | | |
| | 128 | 386 | | if (entry.IsCrypted) { |
| | 33 | 387 | | if (entry.AESKeySize > 0) { |
| | 0 | 388 | | WriteAESHeader(entry); |
| | 0 | 389 | | } else { |
| | 33 | 390 | | if (entry.Crc < 0) { // so testing Zip will says its ok |
| | 29 | 391 | | WriteEncryptionHeader(entry.DosTime << 16); |
| | 29 | 392 | | } else { |
| | 4 | 393 | | WriteEncryptionHeader(entry.Crc); |
| | | 394 | | } |
| | | 395 | | } |
| | | 396 | | } |
| | 99 | 397 | | } |
| | | 398 | | |
| | | 399 | | /// <summary> |
| | | 400 | | /// Closes the current entry, updating header and footer information as required |
| | | 401 | | /// </summary> |
| | | 402 | | /// <exception cref="System.IO.IOException"> |
| | | 403 | | /// An I/O error occurs. |
| | | 404 | | /// </exception> |
| | | 405 | | /// <exception cref="System.InvalidOperationException"> |
| | | 406 | | /// No entry is active. |
| | | 407 | | /// </exception> |
| | | 408 | | public void CloseEntry() |
| | | 409 | | { |
| | 128 | 410 | | if (curEntry == null) { |
| | 0 | 411 | | throw new InvalidOperationException("No open entry"); |
| | | 412 | | } |
| | | 413 | | |
| | 128 | 414 | | long csize = size; |
| | | 415 | | |
| | | 416 | | // First finish the deflater, if appropriate |
| | 128 | 417 | | if (curMethod == CompressionMethod.Deflated) { |
| | 119 | 418 | | if (size >= 0) { |
| | 119 | 419 | | base.Finish(); |
| | 119 | 420 | | csize = deflater_.TotalOut; |
| | 119 | 421 | | } else { |
| | 0 | 422 | | deflater_.Reset(); |
| | | 423 | | } |
| | | 424 | | } |
| | | 425 | | |
| | | 426 | | // Write the AES Authentication Code (a hash of the compressed and encrypted data) |
| | 128 | 427 | | if (curEntry.AESKeySize > 0) { |
| | 0 | 428 | | baseOutputStream_.Write(AESAuthCode, 0, 10); |
| | | 429 | | } |
| | | 430 | | |
| | 128 | 431 | | if (curEntry.Size < 0) { |
| | 121 | 432 | | curEntry.Size = size; |
| | 128 | 433 | | } else if (curEntry.Size != size) { |
| | 0 | 434 | | throw new ZipException("size was " + size + ", but I expected " + curEntry.Size); |
| | | 435 | | } |
| | | 436 | | |
| | 128 | 437 | | if (curEntry.CompressedSize < 0) { |
| | 127 | 438 | | curEntry.CompressedSize = csize; |
| | 128 | 439 | | } else if (curEntry.CompressedSize != csize) { |
| | 0 | 440 | | throw new ZipException("compressed size was " + csize + ", but I expected " + curEntry.CompressedSize); |
| | | 441 | | } |
| | | 442 | | |
| | 128 | 443 | | if (curEntry.Crc < 0) { |
| | 114 | 444 | | curEntry.Crc = crc.Value; |
| | 128 | 445 | | } else if (curEntry.Crc != crc.Value) { |
| | 0 | 446 | | throw new ZipException("crc was " + crc.Value + ", but I expected " + curEntry.Crc); |
| | | 447 | | } |
| | | 448 | | |
| | 128 | 449 | | offset += csize; |
| | | 450 | | |
| | 128 | 451 | | if (curEntry.IsCrypted) { |
| | 33 | 452 | | if (curEntry.AESKeySize > 0) { |
| | 0 | 453 | | curEntry.CompressedSize += curEntry.AESOverheadSize; |
| | | 454 | | |
| | 0 | 455 | | } else { |
| | 33 | 456 | | curEntry.CompressedSize += ZipConstants.CryptoHeaderSize; |
| | | 457 | | } |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | // Patch the header if possible |
| | 128 | 461 | | if (patchEntryHeader) { |
| | 95 | 462 | | patchEntryHeader = false; |
| | | 463 | | |
| | 95 | 464 | | long curPos = baseOutputStream_.Position; |
| | 95 | 465 | | baseOutputStream_.Seek(crcPatchPos, SeekOrigin.Begin); |
| | 95 | 466 | | WriteLeInt((int)curEntry.Crc); |
| | | 467 | | |
| | 95 | 468 | | if (curEntry.LocalHeaderRequiresZip64) { |
| | | 469 | | |
| | 91 | 470 | | if (sizePatchPos == -1) { |
| | 0 | 471 | | throw new ZipException("Entry requires zip64 but this has been turned off"); |
| | | 472 | | } |
| | | 473 | | |
| | 91 | 474 | | baseOutputStream_.Seek(sizePatchPos, SeekOrigin.Begin); |
| | 91 | 475 | | WriteLeLong(curEntry.Size); |
| | 91 | 476 | | WriteLeLong(curEntry.CompressedSize); |
| | 91 | 477 | | } else { |
| | 4 | 478 | | WriteLeInt((int)curEntry.CompressedSize); |
| | 4 | 479 | | WriteLeInt((int)curEntry.Size); |
| | | 480 | | } |
| | 95 | 481 | | baseOutputStream_.Seek(curPos, SeekOrigin.Begin); |
| | | 482 | | } |
| | | 483 | | |
| | | 484 | | // Add data descriptor if flagged as required |
| | 128 | 485 | | if ((curEntry.Flags & 8) != 0) { |
| | 48 | 486 | | WriteLeInt(ZipConstants.DataDescriptorSignature); |
| | 48 | 487 | | WriteLeInt(unchecked((int)curEntry.Crc)); |
| | | 488 | | |
| | 48 | 489 | | if (curEntry.LocalHeaderRequiresZip64) { |
| | 43 | 490 | | WriteLeLong(curEntry.CompressedSize); |
| | 43 | 491 | | WriteLeLong(curEntry.Size); |
| | 43 | 492 | | offset += ZipConstants.Zip64DataDescriptorSize; |
| | 43 | 493 | | } else { |
| | 5 | 494 | | WriteLeInt((int)curEntry.CompressedSize); |
| | 5 | 495 | | WriteLeInt((int)curEntry.Size); |
| | 5 | 496 | | offset += ZipConstants.DataDescriptorSize; |
| | | 497 | | } |
| | | 498 | | } |
| | | 499 | | |
| | 128 | 500 | | entries.Add(curEntry); |
| | 128 | 501 | | curEntry = null; |
| | 128 | 502 | | } |
| | | 503 | | |
| | | 504 | | void WriteEncryptionHeader(long crcValue) |
| | | 505 | | { |
| | 33 | 506 | | offset += ZipConstants.CryptoHeaderSize; |
| | | 507 | | |
| | 33 | 508 | | InitializePassword(Password); |
| | | 509 | | |
| | 33 | 510 | | byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize]; |
| | 33 | 511 | | var rnd = new Random(); |
| | 33 | 512 | | rnd.NextBytes(cryptBuffer); |
| | 33 | 513 | | cryptBuffer[11] = (byte)(crcValue >> 24); |
| | | 514 | | |
| | 33 | 515 | | EncryptBlock(cryptBuffer, 0, cryptBuffer.Length); |
| | 33 | 516 | | baseOutputStream_.Write(cryptBuffer, 0, cryptBuffer.Length); |
| | 33 | 517 | | } |
| | | 518 | | |
| | | 519 | | private static void AddExtraDataAES(ZipEntry entry, ZipExtraData extraData) |
| | | 520 | | { |
| | | 521 | | |
| | | 522 | | // Vendor Version: AE-1 IS 1. AE-2 is 2. With AE-2 no CRC is required and 0 is stored. |
| | | 523 | | const int VENDOR_VERSION = 2; |
| | | 524 | | // Vendor ID is the two ASCII characters "AE". |
| | | 525 | | const int VENDOR_ID = 0x4541; //not 6965; |
| | 0 | 526 | | extraData.StartNewEntry(); |
| | | 527 | | // Pack AES extra data field see http://www.winzip.com/aes_info.htm |
| | | 528 | | //extraData.AddLeShort(7); // Data size (currently 7) |
| | 0 | 529 | | extraData.AddLeShort(VENDOR_VERSION); // 2 = AE-2 |
| | 0 | 530 | | extraData.AddLeShort(VENDOR_ID); // "AE" |
| | 0 | 531 | | extraData.AddData(entry.AESEncryptionStrength); // 1 = 128, 2 = 192, 3 = 256 |
| | 0 | 532 | | extraData.AddLeShort((int)entry.CompressionMethod); // The actual compression method used to compress the file |
| | 0 | 533 | | extraData.AddNewEntry(0x9901); |
| | 0 | 534 | | } |
| | | 535 | | |
| | | 536 | | // Replaces WriteEncryptionHeader for AES |
| | | 537 | | // |
| | | 538 | | private void WriteAESHeader(ZipEntry entry) |
| | | 539 | | { |
| | | 540 | | byte[] salt; |
| | | 541 | | byte[] pwdVerifier; |
| | 0 | 542 | | InitializeAESPassword(entry, Password, out salt, out pwdVerifier); |
| | | 543 | | // File format for AES: |
| | | 544 | | // Size (bytes) Content |
| | | 545 | | // ------------ ------- |
| | | 546 | | // Variable Salt value |
| | | 547 | | // 2 Password verification value |
| | | 548 | | // Variable Encrypted file data |
| | | 549 | | // 10 Authentication code |
| | | 550 | | // |
| | | 551 | | // Value in the "compressed size" fields of the local file header and the central directory entry |
| | | 552 | | // is the total size of all the items listed above. In other words, it is the total size of the |
| | | 553 | | // salt value, password verification value, encrypted data, and authentication code. |
| | 0 | 554 | | baseOutputStream_.Write(salt, 0, salt.Length); |
| | 0 | 555 | | baseOutputStream_.Write(pwdVerifier, 0, pwdVerifier.Length); |
| | 0 | 556 | | } |
| | | 557 | | |
| | | 558 | | /// <summary> |
| | | 559 | | /// Writes the given buffer to the current entry. |
| | | 560 | | /// </summary> |
| | | 561 | | /// <param name="buffer">The buffer containing data to write.</param> |
| | | 562 | | /// <param name="offset">The offset of the first byte to write.</param> |
| | | 563 | | /// <param name="count">The number of bytes to write.</param> |
| | | 564 | | /// <exception cref="ZipException">Archive size is invalid</exception> |
| | | 565 | | /// <exception cref="System.InvalidOperationException">No entry is active.</exception> |
| | | 566 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 567 | | { |
| | 4503 | 568 | | if (curEntry == null) { |
| | 0 | 569 | | throw new InvalidOperationException("No open entry."); |
| | | 570 | | } |
| | | 571 | | |
| | 4503 | 572 | | if (buffer == null) { |
| | 0 | 573 | | throw new ArgumentNullException(nameof(buffer)); |
| | | 574 | | } |
| | | 575 | | |
| | 4503 | 576 | | if (offset < 0) { |
| | 0 | 577 | | throw new ArgumentOutOfRangeException(nameof(offset), "Cannot be negative"); |
| | | 578 | | } |
| | | 579 | | |
| | 4503 | 580 | | if (count < 0) { |
| | 0 | 581 | | throw new ArgumentOutOfRangeException(nameof(count), "Cannot be negative"); |
| | | 582 | | } |
| | | 583 | | |
| | 4503 | 584 | | if ((buffer.Length - offset) < count) { |
| | 0 | 585 | | throw new ArgumentException("Invalid offset/count combination"); |
| | | 586 | | } |
| | | 587 | | |
| | 4503 | 588 | | crc.Update(buffer, offset, count); |
| | 4502 | 589 | | size += count; |
| | | 590 | | |
| | 4502 | 591 | | switch (curMethod) { |
| | | 592 | | case CompressionMethod.Deflated: |
| | 4303 | 593 | | base.Write(buffer, offset, count); |
| | 4303 | 594 | | break; |
| | | 595 | | |
| | | 596 | | case CompressionMethod.Stored: |
| | 199 | 597 | | if (Password != null) { |
| | 99 | 598 | | CopyAndEncrypt(buffer, offset, count); |
| | 99 | 599 | | } else { |
| | 100 | 600 | | baseOutputStream_.Write(buffer, offset, count); |
| | | 601 | | } |
| | | 602 | | break; |
| | | 603 | | } |
| | 100 | 604 | | } |
| | | 605 | | |
| | | 606 | | void CopyAndEncrypt(byte[] buffer, int offset, int count) |
| | | 607 | | { |
| | | 608 | | const int CopyBufferSize = 4096; |
| | 99 | 609 | | byte[] localBuffer = new byte[CopyBufferSize]; |
| | 198 | 610 | | while (count > 0) { |
| | 99 | 611 | | int bufferCount = (count < CopyBufferSize) ? count : CopyBufferSize; |
| | | 612 | | |
| | 99 | 613 | | Array.Copy(buffer, offset, localBuffer, 0, bufferCount); |
| | 99 | 614 | | EncryptBlock(localBuffer, 0, bufferCount); |
| | 99 | 615 | | baseOutputStream_.Write(localBuffer, 0, bufferCount); |
| | 99 | 616 | | count -= bufferCount; |
| | 99 | 617 | | offset += bufferCount; |
| | | 618 | | } |
| | 99 | 619 | | } |
| | | 620 | | |
| | | 621 | | /// <summary> |
| | | 622 | | /// Finishes the stream. This will write the central directory at the |
| | | 623 | | /// end of the zip file and flush the stream. |
| | | 624 | | /// </summary> |
| | | 625 | | /// <remarks> |
| | | 626 | | /// This is automatically called when the stream is closed. |
| | | 627 | | /// </remarks> |
| | | 628 | | /// <exception cref="System.IO.IOException"> |
| | | 629 | | /// An I/O error occurs. |
| | | 630 | | /// </exception> |
| | | 631 | | /// <exception cref="ZipException"> |
| | | 632 | | /// Comment exceeds the maximum length<br/> |
| | | 633 | | /// Entry name exceeds the maximum length |
| | | 634 | | /// </exception> |
| | | 635 | | public override void Finish() |
| | | 636 | | { |
| | 88 | 637 | | if (entries == null) { |
| | 2 | 638 | | return; |
| | | 639 | | } |
| | | 640 | | |
| | 86 | 641 | | if (curEntry != null) { |
| | 77 | 642 | | CloseEntry(); |
| | | 643 | | } |
| | | 644 | | |
| | 86 | 645 | | long numEntries = entries.Count; |
| | 86 | 646 | | long sizeEntries = 0; |
| | | 647 | | |
| | 428 | 648 | | foreach (ZipEntry entry in entries) { |
| | 128 | 649 | | WriteLeInt(ZipConstants.CentralHeaderSignature); |
| | 128 | 650 | | WriteLeShort(ZipConstants.VersionMadeBy); |
| | 128 | 651 | | WriteLeShort(entry.Version); |
| | 128 | 652 | | WriteLeShort(entry.Flags); |
| | 128 | 653 | | WriteLeShort((short)entry.CompressionMethodForHeader); |
| | 128 | 654 | | WriteLeInt((int)entry.DosTime); |
| | 128 | 655 | | WriteLeInt((int)entry.Crc); |
| | | 656 | | |
| | 128 | 657 | | if (entry.IsZip64Forced() || |
| | 128 | 658 | | (entry.CompressedSize >= uint.MaxValue)) { |
| | 121 | 659 | | WriteLeInt(-1); |
| | 121 | 660 | | } else { |
| | 7 | 661 | | WriteLeInt((int)entry.CompressedSize); |
| | | 662 | | } |
| | | 663 | | |
| | 128 | 664 | | if (entry.IsZip64Forced() || |
| | 128 | 665 | | (entry.Size >= uint.MaxValue)) { |
| | 121 | 666 | | WriteLeInt(-1); |
| | 121 | 667 | | } else { |
| | 7 | 668 | | WriteLeInt((int)entry.Size); |
| | | 669 | | } |
| | | 670 | | |
| | 128 | 671 | | byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); |
| | | 672 | | |
| | 128 | 673 | | if (name.Length > 0xffff) { |
| | 0 | 674 | | throw new ZipException("Name too long."); |
| | | 675 | | } |
| | | 676 | | |
| | 128 | 677 | | var ed = new ZipExtraData(entry.ExtraData); |
| | | 678 | | |
| | 128 | 679 | | if (entry.CentralHeaderRequiresZip64) { |
| | 121 | 680 | | ed.StartNewEntry(); |
| | 121 | 681 | | if (entry.IsZip64Forced() || |
| | 121 | 682 | | (entry.Size >= 0xffffffff)) { |
| | 121 | 683 | | ed.AddLeLong(entry.Size); |
| | | 684 | | } |
| | | 685 | | |
| | 121 | 686 | | if (entry.IsZip64Forced() || |
| | 121 | 687 | | (entry.CompressedSize >= 0xffffffff)) { |
| | 121 | 688 | | ed.AddLeLong(entry.CompressedSize); |
| | | 689 | | } |
| | | 690 | | |
| | 121 | 691 | | if (entry.Offset >= 0xffffffff) { |
| | 0 | 692 | | ed.AddLeLong(entry.Offset); |
| | | 693 | | } |
| | | 694 | | |
| | 121 | 695 | | ed.AddNewEntry(1); |
| | 121 | 696 | | } else { |
| | 7 | 697 | | ed.Delete(1); |
| | | 698 | | } |
| | | 699 | | |
| | 128 | 700 | | if (entry.AESKeySize > 0) { |
| | 0 | 701 | | AddExtraDataAES(entry, ed); |
| | | 702 | | } |
| | 128 | 703 | | byte[] extra = ed.GetEntryData(); |
| | | 704 | | |
| | 128 | 705 | | byte[] entryComment = |
| | 128 | 706 | | (entry.Comment != null) ? |
| | 128 | 707 | | ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : |
| | 128 | 708 | | new byte[0]; |
| | | 709 | | |
| | 128 | 710 | | if (entryComment.Length > 0xffff) { |
| | 0 | 711 | | throw new ZipException("Comment too long."); |
| | | 712 | | } |
| | | 713 | | |
| | 128 | 714 | | WriteLeShort(name.Length); |
| | 128 | 715 | | WriteLeShort(extra.Length); |
| | 128 | 716 | | WriteLeShort(entryComment.Length); |
| | 128 | 717 | | WriteLeShort(0); // disk number |
| | 128 | 718 | | WriteLeShort(0); // internal file attributes |
| | | 719 | | // external file attributes |
| | | 720 | | |
| | 128 | 721 | | if (entry.ExternalFileAttributes != -1) { |
| | 4 | 722 | | WriteLeInt(entry.ExternalFileAttributes); |
| | 4 | 723 | | } else { |
| | 124 | 724 | | if (entry.IsDirectory) { // mark entry as directory (from nikolam.AT.perfectinfo.com) |
| | 8 | 725 | | WriteLeInt(16); |
| | 8 | 726 | | } else { |
| | 116 | 727 | | WriteLeInt(0); |
| | | 728 | | } |
| | | 729 | | } |
| | | 730 | | |
| | 128 | 731 | | if (entry.Offset >= uint.MaxValue) { |
| | 0 | 732 | | WriteLeInt(-1); |
| | 0 | 733 | | } else { |
| | 128 | 734 | | WriteLeInt((int)entry.Offset); |
| | | 735 | | } |
| | | 736 | | |
| | 128 | 737 | | if (name.Length > 0) { |
| | 128 | 738 | | baseOutputStream_.Write(name, 0, name.Length); |
| | | 739 | | } |
| | | 740 | | |
| | 128 | 741 | | if (extra.Length > 0) { |
| | 121 | 742 | | baseOutputStream_.Write(extra, 0, extra.Length); |
| | | 743 | | } |
| | | 744 | | |
| | 128 | 745 | | if (entryComment.Length > 0) { |
| | 0 | 746 | | baseOutputStream_.Write(entryComment, 0, entryComment.Length); |
| | | 747 | | } |
| | | 748 | | |
| | 128 | 749 | | sizeEntries += ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length; |
| | | 750 | | } |
| | | 751 | | |
| | 86 | 752 | | using (ZipHelperStream zhs = new ZipHelperStream(baseOutputStream_)) { |
| | 86 | 753 | | zhs.WriteEndOfCentralDirectory(numEntries, sizeEntries, offset, zipComment); |
| | 85 | 754 | | } |
| | | 755 | | |
| | 85 | 756 | | entries = null; |
| | 85 | 757 | | } |
| | | 758 | | |
| | | 759 | | #region Instance Fields |
| | | 760 | | /// <summary> |
| | | 761 | | /// The entries for the archive. |
| | | 762 | | /// </summary> |
| | 87 | 763 | | ArrayList entries = new ArrayList(); |
| | | 764 | | |
| | | 765 | | /// <summary> |
| | | 766 | | /// Used to track the crc of data added to entries. |
| | | 767 | | /// </summary> |
| | 87 | 768 | | Crc32 crc = new Crc32(); |
| | | 769 | | |
| | | 770 | | /// <summary> |
| | | 771 | | /// The current entry being added. |
| | | 772 | | /// </summary> |
| | | 773 | | ZipEntry curEntry; |
| | | 774 | | |
| | 87 | 775 | | int defaultCompressionLevel = Deflater.DEFAULT_COMPRESSION; |
| | | 776 | | |
| | 87 | 777 | | CompressionMethod curMethod = CompressionMethod.Deflated; |
| | | 778 | | |
| | | 779 | | /// <summary> |
| | | 780 | | /// Used to track the size of data for an entry during writing. |
| | | 781 | | /// </summary> |
| | | 782 | | long size; |
| | | 783 | | |
| | | 784 | | /// <summary> |
| | | 785 | | /// Offset to be recorded for each entry in the central header. |
| | | 786 | | /// </summary> |
| | | 787 | | long offset; |
| | | 788 | | |
| | | 789 | | /// <summary> |
| | | 790 | | /// Comment for the entire archive recorded in central header. |
| | | 791 | | /// </summary> |
| | 87 | 792 | | byte[] zipComment = new byte[0]; |
| | | 793 | | |
| | | 794 | | /// <summary> |
| | | 795 | | /// Flag indicating that header patching is required for the current entry. |
| | | 796 | | /// </summary> |
| | | 797 | | bool patchEntryHeader; |
| | | 798 | | |
| | | 799 | | /// <summary> |
| | | 800 | | /// Position to patch crc |
| | | 801 | | /// </summary> |
| | 87 | 802 | | long crcPatchPos = -1; |
| | | 803 | | |
| | | 804 | | /// <summary> |
| | | 805 | | /// Position to patch size. |
| | | 806 | | /// </summary> |
| | 87 | 807 | | long sizePatchPos = -1; |
| | | 808 | | |
| | | 809 | | // Default is dynamic which is not backwards compatible and can cause problems |
| | | 810 | | // with XP's built in compression which cant read Zip64 archives. |
| | | 811 | | // However it does avoid the situation were a large file is added and cannot be completed correctly. |
| | | 812 | | // NOTE: Setting the size for entries before they are added is the best solution! |
| | 87 | 813 | | UseZip64 useZip64_ = UseZip64.Dynamic; |
| | | 814 | | #endregion |
| | | 815 | | } |
| | | 816 | | } |