ZipInputStream.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. using System;
  2. using System.IO;
  3. using ICSharpCode.SharpZipLib.Checksum;
  4. using ICSharpCode.SharpZipLib.Encryption;
  5. using ICSharpCode.SharpZipLib.Zip.Compression;
  6. using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
  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. delegate int ReadDataHandler(byte[] b, int offset, int length);
  64. /// <summary>
  65. /// The current reader this instance.
  66. /// </summary>
  67. ReadDataHandler internalReader;
  68. Crc32 crc = new Crc32();
  69. ZipEntry entry;
  70. long size;
  71. int method;
  72. int flags;
  73. string password;
  74. #endregion
  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
  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. get {
  102. return password;
  103. }
  104. set {
  105. password = value;
  106. }
  107. }
  108. /// <summary>
  109. /// Gets a value indicating if there is a current entry and it can be decompressed
  110. /// </summary>
  111. /// <remarks>
  112. /// The entry can only be decompressed if the library supports the zip features required to extract it.
  113. /// See the <see cref="ZipEntry.Version">ZipEntry Version</see> property for more details.
  114. /// </remarks>
  115. public bool CanDecompressEntry {
  116. get {
  117. return (entry != null) && entry.CanDecompress;
  118. }
  119. }
  120. /// <summary>
  121. /// Advances to the next entry in the archive
  122. /// </summary>
  123. /// <returns>
  124. /// The next <see cref="ZipEntry">entry</see> in the archive or null if there are no more entries.
  125. /// </returns>
  126. /// <remarks>
  127. /// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called.
  128. /// </remarks>
  129. /// <exception cref="InvalidOperationException">
  130. /// Input stream is closed
  131. /// </exception>
  132. /// <exception cref="ZipException">
  133. /// Password is not set, password is invalid, compression method is invalid,
  134. /// version required to extract is not supported
  135. /// </exception>
  136. public ZipEntry GetNextEntry()
  137. {
  138. if (crc == null) {
  139. throw new InvalidOperationException("Closed.");
  140. }
  141. if (entry != null) {
  142. CloseEntry();
  143. }
  144. int header = inputBuffer.ReadLeInt();
  145. if (header == ZipConstants.CentralHeaderSignature ||
  146. header == ZipConstants.EndOfCentralDirectorySignature ||
  147. header == ZipConstants.CentralHeaderDigitalSignature ||
  148. header == ZipConstants.ArchiveExtraDataSignature ||
  149. header == ZipConstants.Zip64CentralFileHeaderSignature) {
  150. // No more individual entries exist
  151. Dispose();
  152. return null;
  153. }
  154. // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found
  155. // Spanning signature is same as descriptor signature and is untested as yet.
  156. if ((header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature)) {
  157. header = inputBuffer.ReadLeInt();
  158. }
  159. if (header != ZipConstants.LocalHeaderSignature) {
  160. throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
  161. }
  162. var versionRequiredToExtract = (short)inputBuffer.ReadLeShort();
  163. flags = inputBuffer.ReadLeShort();
  164. method = inputBuffer.ReadLeShort();
  165. var dostime = (uint)inputBuffer.ReadLeInt();
  166. int crc2 = inputBuffer.ReadLeInt();
  167. csize = inputBuffer.ReadLeInt();
  168. size = inputBuffer.ReadLeInt();
  169. int nameLen = inputBuffer.ReadLeShort();
  170. int extraLen = inputBuffer.ReadLeShort();
  171. bool isCrypted = (flags & 1) == 1;
  172. byte[] buffer = new byte[nameLen];
  173. inputBuffer.ReadRawBuffer(buffer);
  174. string name = ZipConstants.ConvertToStringExt(flags, buffer);
  175. entry = new ZipEntry(name, versionRequiredToExtract);
  176. entry.Flags = flags;
  177. entry.CompressionMethod = (CompressionMethod)method;
  178. if ((flags & 8) == 0) {
  179. entry.Crc = crc2 & 0xFFFFFFFFL;
  180. entry.Size = size & 0xFFFFFFFFL;
  181. entry.CompressedSize = csize & 0xFFFFFFFFL;
  182. entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff);
  183. } else {
  184. // This allows for GNU, WinZip and possibly other archives, the PKZIP spec
  185. // says these values are zero under these circumstances.
  186. if (crc2 != 0) {
  187. entry.Crc = crc2 & 0xFFFFFFFFL;
  188. }
  189. if (size != 0) {
  190. entry.Size = size & 0xFFFFFFFFL;
  191. }
  192. if (csize != 0) {
  193. entry.CompressedSize = csize & 0xFFFFFFFFL;
  194. }
  195. entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
  196. }
  197. entry.DosTime = dostime;
  198. // If local header requires Zip64 is true then the extended header should contain
  199. // both values.
  200. // Handle extra data if present. This can set/alter some fields of the entry.
  201. if (extraLen > 0) {
  202. byte[] extra = new byte[extraLen];
  203. inputBuffer.ReadRawBuffer(extra);
  204. entry.ExtraData = extra;
  205. }
  206. entry.ProcessExtraData(true);
  207. if (entry.CompressedSize >= 0) {
  208. csize = entry.CompressedSize;
  209. }
  210. if (entry.Size >= 0) {
  211. size = entry.Size;
  212. }
  213. if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size))) {
  214. throw new ZipException("Stored, but compressed != uncompressed");
  215. }
  216. // Determine how to handle reading of data if this is attempted.
  217. if (entry.IsCompressionMethodSupported()) {
  218. internalReader = new ReadDataHandler(InitialRead);
  219. } else {
  220. internalReader = new ReadDataHandler(ReadingNotSupported);
  221. }
  222. return entry;
  223. }
  224. /// <summary>
  225. /// Read data descriptor at the end of compressed data.
  226. /// </summary>
  227. void ReadDataDescriptor()
  228. {
  229. if (inputBuffer.ReadLeInt() != ZipConstants.DataDescriptorSignature) {
  230. throw new ZipException("Data descriptor signature not found");
  231. }
  232. entry.Crc = inputBuffer.ReadLeInt() & 0xFFFFFFFFL;
  233. if (entry.LocalHeaderRequiresZip64) {
  234. csize = inputBuffer.ReadLeLong();
  235. size = inputBuffer.ReadLeLong();
  236. } else {
  237. csize = inputBuffer.ReadLeInt();
  238. size = inputBuffer.ReadLeInt();
  239. }
  240. entry.CompressedSize = csize;
  241. entry.Size = size;
  242. }
  243. /// <summary>
  244. /// Complete cleanup as the final part of closing.
  245. /// </summary>
  246. /// <param name="testCrc">True if the crc value should be tested</param>
  247. void CompleteCloseEntry(bool testCrc)
  248. {
  249. StopDecrypting();
  250. if ((flags & 8) != 0) {
  251. ReadDataDescriptor();
  252. }
  253. size = 0;
  254. if (testCrc &&
  255. ((crc.Value & 0xFFFFFFFFL) != entry.Crc) && (entry.Crc != -1)) {
  256. throw new ZipException("CRC mismatch");
  257. }
  258. crc.Reset();
  259. if (method == (int)CompressionMethod.Deflated) {
  260. inf.Reset();
  261. }
  262. entry = null;
  263. }
  264. /// <summary>
  265. /// Closes the current zip entry and moves to the next one.
  266. /// </summary>
  267. /// <exception cref="InvalidOperationException">
  268. /// The stream is closed
  269. /// </exception>
  270. /// <exception cref="ZipException">
  271. /// The Zip stream ends early
  272. /// </exception>
  273. public void CloseEntry()
  274. {
  275. if (crc == null) {
  276. throw new InvalidOperationException("Closed");
  277. }
  278. if (entry == null) {
  279. return;
  280. }
  281. if (method == (int)CompressionMethod.Deflated) {
  282. if ((flags & 8) != 0) {
  283. // We don't know how much we must skip, read until end.
  284. byte[] tmp = new byte[4096];
  285. // Read will close this entry
  286. while (Read(tmp, 0, tmp.Length) > 0) {
  287. }
  288. return;
  289. }
  290. csize -= inf.TotalIn;
  291. inputBuffer.Available += inf.RemainingInput;
  292. }
  293. if ((inputBuffer.Available > csize) && (csize >= 0)) {
  294. inputBuffer.Available = (int)((long)inputBuffer.Available - csize);
  295. } else {
  296. csize -= inputBuffer.Available;
  297. inputBuffer.Available = 0;
  298. while (csize != 0) {
  299. long skipped = Skip(csize);
  300. if (skipped <= 0) {
  301. throw new ZipException("Zip archive ends early.");
  302. }
  303. csize -= skipped;
  304. }
  305. }
  306. CompleteCloseEntry(false);
  307. }
  308. /// <summary>
  309. /// Returns 1 if there is an entry available
  310. /// Otherwise returns 0.
  311. /// </summary>
  312. public override int Available {
  313. get {
  314. return entry != null ? 1 : 0;
  315. }
  316. }
  317. /// <summary>
  318. /// Returns the current size that can be read from the current entry if available
  319. /// </summary>
  320. /// <exception cref="ZipException">Thrown if the entry size is not known.</exception>
  321. /// <exception cref="InvalidOperationException">Thrown if no entry is currently available.</exception>
  322. public override long Length {
  323. get {
  324. if (entry != null) {
  325. if (entry.Size >= 0) {
  326. return entry.Size;
  327. } else {
  328. throw new ZipException("Length not available for the current entry");
  329. }
  330. } else {
  331. throw new InvalidOperationException("No current entry");
  332. }
  333. }
  334. }
  335. /// <summary>
  336. /// Reads a byte from the current zip entry.
  337. /// </summary>
  338. /// <returns>
  339. /// The byte or -1 if end of stream is reached.
  340. /// </returns>
  341. public override int ReadByte()
  342. {
  343. byte[] b = new byte[1];
  344. if (Read(b, 0, 1) <= 0) {
  345. return -1;
  346. }
  347. return b[0] & 0xff;
  348. }
  349. /// <summary>
  350. /// Handle attempts to read by throwing an <see cref="InvalidOperationException"/>.
  351. /// </summary>
  352. /// <param name="destination">The destination array to store data in.</param>
  353. /// <param name="offset">The offset at which data read should be stored.</param>
  354. /// <param name="count">The maximum number of bytes to read.</param>
  355. /// <returns>Returns the number of bytes actually read.</returns>
  356. int ReadingNotAvailable(byte[] destination, int offset, int count)
  357. {
  358. throw new InvalidOperationException("Unable to read from this stream");
  359. }
  360. /// <summary>
  361. /// Handle attempts to read from this entry by throwing an exception
  362. /// </summary>
  363. int ReadingNotSupported(byte[] destination, int offset, int count)
  364. {
  365. throw new ZipException("The compression method for this entry is not supported");
  366. }
  367. /// <summary>
  368. /// Perform the initial read on an entry which may include
  369. /// reading encryption headers and setting up inflation.
  370. /// </summary>
  371. /// <param name="destination">The destination to fill with data read.</param>
  372. /// <param name="offset">The offset to start reading at.</param>
  373. /// <param name="count">The maximum number of bytes to read.</param>
  374. /// <returns>The actual number of bytes read.</returns>
  375. int InitialRead(byte[] destination, int offset, int count)
  376. {
  377. if (!CanDecompressEntry) {
  378. throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version + ")");
  379. }
  380. // Handle encryption if required.
  381. if (entry.IsCrypted) {
  382. if (password == null) {
  383. throw new ZipException("No password set.");
  384. }
  385. // Generate and set crypto transform...
  386. var managed = new PkzipClassicManaged();
  387. byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));
  388. inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);
  389. byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
  390. inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);
  391. if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) {
  392. throw new ZipException("Invalid password");
  393. }
  394. if (csize >= ZipConstants.CryptoHeaderSize) {
  395. csize -= ZipConstants.CryptoHeaderSize;
  396. } else if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0) {
  397. throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", csize));
  398. }
  399. } else {
  400. inputBuffer.CryptoTransform = null;
  401. }
  402. if ((csize > 0) || ((flags & (int)GeneralBitFlags.Descriptor) != 0)) {
  403. if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0)) {
  404. inputBuffer.SetInflaterInput(inf);
  405. }
  406. internalReader = new ReadDataHandler(BodyRead);
  407. return BodyRead(destination, offset, count);
  408. } else {
  409. internalReader = new ReadDataHandler(ReadingNotAvailable);
  410. return 0;
  411. }
  412. }
  413. /// <summary>
  414. /// Read a block of bytes from the stream.
  415. /// </summary>
  416. /// <param name="buffer">The destination for the bytes.</param>
  417. /// <param name="offset">The index to start storing data.</param>
  418. /// <param name="count">The number of bytes to attempt to read.</param>
  419. /// <returns>Returns the number of bytes read.</returns>
  420. /// <remarks>Zero bytes read means end of stream.</remarks>
  421. public override int Read(byte[] buffer, int offset, int count)
  422. {
  423. if (buffer == null) {
  424. throw new ArgumentNullException("nameof(buffer)");
  425. }
  426. if (offset < 0) {
  427. throw new ArgumentOutOfRangeException("nameof(offset)", "Cannot be negative");
  428. }
  429. if (count < 0) {
  430. throw new ArgumentOutOfRangeException("nameof(count)", "Cannot be negative");
  431. }
  432. if ((buffer.Length - offset) < count) {
  433. throw new ArgumentException("Invalid offset/count combination");
  434. }
  435. return internalReader(buffer, offset, count);
  436. }
  437. /// <summary>
  438. /// Reads a block of bytes from the current zip entry.
  439. /// </summary>
  440. /// <returns>
  441. /// 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.
  442. /// </returns>
  443. /// <exception name="IOException">
  444. /// An i/o error occured.
  445. /// </exception>
  446. /// <exception cref="ZipException">
  447. /// The deflated stream is corrupted.
  448. /// </exception>
  449. /// <exception cref="InvalidOperationException">
  450. /// The stream is not open.
  451. /// </exception>
  452. int BodyRead(byte[] buffer, int offset, int count)
  453. {
  454. if (crc == null) {
  455. throw new InvalidOperationException("Closed");
  456. }
  457. if ((entry == null) || (count <= 0)) {
  458. return 0;
  459. }
  460. if (offset + count > buffer.Length) {
  461. throw new ArgumentException("Offset + count exceeds buffer size");
  462. }
  463. bool finished = false;
  464. switch (method) {
  465. case (int)CompressionMethod.Deflated:
  466. count = base.Read(buffer, offset, count);
  467. if (count <= 0) {
  468. if (!inf.IsFinished) {
  469. throw new ZipException("Inflater not finished!");
  470. }
  471. inputBuffer.Available = inf.RemainingInput;
  472. // A csize of -1 is from an unpatched local header
  473. if ((flags & 8) == 0 &&
  474. (inf.TotalIn != csize && csize != 0xFFFFFFFF && csize != -1 || inf.TotalOut != size)) {
  475. throw new ZipException("Size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
  476. }
  477. inf.Reset();
  478. finished = true;
  479. }
  480. break;
  481. case (int)CompressionMethod.Stored:
  482. if ((count > csize) && (csize >= 0)) {
  483. count = (int)csize;
  484. }
  485. if (count > 0) {
  486. count = inputBuffer.ReadClearTextBuffer(buffer, offset, count);
  487. if (count > 0) {
  488. csize -= count;
  489. size -= count;
  490. }
  491. }
  492. if (csize == 0) {
  493. finished = true;
  494. } else {
  495. if (count < 0) {
  496. throw new ZipException("EOF in stored block");
  497. }
  498. }
  499. break;
  500. }
  501. if (count > 0) {
  502. crc.Update(buffer, offset, count);
  503. }
  504. if (finished) {
  505. CompleteCloseEntry(true);
  506. }
  507. return count;
  508. }
  509. /// <summary>
  510. /// Closes the zip input stream
  511. /// </summary>
  512. protected override void Dispose(bool disposing)
  513. {
  514. internalReader = new ReadDataHandler(ReadingNotAvailable);
  515. crc = null;
  516. entry = null;
  517. base.Dispose(disposing);
  518. }
  519. }
  520. }