ZipInputStream.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. using ICSharpCode.SharpZipLib.Checksum;
  2. using ICSharpCode.SharpZipLib.Encryption;
  3. using ICSharpCode.SharpZipLib.Zip.Compression;
  4. using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
  5. using System;
  6. using System.IO;
  7. namespace ICSharpCode.SharpZipLib.Zip
  8. {
  9. /// <summary>
  10. /// This is an InflaterInputStream that reads the files baseInputStream an zip archive
  11. /// one after another. It has a special method to get the zip entry of
  12. /// the next file. The zip entry contains information about the file name
  13. /// size, compressed size, Crc, etc.
  14. /// It includes support for Stored and Deflated entries.
  15. /// <br/>
  16. /// <br/>Author of the original java version : Jochen Hoenicke
  17. /// </summary>
  18. ///
  19. /// <example> This sample shows how to read a zip file
  20. /// <code lang="C#">
  21. /// using System;
  22. /// using System.Text;
  23. /// using System.IO;
  24. ///
  25. /// using ICSharpCode.SharpZipLib.Zip;
  26. ///
  27. /// class MainClass
  28. /// {
  29. /// public static void Main(string[] args)
  30. /// {
  31. /// using ( ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]))) {
  32. ///
  33. /// ZipEntry theEntry;
  34. /// const int size = 2048;
  35. /// byte[] data = new byte[2048];
  36. ///
  37. /// while ((theEntry = s.GetNextEntry()) != null) {
  38. /// if ( entry.IsFile ) {
  39. /// Console.Write("Show contents (y/n) ?");
  40. /// if (Console.ReadLine() == "y") {
  41. /// while (true) {
  42. /// size = s.Read(data, 0, data.Length);
  43. /// if (size > 0) {
  44. /// Console.Write(new ASCIIEncoding().GetString(data, 0, size));
  45. /// } else {
  46. /// break;
  47. /// }
  48. /// }
  49. /// }
  50. /// }
  51. /// }
  52. /// }
  53. /// }
  54. /// }
  55. /// </code>
  56. /// </example>
  57. public class ZipInputStream : InflaterInputStream
  58. {
  59. #region Instance Fields
  60. /// <summary>
  61. /// Delegate for reading bytes from a stream.
  62. /// </summary>
  63. private delegate int ReadDataHandler(byte[] b, int offset, int length);
  64. /// <summary>
  65. /// The current reader this instance.
  66. /// </summary>
  67. private ReadDataHandler internalReader;
  68. private Crc32 crc = new Crc32();
  69. private ZipEntry entry;
  70. private long size;
  71. private CompressionMethod method;
  72. private int flags;
  73. private string password;
  74. #endregion Instance Fields
  75. #region Constructors
  76. /// <summary>
  77. /// Creates a new Zip input stream, for reading a zip archive.
  78. /// </summary>
  79. /// <param name="baseInputStream">The underlying <see cref="Stream"/> providing data.</param>
  80. public ZipInputStream(Stream baseInputStream)
  81. : base(baseInputStream, new Inflater(true))
  82. {
  83. internalReader = new ReadDataHandler(ReadingNotAvailable);
  84. }
  85. /// <summary>
  86. /// Creates a new Zip input stream, for reading a zip archive.
  87. /// </summary>
  88. /// <param name="baseInputStream">The underlying <see cref="Stream"/> providing data.</param>
  89. /// <param name="bufferSize">Size of the buffer.</param>
  90. public ZipInputStream(Stream baseInputStream, int bufferSize)
  91. : base(baseInputStream, new Inflater(true), bufferSize)
  92. {
  93. internalReader = new ReadDataHandler(ReadingNotAvailable);
  94. }
  95. #endregion Constructors
  96. /// <summary>
  97. /// Optional password used for encryption when non-null
  98. /// </summary>
  99. /// <value>A password for all encrypted <see cref="ZipEntry">entries </see> in this <see cref="ZipInputStream"/></value>
  100. public string Password
  101. {
  102. get
  103. {
  104. return password;
  105. }
  106. set
  107. {
  108. password = value;
  109. }
  110. }
  111. /// <summary>
  112. /// Gets a value indicating if there is a current entry and it can be decompressed
  113. /// </summary>
  114. /// <remarks>
  115. /// The entry can only be decompressed if the library supports the zip features required to extract it.
  116. /// See the <see cref="ZipEntry.Version">ZipEntry Version</see> property for more details.
  117. /// </remarks>
  118. public bool CanDecompressEntry
  119. {
  120. get
  121. {
  122. return (entry != null) && entry.CanDecompress;
  123. }
  124. }
  125. /// <summary>
  126. /// Advances to the next entry in the archive
  127. /// </summary>
  128. /// <returns>
  129. /// The next <see cref="ZipEntry">entry</see> in the archive or null if there are no more entries.
  130. /// </returns>
  131. /// <remarks>
  132. /// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called.
  133. /// </remarks>
  134. /// <exception cref="InvalidOperationException">
  135. /// Input stream is closed
  136. /// </exception>
  137. /// <exception cref="ZipException">
  138. /// Password is not set, password is invalid, compression method is invalid,
  139. /// version required to extract is not supported
  140. /// </exception>
  141. public ZipEntry GetNextEntry()
  142. {
  143. if (crc == null)
  144. {
  145. throw new InvalidOperationException("Closed.");
  146. }
  147. if (entry != null)
  148. {
  149. CloseEntry();
  150. }
  151. int header = inputBuffer.ReadLeInt();
  152. if (header == ZipConstants.CentralHeaderSignature ||
  153. header == ZipConstants.EndOfCentralDirectorySignature ||
  154. header == ZipConstants.CentralHeaderDigitalSignature ||
  155. header == ZipConstants.ArchiveExtraDataSignature ||
  156. header == ZipConstants.Zip64CentralFileHeaderSignature)
  157. {
  158. // No more individual entries exist
  159. Dispose();
  160. return null;
  161. }
  162. // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found
  163. // Spanning signature is same as descriptor signature and is untested as yet.
  164. if ((header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature))
  165. {
  166. header = inputBuffer.ReadLeInt();
  167. }
  168. if (header != ZipConstants.LocalHeaderSignature)
  169. {
  170. throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
  171. }
  172. var versionRequiredToExtract = (short)inputBuffer.ReadLeShort();
  173. flags = inputBuffer.ReadLeShort();
  174. method = (CompressionMethod)inputBuffer.ReadLeShort();
  175. var dostime = (uint)inputBuffer.ReadLeInt();
  176. int crc2 = inputBuffer.ReadLeInt();
  177. csize = inputBuffer.ReadLeInt();
  178. size = inputBuffer.ReadLeInt();
  179. int nameLen = inputBuffer.ReadLeShort();
  180. int extraLen = inputBuffer.ReadLeShort();
  181. bool isCrypted = (flags & 1) == 1;
  182. byte[] buffer = new byte[nameLen];
  183. inputBuffer.ReadRawBuffer(buffer);
  184. string name = ZipStrings.ConvertToStringExt(flags, buffer);
  185. entry = new ZipEntry(name, versionRequiredToExtract, ZipConstants.VersionMadeBy, method)
  186. {
  187. Flags = flags,
  188. };
  189. if ((flags & 8) == 0)
  190. {
  191. entry.Crc = crc2 & 0xFFFFFFFFL;
  192. entry.Size = size & 0xFFFFFFFFL;
  193. entry.CompressedSize = csize & 0xFFFFFFFFL;
  194. entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff);
  195. }
  196. else
  197. {
  198. // This allows for GNU, WinZip and possibly other archives, the PKZIP spec
  199. // says these values are zero under these circumstances.
  200. if (crc2 != 0)
  201. {
  202. entry.Crc = crc2 & 0xFFFFFFFFL;
  203. }
  204. if (size != 0)
  205. {
  206. entry.Size = size & 0xFFFFFFFFL;
  207. }
  208. if (csize != 0)
  209. {
  210. entry.CompressedSize = csize & 0xFFFFFFFFL;
  211. }
  212. entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
  213. }
  214. entry.DosTime = dostime;
  215. // If local header requires Zip64 is true then the extended header should contain
  216. // both values.
  217. // Handle extra data if present. This can set/alter some fields of the entry.
  218. if (extraLen > 0)
  219. {
  220. byte[] extra = new byte[extraLen];
  221. inputBuffer.ReadRawBuffer(extra);
  222. entry.ExtraData = extra;
  223. }
  224. entry.ProcessExtraData(true);
  225. if (entry.CompressedSize >= 0)
  226. {
  227. csize = entry.CompressedSize;
  228. }
  229. if (entry.Size >= 0)
  230. {
  231. size = entry.Size;
  232. }
  233. if (method == CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size)))
  234. {
  235. throw new ZipException("Stored, but compressed != uncompressed");
  236. }
  237. // Determine how to handle reading of data if this is attempted.
  238. if (entry.IsCompressionMethodSupported())
  239. {
  240. internalReader = new ReadDataHandler(InitialRead);
  241. }
  242. else
  243. {
  244. internalReader = new ReadDataHandler(ReadingNotSupported);
  245. }
  246. return entry;
  247. }
  248. /// <summary>
  249. /// Read data descriptor at the end of compressed data.
  250. /// </summary>
  251. private void ReadDataDescriptor()
  252. {
  253. if (inputBuffer.ReadLeInt() != ZipConstants.DataDescriptorSignature)
  254. {
  255. throw new ZipException("Data descriptor signature not found");
  256. }
  257. entry.Crc = inputBuffer.ReadLeInt() & 0xFFFFFFFFL;
  258. if (entry.LocalHeaderRequiresZip64)
  259. {
  260. csize = inputBuffer.ReadLeLong();
  261. size = inputBuffer.ReadLeLong();
  262. }
  263. else
  264. {
  265. csize = inputBuffer.ReadLeInt();
  266. size = inputBuffer.ReadLeInt();
  267. }
  268. entry.CompressedSize = csize;
  269. entry.Size = size;
  270. }
  271. /// <summary>
  272. /// Complete cleanup as the final part of closing.
  273. /// </summary>
  274. /// <param name="testCrc">True if the crc value should be tested</param>
  275. private void CompleteCloseEntry(bool testCrc)
  276. {
  277. StopDecrypting();
  278. if ((flags & 8) != 0)
  279. {
  280. ReadDataDescriptor();
  281. }
  282. size = 0;
  283. if (testCrc &&
  284. ((crc.Value & 0xFFFFFFFFL) != entry.Crc) && (entry.Crc != -1))
  285. {
  286. throw new ZipException("CRC mismatch");
  287. }
  288. crc.Reset();
  289. if (method == CompressionMethod.Deflated)
  290. {
  291. inf.Reset();
  292. }
  293. entry = null;
  294. }
  295. /// <summary>
  296. /// Closes the current zip entry and moves to the next one.
  297. /// </summary>
  298. /// <exception cref="InvalidOperationException">
  299. /// The stream is closed
  300. /// </exception>
  301. /// <exception cref="ZipException">
  302. /// The Zip stream ends early
  303. /// </exception>
  304. public void CloseEntry()
  305. {
  306. if (crc == null)
  307. {
  308. throw new InvalidOperationException("Closed");
  309. }
  310. if (entry == null)
  311. {
  312. return;
  313. }
  314. if (method == CompressionMethod.Deflated)
  315. {
  316. if ((flags & 8) != 0)
  317. {
  318. // We don't know how much we must skip, read until end.
  319. byte[] tmp = new byte[4096];
  320. // Read will close this entry
  321. while (Read(tmp, 0, tmp.Length) > 0)
  322. {
  323. }
  324. return;
  325. }
  326. csize -= inf.TotalIn;
  327. inputBuffer.Available += inf.RemainingInput;
  328. }
  329. if ((inputBuffer.Available > csize) && (csize >= 0))
  330. {
  331. inputBuffer.Available = (int)((long)inputBuffer.Available - csize);
  332. }
  333. else
  334. {
  335. csize -= inputBuffer.Available;
  336. inputBuffer.Available = 0;
  337. while (csize != 0)
  338. {
  339. long skipped = Skip(csize);
  340. if (skipped <= 0)
  341. {
  342. throw new ZipException("Zip archive ends early.");
  343. }
  344. csize -= skipped;
  345. }
  346. }
  347. CompleteCloseEntry(false);
  348. }
  349. /// <summary>
  350. /// Returns 1 if there is an entry available
  351. /// Otherwise returns 0.
  352. /// </summary>
  353. public override int Available
  354. {
  355. get
  356. {
  357. return entry != null ? 1 : 0;
  358. }
  359. }
  360. /// <summary>
  361. /// Returns the current size that can be read from the current entry if available
  362. /// </summary>
  363. /// <exception cref="ZipException">Thrown if the entry size is not known.</exception>
  364. /// <exception cref="InvalidOperationException">Thrown if no entry is currently available.</exception>
  365. public override long Length
  366. {
  367. get
  368. {
  369. if (entry != null)
  370. {
  371. if (entry.Size >= 0)
  372. {
  373. return entry.Size;
  374. }
  375. else
  376. {
  377. throw new ZipException("Length not available for the current entry");
  378. }
  379. }
  380. else
  381. {
  382. throw new InvalidOperationException("No current entry");
  383. }
  384. }
  385. }
  386. /// <summary>
  387. /// Reads a byte from the current zip entry.
  388. /// </summary>
  389. /// <returns>
  390. /// The byte or -1 if end of stream is reached.
  391. /// </returns>
  392. public override int ReadByte()
  393. {
  394. byte[] b = new byte[1];
  395. if (Read(b, 0, 1) <= 0)
  396. {
  397. return -1;
  398. }
  399. return b[0] & 0xff;
  400. }
  401. /// <summary>
  402. /// Handle attempts to read by throwing an <see cref="InvalidOperationException"/>.
  403. /// </summary>
  404. /// <param name="destination">The destination array to store data in.</param>
  405. /// <param name="offset">The offset at which data read should be stored.</param>
  406. /// <param name="count">The maximum number of bytes to read.</param>
  407. /// <returns>Returns the number of bytes actually read.</returns>
  408. private int ReadingNotAvailable(byte[] destination, int offset, int count)
  409. {
  410. throw new InvalidOperationException("Unable to read from this stream");
  411. }
  412. /// <summary>
  413. /// Handle attempts to read from this entry by throwing an exception
  414. /// </summary>
  415. private int ReadingNotSupported(byte[] destination, int offset, int count)
  416. {
  417. throw new ZipException("The compression method for this entry is not supported");
  418. }
  419. /// <summary>
  420. /// Perform the initial read on an entry which may include
  421. /// reading encryption headers and setting up inflation.
  422. /// </summary>
  423. /// <param name="destination">The destination to fill with data read.</param>
  424. /// <param name="offset">The offset to start reading at.</param>
  425. /// <param name="count">The maximum number of bytes to read.</param>
  426. /// <returns>The actual number of bytes read.</returns>
  427. private int InitialRead(byte[] destination, int offset, int count)
  428. {
  429. if (!CanDecompressEntry)
  430. {
  431. throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version + ")");
  432. }
  433. // Handle encryption if required.
  434. if (entry.IsCrypted)
  435. {
  436. if (password == null)
  437. {
  438. throw new ZipException("No password set.");
  439. }
  440. // Generate and set crypto transform...
  441. var managed = new PkzipClassicManaged();
  442. byte[] key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));
  443. inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);
  444. byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
  445. inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);
  446. if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue)
  447. {
  448. throw new ZipException("Invalid password");
  449. }
  450. if (csize >= ZipConstants.CryptoHeaderSize)
  451. {
  452. csize -= ZipConstants.CryptoHeaderSize;
  453. }
  454. else if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0)
  455. {
  456. throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", csize));
  457. }
  458. }
  459. else
  460. {
  461. inputBuffer.CryptoTransform = null;
  462. }
  463. if ((csize > 0) || ((flags & (int)GeneralBitFlags.Descriptor) != 0))
  464. {
  465. if ((method == CompressionMethod.Deflated) && (inputBuffer.Available > 0))
  466. {
  467. inputBuffer.SetInflaterInput(inf);
  468. }
  469. internalReader = new ReadDataHandler(BodyRead);
  470. return BodyRead(destination, offset, count);
  471. }
  472. else
  473. {
  474. internalReader = new ReadDataHandler(ReadingNotAvailable);
  475. return 0;
  476. }
  477. }
  478. /// <summary>
  479. /// Read a block of bytes from the stream.
  480. /// </summary>
  481. /// <param name="buffer">The destination for the bytes.</param>
  482. /// <param name="offset">The index to start storing data.</param>
  483. /// <param name="count">The number of bytes to attempt to read.</param>
  484. /// <returns>Returns the number of bytes read.</returns>
  485. /// <remarks>Zero bytes read means end of stream.</remarks>
  486. public override int Read(byte[] buffer, int offset, int count)
  487. {
  488. if (buffer == null)
  489. {
  490. throw new ArgumentNullException(nameof(buffer));
  491. }
  492. if (offset < 0)
  493. {
  494. throw new ArgumentOutOfRangeException(nameof(offset), "Cannot be negative");
  495. }
  496. if (count < 0)
  497. {
  498. throw new ArgumentOutOfRangeException(nameof(count), "Cannot be negative");
  499. }
  500. if ((buffer.Length - offset) < count)
  501. {
  502. throw new ArgumentException("Invalid offset/count combination");
  503. }
  504. return internalReader(buffer, offset, count);
  505. }
  506. /// <summary>
  507. /// Reads a block of bytes from the current zip entry.
  508. /// </summary>
  509. /// <returns>
  510. /// The number of bytes read (this may be less than the length requested, even before the end of stream), or 0 on end of stream.
  511. /// </returns>
  512. /// <exception name="IOException">
  513. /// An i/o error occured.
  514. /// </exception>
  515. /// <exception cref="ZipException">
  516. /// The deflated stream is corrupted.
  517. /// </exception>
  518. /// <exception cref="InvalidOperationException">
  519. /// The stream is not open.
  520. /// </exception>
  521. private int BodyRead(byte[] buffer, int offset, int count)
  522. {
  523. if (crc == null)
  524. {
  525. throw new InvalidOperationException("Closed");
  526. }
  527. if ((entry == null) || (count <= 0))
  528. {
  529. return 0;
  530. }
  531. if (offset + count > buffer.Length)
  532. {
  533. throw new ArgumentException("Offset + count exceeds buffer size");
  534. }
  535. bool finished = false;
  536. switch (method)
  537. {
  538. case CompressionMethod.Deflated:
  539. count = base.Read(buffer, offset, count);
  540. if (count <= 0)
  541. {
  542. if (!inf.IsFinished)
  543. {
  544. throw new ZipException("Inflater not finished!");
  545. }
  546. inputBuffer.Available = inf.RemainingInput;
  547. // A csize of -1 is from an unpatched local header
  548. if ((flags & 8) == 0 &&
  549. (inf.TotalIn != csize && csize != 0xFFFFFFFF && csize != -1 || inf.TotalOut != size))
  550. {
  551. throw new ZipException("Size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
  552. }
  553. inf.Reset();
  554. finished = true;
  555. }
  556. break;
  557. case CompressionMethod.Stored:
  558. if ((count > csize) && (csize >= 0))
  559. {
  560. count = (int)csize;
  561. }
  562. if (count > 0)
  563. {
  564. count = inputBuffer.ReadClearTextBuffer(buffer, offset, count);
  565. if (count > 0)
  566. {
  567. csize -= count;
  568. size -= count;
  569. }
  570. }
  571. if (csize == 0)
  572. {
  573. finished = true;
  574. }
  575. else
  576. {
  577. if (count < 0)
  578. {
  579. throw new ZipException("EOF in stored block");
  580. }
  581. }
  582. break;
  583. }
  584. if (count > 0)
  585. {
  586. crc.Update(new ArraySegment<byte>(buffer, offset, count));
  587. }
  588. if (finished)
  589. {
  590. CompleteCloseEntry(true);
  591. }
  592. return count;
  593. }
  594. /// <summary>
  595. /// Closes the zip input stream
  596. /// </summary>
  597. protected override void Dispose(bool disposing)
  598. {
  599. internalReader = new ReadDataHandler(ReadingNotAvailable);
  600. crc = null;
  601. entry = null;
  602. base.Dispose(disposing);
  603. }
  604. }
  605. }