You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4827 lines
169 KiB

4 years ago
  1. // ZipFile.cs
  2. //
  3. // Copyright (C) 2001 Mike Krueger
  4. // Copyright (C) 2004 John Reilly
  5. //
  6. // This file was translated from java, it was part of the GNU Classpath
  7. // Copyright (C) 2001 Free Software Foundation, Inc.
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public License
  11. // as published by the Free Software Foundation; either version 2
  12. // of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. //
  23. // Linking this library statically or dynamically with other modules is
  24. // making a combined work based on this library. Thus, the terms and
  25. // conditions of the GNU General Public License cover the whole
  26. // combination.
  27. //
  28. // As a special exception, the copyright holders of this library give you
  29. // permission to link this library with independent modules to produce an
  30. // executable, regardless of the license terms of these independent
  31. // modules, and to copy and distribute the resulting executable under
  32. // terms of your choice, provided that you also meet, for each linked
  33. // independent module, the terms and conditions of the license of that
  34. // module. An independent module is a module which is not derived from
  35. // or based on this library. If you modify this library, you may extend
  36. // this exception to your version of the library, but you are not
  37. // obligated to do so. If you do not wish to do so, delete this
  38. // exception statement from your version.
  39. // HISTORY
  40. // 2009-12-22 Z-1649 Added AES support
  41. // 2010-03-02 Z-1650 Fixed updating ODT archives in memory. Exposed exceptions in updating.
  42. // 2010-05-25 Z-1663 Fixed exception when testing local header compressed size of -1
  43. using System;
  44. using System.Collections;
  45. using System.IO;
  46. using System.Text;
  47. using System.Globalization;
  48. #if !NETCF_1_0
  49. using System.Security.Cryptography;
  50. using Externals.Compression.Encryption;
  51. #endif
  52. using Externals.Compression.Core;
  53. using Externals.Compression.Checksums;
  54. using Externals.Compression.Zip.Compression.Streams;
  55. using Externals.Compression.Zip.Compression;
  56. namespace Externals.Compression.Zip
  57. {
  58. #region Keys Required Event Args
  59. /// <summary>
  60. /// Arguments used with KeysRequiredEvent
  61. /// </summary>
  62. internal class KeysRequiredEventArgs : EventArgs
  63. {
  64. #region Constructors
  65. /// <summary>
  66. /// Initialise a new instance of <see cref="KeysRequiredEventArgs"></see>
  67. /// </summary>
  68. /// <param name="name">The name of the file for which keys are required.</param>
  69. public KeysRequiredEventArgs(string name)
  70. {
  71. fileName = name;
  72. }
  73. /// <summary>
  74. /// Initialise a new instance of <see cref="KeysRequiredEventArgs"></see>
  75. /// </summary>
  76. /// <param name="name">The name of the file for which keys are required.</param>
  77. /// <param name="keyValue">The current key value.</param>
  78. public KeysRequiredEventArgs(string name, byte[] keyValue)
  79. {
  80. fileName = name;
  81. key = keyValue;
  82. }
  83. #endregion
  84. #region Properties
  85. /// <summary>
  86. /// Gets the name of the file for which keys are required.
  87. /// </summary>
  88. public string FileName
  89. {
  90. get { return fileName; }
  91. }
  92. /// <summary>
  93. /// Gets or sets the key value
  94. /// </summary>
  95. public byte[] Key
  96. {
  97. get { return key; }
  98. set { key = value; }
  99. }
  100. #endregion
  101. #region Instance Fields
  102. string fileName;
  103. byte[] key;
  104. #endregion
  105. }
  106. #endregion
  107. #region Test Definitions
  108. /// <summary>
  109. /// The strategy to apply to testing.
  110. /// </summary>
  111. internal enum TestStrategy
  112. {
  113. /// <summary>
  114. /// Find the first error only.
  115. /// </summary>
  116. FindFirstError,
  117. /// <summary>
  118. /// Find all possible errors.
  119. /// </summary>
  120. FindAllErrors,
  121. }
  122. /// <summary>
  123. /// The operation in progress reported by a <see cref="ZipTestResultHandler"/> during testing.
  124. /// </summary>
  125. /// <seealso cref="ZipFile.TestArchive(bool)">TestArchive</seealso>
  126. internal enum TestOperation
  127. {
  128. /// <summary>
  129. /// Setting up testing.
  130. /// </summary>
  131. Initialising,
  132. /// <summary>
  133. /// Testing an individual entries header
  134. /// </summary>
  135. EntryHeader,
  136. /// <summary>
  137. /// Testing an individual entries data
  138. /// </summary>
  139. EntryData,
  140. /// <summary>
  141. /// Testing an individual entry has completed.
  142. /// </summary>
  143. EntryComplete,
  144. /// <summary>
  145. /// Running miscellaneous tests
  146. /// </summary>
  147. MiscellaneousTests,
  148. /// <summary>
  149. /// Testing is complete
  150. /// </summary>
  151. Complete,
  152. }
  153. /// <summary>
  154. /// Status returned returned by <see cref="ZipTestResultHandler"/> during testing.
  155. /// </summary>
  156. /// <seealso cref="ZipFile.TestArchive(bool)">TestArchive</seealso>
  157. internal class TestStatus
  158. {
  159. #region Constructors
  160. /// <summary>
  161. /// Initialise a new instance of <see cref="TestStatus"/>
  162. /// </summary>
  163. /// <param name="file">The <see cref="ZipFile"/> this status applies to.</param>
  164. public TestStatus(ZipFile file)
  165. {
  166. file_ = file;
  167. }
  168. #endregion
  169. #region Properties
  170. /// <summary>
  171. /// Get the current <see cref="TestOperation"/> in progress.
  172. /// </summary>
  173. public TestOperation Operation
  174. {
  175. get { return operation_; }
  176. }
  177. /// <summary>
  178. /// Get the <see cref="ZipFile"/> this status is applicable to.
  179. /// </summary>
  180. public ZipFile File
  181. {
  182. get { return file_; }
  183. }
  184. /// <summary>
  185. /// Get the current/last entry tested.
  186. /// </summary>
  187. public ZipEntry Entry
  188. {
  189. get { return entry_; }
  190. }
  191. /// <summary>
  192. /// Get the number of errors detected so far.
  193. /// </summary>
  194. public int ErrorCount
  195. {
  196. get { return errorCount_; }
  197. }
  198. /// <summary>
  199. /// Get the number of bytes tested so far for the current entry.
  200. /// </summary>
  201. public long BytesTested
  202. {
  203. get { return bytesTested_; }
  204. }
  205. /// <summary>
  206. /// Get a value indicating wether the last entry test was valid.
  207. /// </summary>
  208. public bool EntryValid
  209. {
  210. get { return entryValid_; }
  211. }
  212. #endregion
  213. #region Internal API
  214. internal void AddError()
  215. {
  216. errorCount_++;
  217. entryValid_ = false;
  218. }
  219. internal void SetOperation(TestOperation operation)
  220. {
  221. operation_ = operation;
  222. }
  223. internal void SetEntry(ZipEntry entry)
  224. {
  225. entry_ = entry;
  226. entryValid_ = true;
  227. bytesTested_ = 0;
  228. }
  229. internal void SetBytesTested(long value)
  230. {
  231. bytesTested_ = value;
  232. }
  233. #endregion
  234. #region Instance Fields
  235. ZipFile file_;
  236. ZipEntry entry_;
  237. bool entryValid_;
  238. int errorCount_;
  239. long bytesTested_;
  240. TestOperation operation_;
  241. #endregion
  242. }
  243. /// <summary>
  244. /// Delegate invoked during <see cref="ZipFile.TestArchive(bool, TestStrategy, ZipTestResultHandler)">testing</see> if supplied indicating current progress and status.
  245. /// </summary>
  246. /// <remarks>If the message is non-null an error has occured. If the message is null
  247. /// the operation as found in <see cref="TestStatus">status</see> has started.</remarks>
  248. internal delegate void ZipTestResultHandler(TestStatus status, string message);
  249. #endregion
  250. #region Update Definitions
  251. /// <summary>
  252. /// The possible ways of <see cref="ZipFile.CommitUpdate()">applying updates</see> to an archive.
  253. /// </summary>
  254. internal enum FileUpdateMode
  255. {
  256. /// <summary>
  257. /// Perform all updates on temporary files ensuring that the original file is saved.
  258. /// </summary>
  259. Safe,
  260. /// <summary>
  261. /// Update the archive directly, which is faster but less safe.
  262. /// </summary>
  263. Direct,
  264. }
  265. #endregion
  266. #region ZipFile Class
  267. /// <summary>
  268. /// This class represents a Zip archive. You can ask for the contained
  269. /// entries, or get an input stream for a file entry. The entry is
  270. /// automatically decompressed.
  271. ///
  272. /// You can also update the archive adding or deleting entries.
  273. ///
  274. /// This class is thread safe for input: You can open input streams for arbitrary
  275. /// entries in different threads.
  276. /// <br/>
  277. /// <br/>Author of the original java version : Jochen Hoenicke
  278. /// </summary>
  279. /// <example>
  280. /// <code>
  281. /// using System;
  282. /// using System.Text;
  283. /// using System.Collections;
  284. /// using System.IO;
  285. ///
  286. /// using Externals.Compression.Zip;
  287. ///
  288. /// class MainClass
  289. /// {
  290. /// static public void Main(string[] args)
  291. /// {
  292. /// using (ZipFile zFile = new ZipFile(args[0])) {
  293. /// Console.WriteLine("Listing of : " + zFile.Name);
  294. /// Console.WriteLine("");
  295. /// Console.WriteLine("Raw Size Size Date Time Name");
  296. /// Console.WriteLine("-------- -------- -------- ------ ---------");
  297. /// foreach (ZipEntry e in zFile) {
  298. /// if ( e.IsFile ) {
  299. /// DateTime d = e.DateTime;
  300. /// Console.WriteLine("{0, -10}{1, -10}{2} {3} {4}", e.Size, e.CompressedSize,
  301. /// d.ToString("dd-MM-yy"), d.ToString("HH:mm"),
  302. /// e.Name);
  303. /// }
  304. /// }
  305. /// }
  306. /// }
  307. /// }
  308. /// </code>
  309. /// </example>
  310. internal class ZipFile : IEnumerable, IDisposable
  311. {
  312. #region KeyHandling
  313. /// <summary>
  314. /// Delegate for handling keys/password setting during compresion/decompression.
  315. /// </summary>
  316. internal delegate void KeysRequiredEventHandler(
  317. object sender,
  318. KeysRequiredEventArgs e
  319. );
  320. /// <summary>
  321. /// Event handler for handling encryption keys.
  322. /// </summary>
  323. internal KeysRequiredEventHandler KeysRequired = null;
  324. /// <summary>
  325. /// Handles getting of encryption keys when required.
  326. /// </summary>
  327. /// <param name="fileName">The file for which encryption keys are required.</param>
  328. void OnKeysRequired(string fileName)
  329. {
  330. if (KeysRequired != null)
  331. {
  332. KeysRequiredEventArgs krea = new KeysRequiredEventArgs(fileName, key);
  333. KeysRequired(this, krea);
  334. key = krea.Key;
  335. }
  336. }
  337. /// <summary>
  338. /// Get/set the encryption key value.
  339. /// </summary>
  340. byte[] Key
  341. {
  342. get { return key; }
  343. set { key = value; }
  344. }
  345. #if !NETCF_1_0
  346. /// <summary>
  347. /// Password to be used for encrypting/decrypting files.
  348. /// </summary>
  349. /// <remarks>Set to null if no password is required.</remarks>
  350. public string Password
  351. {
  352. set
  353. {
  354. if ((value == null) || (value.Length == 0))
  355. {
  356. key = null;
  357. }
  358. else
  359. {
  360. rawPassword_ = value;
  361. key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(value));
  362. }
  363. }
  364. }
  365. #endif
  366. /// <summary>
  367. /// Get a value indicating wether encryption keys are currently available.
  368. /// </summary>
  369. bool HaveKeys
  370. {
  371. get { return key != null; }
  372. }
  373. #endregion
  374. #region Constructors
  375. /// <summary>
  376. /// Opens a Zip file with the given name for reading.
  377. /// </summary>
  378. /// <param name="name">The name of the file to open.</param>
  379. /// <exception cref="ArgumentNullException">The argument supplied is null.</exception>
  380. /// <exception cref="IOException">
  381. /// An i/o error occurs
  382. /// </exception>
  383. /// <exception cref="ZipException">
  384. /// The file doesn't contain a valid zip archive.
  385. /// </exception>
  386. public ZipFile(string name)
  387. {
  388. if (name == null)
  389. {
  390. throw new ArgumentNullException("name");
  391. }
  392. name_ = name;
  393. baseStream_ = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
  394. isStreamOwner = true;
  395. try
  396. {
  397. ReadEntries();
  398. }
  399. catch
  400. {
  401. DisposeInternal(true);
  402. throw;
  403. }
  404. }
  405. /// <summary>
  406. /// Opens a Zip file reading the given <see cref="FileStream"/>.
  407. /// </summary>
  408. /// <param name="file">The <see cref="FileStream"/> to read archive data from.</param>
  409. /// <exception cref="ArgumentNullException">The supplied argument is null.</exception>
  410. /// <exception cref="IOException">
  411. /// An i/o error occurs.
  412. /// </exception>
  413. /// <exception cref="ZipException">
  414. /// The file doesn't contain a valid zip archive.
  415. /// </exception>
  416. public ZipFile(FileStream file)
  417. {
  418. if (file == null)
  419. {
  420. throw new ArgumentNullException("file");
  421. }
  422. if (!file.CanSeek)
  423. {
  424. throw new ArgumentException("Stream is not seekable", "file");
  425. }
  426. baseStream_ = file;
  427. name_ = file.Name;
  428. isStreamOwner = true;
  429. try
  430. {
  431. ReadEntries();
  432. }
  433. catch
  434. {
  435. DisposeInternal(true);
  436. throw;
  437. }
  438. }
  439. /// <summary>
  440. /// Opens a Zip file reading the given <see cref="Stream"/>.
  441. /// </summary>
  442. /// <param name="stream">The <see cref="Stream"/> to read archive data from.</param>
  443. /// <exception cref="IOException">
  444. /// An i/o error occurs
  445. /// </exception>
  446. /// <exception cref="ZipException">
  447. /// The stream doesn't contain a valid zip archive.<br/>
  448. /// </exception>
  449. /// <exception cref="ArgumentException">
  450. /// The <see cref="Stream">stream</see> doesnt support seeking.
  451. /// </exception>
  452. /// <exception cref="ArgumentNullException">
  453. /// The <see cref="Stream">stream</see> argument is null.
  454. /// </exception>
  455. public ZipFile(Stream stream)
  456. {
  457. if (stream == null)
  458. {
  459. throw new ArgumentNullException("stream");
  460. }
  461. if (!stream.CanSeek)
  462. {
  463. throw new ArgumentException("Stream is not seekable", "stream");
  464. }
  465. baseStream_ = stream;
  466. isStreamOwner = true;
  467. if (baseStream_.Length > 0)
  468. {
  469. try
  470. {
  471. ReadEntries();
  472. }
  473. catch
  474. {
  475. DisposeInternal(true);
  476. throw;
  477. }
  478. }
  479. else
  480. {
  481. entries_ = new ZipEntry[0];
  482. isNewArchive_ = true;
  483. }
  484. }
  485. /// <summary>
  486. /// Initialises a default <see cref="ZipFile"/> instance with no entries and no file storage.
  487. /// </summary>
  488. internal ZipFile()
  489. {
  490. entries_ = new ZipEntry[0];
  491. isNewArchive_ = true;
  492. }
  493. #endregion
  494. #region Destructors and Closing
  495. /// <summary>
  496. /// Finalize this instance.
  497. /// </summary>
  498. ~ZipFile()
  499. {
  500. Dispose(false);
  501. }
  502. /// <summary>
  503. /// Closes the ZipFile. If the stream is <see cref="IsStreamOwner">owned</see> then this also closes the underlying input stream.
  504. /// Once closed, no further instance methods should be called.
  505. /// </summary>
  506. /// <exception cref="System.IO.IOException">
  507. /// An i/o error occurs.
  508. /// </exception>
  509. public void Close()
  510. {
  511. DisposeInternal(true);
  512. GC.SuppressFinalize(this);
  513. }
  514. #endregion
  515. #region Creators
  516. /// <summary>
  517. /// Create a new <see cref="ZipFile"/> whose data will be stored in a file.
  518. /// </summary>
  519. /// <param name="fileName">The name of the archive to create.</param>
  520. /// <returns>Returns the newly created <see cref="ZipFile"/></returns>
  521. /// <exception cref="ArgumentNullException"><paramref name="fileName"></paramref> is null</exception>
  522. public static ZipFile Create(string fileName)
  523. {
  524. if (fileName == null)
  525. {
  526. throw new ArgumentNullException("fileName");
  527. }
  528. FileStream fs = File.Create(fileName);
  529. ZipFile result = new ZipFile();
  530. result.name_ = fileName;
  531. result.baseStream_ = fs;
  532. result.isStreamOwner = true;
  533. return result;
  534. }
  535. /// <summary>
  536. /// Create a new <see cref="ZipFile"/> whose data will be stored on a stream.
  537. /// </summary>
  538. /// <param name="outStream">The stream providing data storage.</param>
  539. /// <returns>Returns the newly created <see cref="ZipFile"/></returns>
  540. /// <exception cref="ArgumentNullException"><paramref name="outStream"> is null</paramref></exception>
  541. /// <exception cref="ArgumentException"><paramref name="outStream"> doesnt support writing.</paramref></exception>
  542. public static ZipFile Create(Stream outStream)
  543. {
  544. if (outStream == null)
  545. {
  546. throw new ArgumentNullException("outStream");
  547. }
  548. if (!outStream.CanWrite)
  549. {
  550. throw new ArgumentException("Stream is not writeable", "outStream");
  551. }
  552. if (!outStream.CanSeek)
  553. {
  554. throw new ArgumentException("Stream is not seekable", "outStream");
  555. }
  556. ZipFile result = new ZipFile();
  557. result.baseStream_ = outStream;
  558. return result;
  559. }
  560. #endregion
  561. #region Properties
  562. /// <summary>
  563. /// Get/set a flag indicating if the underlying stream is owned by the ZipFile instance.
  564. /// If the flag is true then the stream will be closed when <see cref="Close">Close</see> is called.
  565. /// </summary>
  566. /// <remarks>
  567. /// The default value is true in all cases.
  568. /// </remarks>
  569. public bool IsStreamOwner
  570. {
  571. get { return isStreamOwner; }
  572. set { isStreamOwner = value; }
  573. }
  574. /// <summary>
  575. /// Get a value indicating wether
  576. /// this archive is embedded in another file or not.
  577. /// </summary>
  578. public bool IsEmbeddedArchive
  579. {
  580. // Not strictly correct in all circumstances currently
  581. get { return offsetOfFirstEntry > 0; }
  582. }
  583. /// <summary>
  584. /// Get a value indicating that this archive is a new one.
  585. /// </summary>
  586. public bool IsNewArchive
  587. {
  588. get { return isNewArchive_; }
  589. }
  590. /// <summary>
  591. /// Gets the comment for the zip file.
  592. /// </summary>
  593. public string ZipFileComment
  594. {
  595. get { return comment_; }
  596. }
  597. /// <summary>
  598. /// Gets the name of this zip file.
  599. /// </summary>
  600. public string Name
  601. {
  602. get { return name_; }
  603. }
  604. /// <summary>
  605. /// Gets the number of entries in this zip file.
  606. /// </summary>
  607. /// <exception cref="InvalidOperationException">
  608. /// The Zip file has been closed.
  609. /// </exception>
  610. [Obsolete("Use the Count property instead")]
  611. public int Size
  612. {
  613. get
  614. {
  615. return entries_.Length;
  616. }
  617. }
  618. /// <summary>
  619. /// Get the number of entries contained in this <see cref="ZipFile"/>.
  620. /// </summary>
  621. public long Count
  622. {
  623. get
  624. {
  625. return entries_.Length;
  626. }
  627. }
  628. /// <summary>
  629. /// Indexer property for ZipEntries
  630. /// </summary>
  631. [System.Runtime.CompilerServices.IndexerNameAttribute("EntryByIndex")]
  632. public ZipEntry this[int index]
  633. {
  634. get
  635. {
  636. return (ZipEntry)entries_[index].Clone();
  637. }
  638. }
  639. #endregion
  640. #region Input Handling
  641. /// <summary>
  642. /// Gets an enumerator for the Zip entries in this Zip file.
  643. /// </summary>
  644. /// <returns>Returns an <see cref="IEnumerator"/> for this archive.</returns>
  645. /// <exception cref="ObjectDisposedException">
  646. /// The Zip file has been closed.
  647. /// </exception>
  648. public IEnumerator GetEnumerator()
  649. {
  650. if (isDisposed_)
  651. {
  652. throw new ObjectDisposedException("ZipFile");
  653. }
  654. return new ZipEntryEnumerator(entries_);
  655. }
  656. /// <summary>
  657. /// Return the index of the entry with a matching name
  658. /// </summary>
  659. /// <param name="name">Entry name to find</param>
  660. /// <param name="ignoreCase">If true the comparison is case insensitive</param>
  661. /// <returns>The index position of the matching entry or -1 if not found</returns>
  662. /// <exception cref="ObjectDisposedException">
  663. /// The Zip file has been closed.
  664. /// </exception>
  665. public int FindEntry(string name, bool ignoreCase)
  666. {
  667. if (isDisposed_)
  668. {
  669. throw new ObjectDisposedException("ZipFile");
  670. }
  671. // TODO: This will be slow as the next ice age for huge archives!
  672. for (int i = 0; i < entries_.Length; i++)
  673. {
  674. if (string.Compare(name, entries_[i].Name, ignoreCase, CultureInfo.InvariantCulture) == 0)
  675. {
  676. return i;
  677. }
  678. }
  679. return -1;
  680. }
  681. /// <summary>
  682. /// Searches for a zip entry in this archive with the given name.
  683. /// String comparisons are case insensitive
  684. /// </summary>
  685. /// <param name="name">
  686. /// The name to find. May contain directory components separated by slashes ('/').
  687. /// </param>
  688. /// <returns>
  689. /// A clone of the zip entry, or null if no entry with that name exists.
  690. /// </returns>
  691. /// <exception cref="ObjectDisposedException">
  692. /// The Zip file has been closed.
  693. /// </exception>
  694. public ZipEntry GetEntry(string name)
  695. {
  696. if (isDisposed_)
  697. {
  698. throw new ObjectDisposedException("ZipFile");
  699. }
  700. int index = FindEntry(name, true);
  701. return (index >= 0) ? (ZipEntry)entries_[index].Clone() : null;
  702. }
  703. /// <summary>
  704. /// Gets an input stream for reading the given zip entry data in an uncompressed form.
  705. /// Normally the <see cref="ZipEntry"/> should be an entry returned by GetEntry().
  706. /// </summary>
  707. /// <param name="entry">The <see cref="ZipEntry"/> to obtain a data <see cref="Stream"/> for</param>
  708. /// <returns>An input <see cref="Stream"/> containing data for this <see cref="ZipEntry"/></returns>
  709. /// <exception cref="ObjectDisposedException">
  710. /// The ZipFile has already been closed
  711. /// </exception>
  712. /// <exception cref="Externals.Compression.Zip.ZipException">
  713. /// The compression method for the entry is unknown
  714. /// </exception>
  715. /// <exception cref="IndexOutOfRangeException">
  716. /// The entry is not found in the ZipFile
  717. /// </exception>
  718. public Stream GetInputStream(ZipEntry entry)
  719. {
  720. if (entry == null)
  721. {
  722. throw new ArgumentNullException("entry");
  723. }
  724. if (isDisposed_)
  725. {
  726. throw new ObjectDisposedException("ZipFile");
  727. }
  728. long index = entry.ZipFileIndex;
  729. if ((index < 0) || (index >= entries_.Length) || (entries_[index].Name != entry.Name))
  730. {
  731. index = FindEntry(entry.Name, true);
  732. if (index < 0)
  733. {
  734. throw new ZipException("Entry cannot be found");
  735. }
  736. }
  737. return GetInputStream(index);
  738. }
  739. /// <summary>
  740. /// Creates an input stream reading a zip entry
  741. /// </summary>
  742. /// <param name="entryIndex">The index of the entry to obtain an input stream for.</param>
  743. /// <returns>
  744. /// An input <see cref="Stream"/> containing data for this <paramref name="entryIndex"/>
  745. /// </returns>
  746. /// <exception cref="ObjectDisposedException">
  747. /// The ZipFile has already been closed
  748. /// </exception>
  749. /// <exception cref="Externals.Compression.Zip.ZipException">
  750. /// The compression method for the entry is unknown
  751. /// </exception>
  752. /// <exception cref="IndexOutOfRangeException">
  753. /// The entry is not found in the ZipFile
  754. /// </exception>
  755. public Stream GetInputStream(long entryIndex)
  756. {
  757. if (isDisposed_)
  758. {
  759. throw new ObjectDisposedException("ZipFile");
  760. }
  761. long start = LocateEntry(entries_[entryIndex]);
  762. CompressionMethod method = entries_[entryIndex].CompressionMethod;
  763. Stream result = new PartialInputStream(this, start, entries_[entryIndex].CompressedSize);
  764. if (entries_[entryIndex].IsCrypted == true)
  765. {
  766. #if NETCF_1_0
  767. throw new ZipException("decryption not supported for Compact Framework 1.0");
  768. #else
  769. result = CreateAndInitDecryptionStream(result, entries_[entryIndex]);
  770. if (result == null)
  771. {
  772. throw new ZipException("Unable to decrypt this entry");
  773. }
  774. #endif
  775. }
  776. switch (method)
  777. {
  778. case CompressionMethod.Stored:
  779. // read as is.
  780. break;
  781. case CompressionMethod.Deflated:
  782. // No need to worry about ownership and closing as underlying stream close does nothing.
  783. result = new InflaterInputStream(result, new Inflater(true));
  784. break;
  785. default:
  786. throw new ZipException("Unsupported compression method " + method);
  787. }
  788. return result;
  789. }
  790. #endregion
  791. #region Archive Testing
  792. /// <summary>
  793. /// Test an archive for integrity/validity
  794. /// </summary>
  795. /// <param name="testData">Perform low level data Crc check</param>
  796. /// <returns>true if all tests pass, false otherwise</returns>
  797. /// <remarks>Testing will terminate on the first error found.</remarks>
  798. public bool TestArchive(bool testData)
  799. {
  800. return TestArchive(testData, TestStrategy.FindFirstError, null);
  801. }
  802. /// <summary>
  803. /// Test an archive for integrity/validity
  804. /// </summary>
  805. /// <param name="testData">Perform low level data Crc check</param>
  806. /// <param name="strategy">The <see cref="TestStrategy"></see> to apply.</param>
  807. /// <param name="resultHandler">The <see cref="ZipTestResultHandler"></see> handler to call during testing.</param>
  808. /// <returns>true if all tests pass, false otherwise</returns>
  809. /// <exception cref="ObjectDisposedException">The object has already been closed.</exception>
  810. public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandler resultHandler)
  811. {
  812. if (isDisposed_)
  813. {
  814. throw new ObjectDisposedException("ZipFile");
  815. }
  816. TestStatus status = new TestStatus(this);
  817. if (resultHandler != null)
  818. {
  819. resultHandler(status, null);
  820. }
  821. HeaderTest test = testData ? (HeaderTest.Header | HeaderTest.Extract) : HeaderTest.Header;
  822. bool testing = true;
  823. try
  824. {
  825. int entryIndex = 0;
  826. while (testing && (entryIndex < Count))
  827. {
  828. if (resultHandler != null)
  829. {
  830. status.SetEntry(this[entryIndex]);
  831. status.SetOperation(TestOperation.EntryHeader);
  832. resultHandler(status, null);
  833. }
  834. try
  835. {
  836. TestLocalHeader(this[entryIndex], test);
  837. }
  838. catch (ZipException ex)
  839. {
  840. status.AddError();
  841. if (resultHandler != null)
  842. {
  843. resultHandler(status,
  844. string.Format("Exception during test - '{0}'", ex.Message));
  845. }
  846. if (strategy == TestStrategy.FindFirstError)
  847. {
  848. testing = false;
  849. }
  850. }
  851. if (testing && testData && this[entryIndex].IsFile)
  852. {
  853. if (resultHandler != null)
  854. {
  855. status.SetOperation(TestOperation.EntryData);
  856. resultHandler(status, null);
  857. }
  858. Crc32 crc = new Crc32();
  859. using (Stream entryStream = this.GetInputStream(this[entryIndex]))
  860. {
  861. byte[] buffer = new byte[4096];
  862. long totalBytes = 0;
  863. int bytesRead;
  864. while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0)
  865. {
  866. crc.Update(buffer, 0, bytesRead);
  867. if (resultHandler != null)
  868. {
  869. totalBytes += bytesRead;
  870. status.SetBytesTested(totalBytes);
  871. resultHandler(status, null);
  872. }
  873. }
  874. }
  875. if (this[entryIndex].Crc != crc.Value)
  876. {
  877. status.AddError();
  878. if (resultHandler != null)
  879. {
  880. resultHandler(status, "CRC mismatch");
  881. }
  882. if (strategy == TestStrategy.FindFirstError)
  883. {
  884. testing = false;
  885. }
  886. }
  887. if ((this[entryIndex].Flags & (int)GeneralBitFlags.Descriptor) != 0)
  888. {
  889. ZipHelperStream helper = new ZipHelperStream(baseStream_);
  890. DescriptorData data = new DescriptorData();
  891. helper.ReadDataDescriptor(this[entryIndex].LocalHeaderRequiresZip64, data);
  892. if (this[entryIndex].Crc != data.Crc)
  893. {
  894. status.AddError();
  895. }
  896. if (this[entryIndex].CompressedSize != data.CompressedSize)
  897. {
  898. status.AddError();
  899. }
  900. if (this[entryIndex].Size != data.Size)
  901. {
  902. status.AddError();
  903. }
  904. }
  905. }
  906. if (resultHandler != null)
  907. {
  908. status.SetOperation(TestOperation.EntryComplete);
  909. resultHandler(status, null);
  910. }
  911. entryIndex += 1;
  912. }
  913. if (resultHandler != null)
  914. {
  915. status.SetOperation(TestOperation.MiscellaneousTests);
  916. resultHandler(status, null);
  917. }
  918. // TODO: the 'Corrina Johns' test where local headers are missing from
  919. // the central directory. They are therefore invisible to many archivers.
  920. }
  921. catch (Exception ex)
  922. {
  923. status.AddError();
  924. if (resultHandler != null)
  925. {
  926. resultHandler(status, string.Format("Exception during test - '{0}'", ex.Message));
  927. }
  928. }
  929. if (resultHandler != null)
  930. {
  931. status.SetOperation(TestOperation.Complete);
  932. status.SetEntry(null);
  933. resultHandler(status, null);
  934. }
  935. return (status.ErrorCount == 0);
  936. }
  937. [Flags]
  938. enum HeaderTest
  939. {
  940. Extract = 0x01, // Check that this header represents an entry whose data can be extracted
  941. Header = 0x02, // Check that this header contents are valid
  942. }
  943. /// <summary>
  944. /// Test a local header against that provided from the central directory
  945. /// </summary>
  946. /// <param name="entry">
  947. /// The entry to test against
  948. /// </param>
  949. /// <param name="tests">The type of <see cref="HeaderTest">tests</see> to carry out.</param>
  950. /// <returns>The offset of the entries data in the file</returns>
  951. long TestLocalHeader(ZipEntry entry, HeaderTest tests)
  952. {
  953. lock (baseStream_)
  954. {
  955. bool testHeader = (tests & HeaderTest.Header) != 0;
  956. bool testData = (tests & HeaderTest.Extract) != 0;
  957. baseStream_.Seek(offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin);
  958. if ((int)ReadLEUint() != ZipConstants.LocalHeaderSignature)
  959. {
  960. throw new ZipException(string.Format("Wrong local header signature @{0:X}", offsetOfFirstEntry + entry.Offset));
  961. }
  962. short extractVersion = (short)ReadLEUshort();
  963. short localFlags = (short)ReadLEUshort();
  964. short compressionMethod = (short)ReadLEUshort();
  965. short fileTime = (short)ReadLEUshort();
  966. short fileDate = (short)ReadLEUshort();
  967. uint crcValue = ReadLEUint();
  968. long compressedSize = ReadLEUint();
  969. long size = ReadLEUint();
  970. int storedNameLength = ReadLEUshort();
  971. int extraDataLength = ReadLEUshort();
  972. byte[] nameData = new byte[storedNameLength];
  973. StreamUtils.ReadFully(baseStream_, nameData);
  974. byte[] extraData = new byte[extraDataLength];
  975. StreamUtils.ReadFully(baseStream_, extraData);
  976. ZipExtraData localExtraData = new ZipExtraData(extraData);
  977. // Extra data / zip64 checks
  978. if (localExtraData.Find(1))
  979. {
  980. // 2010-03-04 Forum 10512: removed checks for version >= ZipConstants.VersionZip64
  981. // and size or compressedSize = MaxValue, due to rogue creators.
  982. size = localExtraData.ReadLong();
  983. compressedSize = localExtraData.ReadLong();
  984. if ((localFlags & (int)GeneralBitFlags.Descriptor) != 0)
  985. {
  986. // These may be valid if patched later
  987. if ((size != -1) && (size != entry.Size))
  988. {
  989. throw new ZipException("Size invalid for descriptor");
  990. }
  991. if ((compressedSize != -1) && (compressedSize != entry.CompressedSize))
  992. {
  993. throw new ZipException("Compressed size invalid for descriptor");
  994. }
  995. }
  996. }
  997. else
  998. {
  999. // No zip64 extra data but entry requires it.
  1000. if ((extractVersion >= ZipConstants.VersionZip64) &&
  1001. (((uint)size == uint.MaxValue) || ((uint)compressedSize == uint.MaxValue)))
  1002. {
  1003. throw new ZipException("Required Zip64 extended information missing");
  1004. }
  1005. }
  1006. if (testData)
  1007. {
  1008. if (entry.IsFile)
  1009. {
  1010. if (!entry.IsCompressionMethodSupported())
  1011. {
  1012. throw new ZipException("Compression method not supported");
  1013. }
  1014. if ((extractVersion > ZipConstants.VersionMadeBy)
  1015. || ((extractVersion > 20) && (extractVersion < ZipConstants.VersionZip64)))
  1016. {
  1017. throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", extractVersion));
  1018. }
  1019. if ((localFlags & (int)(GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked)) != 0)
  1020. {
  1021. throw new ZipException("The library does not support the zip version required to extract this entry");
  1022. }
  1023. }
  1024. }
  1025. if (testHeader)
  1026. {
  1027. if ((extractVersion <= 63) && // Ignore later versions as we dont know about them..
  1028. (extractVersion != 10) &&
  1029. (extractVersion != 11) &&
  1030. (extractVersion != 20) &&
  1031. (extractVersion != 21) &&
  1032. (extractVersion != 25) &&
  1033. (extractVersion != 27) &&
  1034. (extractVersion != 45) &&
  1035. (extractVersion != 46) &&
  1036. (extractVersion != 50) &&
  1037. (extractVersion != 51) &&
  1038. (extractVersion != 52) &&
  1039. (extractVersion != 61) &&
  1040. (extractVersion != 62) &&
  1041. (extractVersion != 63)
  1042. )
  1043. {
  1044. throw new ZipException(string.Format("Version required to extract this entry is invalid ({0})", extractVersion));
  1045. }
  1046. // Local entry flags dont have reserved bit set on.
  1047. if ((localFlags & (int)(GeneralBitFlags.ReservedPKware4 | GeneralBitFlags.ReservedPkware14 | GeneralBitFlags.ReservedPkware15)) != 0)
  1048. {
  1049. throw new ZipException("Reserved bit flags cannot be set.");
  1050. }
  1051. // Encryption requires extract version >= 20
  1052. if (((localFlags & (int)GeneralBitFlags.Encrypted) != 0) && (extractVersion < 20))
  1053. {
  1054. throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
  1055. }
  1056. // Strong encryption requires encryption flag to be set and extract version >= 50.
  1057. if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0)
  1058. {
  1059. if ((localFlags & (int)GeneralBitFlags.Encrypted) == 0)
  1060. {
  1061. throw new ZipException("Strong encryption flag set but encryption flag is not set");
  1062. }
  1063. if (extractVersion < 50)
  1064. {
  1065. throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion));
  1066. }
  1067. }
  1068. // Patched entries require extract version >= 27
  1069. if (((localFlags & (int)GeneralBitFlags.Patched) != 0) && (extractVersion < 27))
  1070. {
  1071. throw new ZipException(string.Format("Patched data requires higher version than ({0})", extractVersion));
  1072. }
  1073. // Central header flags match local entry flags.
  1074. if (localFlags != entry.Flags)
  1075. {
  1076. throw new ZipException("Central header/local header flags mismatch");
  1077. }
  1078. // Central header compression method matches local entry
  1079. if (entry.CompressionMethod != (CompressionMethod)compressionMethod)
  1080. {
  1081. throw new ZipException("Central header/local header compression method mismatch");
  1082. }
  1083. if (entry.Version != extractVersion)
  1084. {
  1085. throw new ZipException("Extract version mismatch");
  1086. }
  1087. // Strong encryption and extract version match
  1088. if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0)
  1089. {
  1090. if (extractVersion < 62)
  1091. {
  1092. throw new ZipException("Strong encryption flag set but version not high enough");
  1093. }
  1094. }
  1095. if ((localFlags & (int)GeneralBitFlags.HeaderMasked) != 0)
  1096. {
  1097. if ((fileTime != 0) || (fileDate != 0))
  1098. {
  1099. throw new ZipException("Header masked set but date/time values non-zero");
  1100. }
  1101. }
  1102. if ((localFlags & (int)GeneralBitFlags.Descriptor) == 0)
  1103. {
  1104. if (crcValue != (uint)entry.Crc)
  1105. {
  1106. throw new ZipException("Central header/local header crc mismatch");
  1107. }
  1108. }
  1109. // Crc valid for empty entry.
  1110. // This will also apply to streamed entries where size isnt known and the header cant be patched
  1111. if ((size == 0) && (compressedSize == 0))
  1112. {
  1113. if (crcValue != 0)
  1114. {
  1115. throw new ZipException("Invalid CRC for empty entry");
  1116. }
  1117. }
  1118. // TODO: make test more correct... can't compare lengths as was done originally as this can fail for MBCS strings
  1119. // Assuming a code page at this point is not valid? Best is to store the name length in the ZipEntry probably
  1120. if (entry.Name.Length > storedNameLength)
  1121. {
  1122. throw new ZipException("File name length mismatch");
  1123. }
  1124. // Name data has already been read convert it and compare.
  1125. string localName = ZipConstants.ConvertToStringExt(localFlags, nameData);
  1126. // Central directory and local entry name match
  1127. if (localName != entry.Name)
  1128. {
  1129. throw new ZipException("Central header and local header file name mismatch");
  1130. }
  1131. // Directories have zero actual size but can have compressed size
  1132. if (entry.IsDirectory)
  1133. {
  1134. if (size > 0)
  1135. {
  1136. throw new ZipException("Directory cannot have size");
  1137. }
  1138. // There may be other cases where the compressed size can be greater than this?
  1139. // If so until details are known we will be strict.
  1140. if (entry.IsCrypted)
  1141. {
  1142. if (compressedSize > ZipConstants.CryptoHeaderSize + 2)
  1143. {
  1144. throw new ZipException("Directory compressed size invalid");
  1145. }
  1146. }
  1147. else if (compressedSize > 2)
  1148. {
  1149. // When not compressed the directory size can validly be 2 bytes
  1150. // if the true size wasnt known when data was originally being written.
  1151. // NOTE: Versions of the library 0.85.4 and earlier always added 2 bytes
  1152. throw new ZipException("Directory compressed size invalid");
  1153. }
  1154. }
  1155. if (!ZipNameTransform.IsValidName(localName, true))
  1156. {
  1157. throw new ZipException("Name is invalid");
  1158. }
  1159. }
  1160. // Tests that apply to both data and header.
  1161. // Size can be verified only if it is known in the local header.
  1162. // it will always be known in the central header.
  1163. if (((localFlags & (int)GeneralBitFlags.Descriptor) == 0) ||
  1164. ((size > 0) || (compressedSize > 0)))
  1165. {
  1166. if (size != entry.Size)
  1167. {
  1168. throw new ZipException(
  1169. string.Format("Size mismatch between central header({0}) and local header({1})",
  1170. entry.Size, size));
  1171. }
  1172. if (compressedSize != entry.CompressedSize &&
  1173. compressedSize != 0xFFFFFFFF && compressedSize != -1)
  1174. {
  1175. throw new ZipException(
  1176. string.Format("Compressed size mismatch between central header({0}) and local header({1})",
  1177. entry.CompressedSize, compressedSize));
  1178. }
  1179. }
  1180. int extraLength = storedNameLength + extraDataLength;
  1181. return offsetOfFirstEntry + entry.Offset + ZipConstants.LocalHeaderBaseSize + extraLength;
  1182. }
  1183. }
  1184. #endregion
  1185. #region Updating
  1186. const int DefaultBufferSize = 4096;
  1187. /// <summary>
  1188. /// The kind of update to apply.
  1189. /// </summary>
  1190. enum UpdateCommand
  1191. {
  1192. Copy, // Copy original file contents.
  1193. Modify, // Change encryption, compression, attributes, name, time etc, of an existing file.
  1194. Add, // Add a new file to the archive.
  1195. }
  1196. #region Properties
  1197. /// <summary>
  1198. /// Get / set the <see cref="INameTransform"/> to apply to names when updating.
  1199. /// </summary>
  1200. public INameTransform NameTransform
  1201. {
  1202. get
  1203. {
  1204. return updateEntryFactory_.NameTransform;
  1205. }
  1206. set
  1207. {
  1208. updateEntryFactory_.NameTransform = value;
  1209. }
  1210. }
  1211. /// <summary>
  1212. /// Get/set the <see cref="IEntryFactory"/> used to generate <see cref="ZipEntry"/> values
  1213. /// during updates.
  1214. /// </summary>
  1215. public IEntryFactory EntryFactory
  1216. {
  1217. get
  1218. {
  1219. return updateEntryFactory_;
  1220. }
  1221. set
  1222. {
  1223. if (value == null)
  1224. {
  1225. updateEntryFactory_ = new ZipEntryFactory();
  1226. }
  1227. else
  1228. {
  1229. updateEntryFactory_ = value;
  1230. }
  1231. }
  1232. }
  1233. /// <summary>
  1234. /// Get /set the buffer size to be used when updating this zip file.
  1235. /// </summary>
  1236. public int BufferSize
  1237. {
  1238. get { return bufferSize_; }
  1239. set
  1240. {
  1241. if (value < 1024)
  1242. {
  1243. #if NETCF_1_0
  1244. throw new ArgumentOutOfRangeException("value");
  1245. #else
  1246. throw new ArgumentOutOfRangeException("value", "cannot be below 1024");
  1247. #endif
  1248. }
  1249. if (bufferSize_ != value)
  1250. {
  1251. bufferSize_ = value;
  1252. copyBuffer_ = null;
  1253. }
  1254. }
  1255. }
  1256. /// <summary>
  1257. /// Get a value indicating an update has <see cref="BeginUpdate()">been started</see>.
  1258. /// </summary>
  1259. public bool IsUpdating
  1260. {
  1261. get { return updates_ != null; }
  1262. }
  1263. /// <summary>
  1264. /// Get / set a value indicating how Zip64 Extension usage is determined when adding entries.
  1265. /// </summary>
  1266. public UseZip64 UseZip64
  1267. {
  1268. get { return useZip64_; }
  1269. set { useZip64_ = value; }
  1270. }
  1271. #endregion
  1272. #region Immediate updating
  1273. // TBD: Direct form of updating
  1274. //
  1275. // public void Update(IEntryMatcher deleteMatcher)
  1276. // {
  1277. // }
  1278. //
  1279. // public void Update(IScanner addScanner)
  1280. // {
  1281. // }
  1282. #endregion
  1283. #region Deferred Updating
  1284. /// <summary>
  1285. /// Begin updating this <see cref="ZipFile"/> archive.
  1286. /// </summary>
  1287. /// <param name="archiveStorage">The <see cref="IArchiveStorage">archive storage</see> for use during the update.</param>
  1288. /// <param name="dataSource">The <see cref="IDynamicDataSource">data source</see> to utilise during updating.</param>
  1289. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1290. /// <exception cref="ArgumentNullException">One of the arguments provided is null</exception>
  1291. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1292. public void BeginUpdate(IArchiveStorage archiveStorage, IDynamicDataSource dataSource)
  1293. {
  1294. if (archiveStorage == null)
  1295. {
  1296. throw new ArgumentNullException("archiveStorage");
  1297. }
  1298. if (dataSource == null)
  1299. {
  1300. throw new ArgumentNullException("dataSource");
  1301. }
  1302. if (isDisposed_)
  1303. {
  1304. throw new ObjectDisposedException("ZipFile");
  1305. }
  1306. if (IsEmbeddedArchive)
  1307. {
  1308. throw new ZipException("Cannot update embedded/SFX archives");
  1309. }
  1310. archiveStorage_ = archiveStorage;
  1311. updateDataSource_ = dataSource;
  1312. // NOTE: the baseStream_ may not currently support writing or seeking.
  1313. updateIndex_ = new Hashtable();
  1314. updates_ = new ArrayList(entries_.Length);
  1315. foreach (ZipEntry entry in entries_)
  1316. {
  1317. int index = updates_.Add(new ZipUpdate(entry));
  1318. updateIndex_.Add(entry.Name, index);
  1319. }
  1320. // We must sort by offset before using offset's calculated sizes
  1321. updates_.Sort(new UpdateComparer());
  1322. int idx = 0;
  1323. foreach (ZipUpdate update in updates_)
  1324. {
  1325. //If last entry, there is no next entry offset to use
  1326. if (idx == updates_.Count - 1)
  1327. break;
  1328. update.OffsetBasedSize = ((ZipUpdate)updates_[idx + 1]).Entry.Offset - update.Entry.Offset;
  1329. idx++;
  1330. }
  1331. updateCount_ = updates_.Count;
  1332. contentsEdited_ = false;
  1333. commentEdited_ = false;
  1334. newComment_ = null;
  1335. }
  1336. /// <summary>
  1337. /// Begin updating to this <see cref="ZipFile"/> archive.
  1338. /// </summary>
  1339. /// <param name="archiveStorage">The storage to use during the update.</param>
  1340. public void BeginUpdate(IArchiveStorage archiveStorage)
  1341. {
  1342. BeginUpdate(archiveStorage, new DynamicDiskDataSource());
  1343. }
  1344. /// <summary>
  1345. /// Begin updating this <see cref="ZipFile"/> archive.
  1346. /// </summary>
  1347. /// <seealso cref="BeginUpdate(IArchiveStorage)"/>
  1348. /// <seealso cref="CommitUpdate"></seealso>
  1349. /// <seealso cref="AbortUpdate"></seealso>
  1350. public void BeginUpdate()
  1351. {
  1352. if (Name == null)
  1353. {
  1354. BeginUpdate(new MemoryArchiveStorage(), new DynamicDiskDataSource());
  1355. }
  1356. else
  1357. {
  1358. BeginUpdate(new DiskArchiveStorage(this), new DynamicDiskDataSource());
  1359. }
  1360. }
  1361. /// <summary>
  1362. /// Commit current updates, updating this archive.
  1363. /// </summary>
  1364. /// <seealso cref="BeginUpdate()"></seealso>
  1365. /// <seealso cref="AbortUpdate"></seealso>
  1366. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1367. public void CommitUpdate()
  1368. {
  1369. if (isDisposed_)
  1370. {
  1371. throw new ObjectDisposedException("ZipFile");
  1372. }
  1373. CheckUpdating();
  1374. try
  1375. {
  1376. updateIndex_.Clear();
  1377. updateIndex_ = null;
  1378. if (contentsEdited_)
  1379. {
  1380. RunUpdates();
  1381. }
  1382. else if (commentEdited_)
  1383. {
  1384. UpdateCommentOnly();
  1385. }
  1386. else
  1387. {
  1388. // Create an empty archive if none existed originally.
  1389. if (entries_.Length == 0)
  1390. {
  1391. byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_);
  1392. using (ZipHelperStream zhs = new ZipHelperStream(baseStream_))
  1393. {
  1394. zhs.WriteEndOfCentralDirectory(0, 0, 0, theComment);
  1395. }
  1396. }
  1397. }
  1398. }
  1399. finally
  1400. {
  1401. PostUpdateCleanup();
  1402. }
  1403. }
  1404. /// <summary>
  1405. /// Abort updating leaving the archive unchanged.
  1406. /// </summary>
  1407. /// <seealso cref="BeginUpdate()"></seealso>
  1408. /// <seealso cref="CommitUpdate"></seealso>
  1409. public void AbortUpdate()
  1410. {
  1411. PostUpdateCleanup();
  1412. }
  1413. /// <summary>
  1414. /// Set the file comment to be recorded when the current update is <see cref="CommitUpdate">commited</see>.
  1415. /// </summary>
  1416. /// <param name="comment">The comment to record.</param>
  1417. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1418. public void SetComment(string comment)
  1419. {
  1420. if (isDisposed_)
  1421. {
  1422. throw new ObjectDisposedException("ZipFile");
  1423. }
  1424. CheckUpdating();
  1425. newComment_ = new ZipString(comment);
  1426. if (newComment_.RawLength > 0xffff)
  1427. {
  1428. newComment_ = null;
  1429. throw new ZipException("Comment length exceeds maximum - 65535");
  1430. }
  1431. // We dont take account of the original and current comment appearing to be the same
  1432. // as encoding may be different.
  1433. commentEdited_ = true;
  1434. }
  1435. #endregion
  1436. #region Adding Entries
  1437. void AddUpdate(ZipUpdate update)
  1438. {
  1439. contentsEdited_ = true;
  1440. int index = FindExistingUpdate(update.Entry.Name);
  1441. if (index >= 0)
  1442. {
  1443. if (updates_[index] == null)
  1444. {
  1445. updateCount_ += 1;
  1446. }
  1447. // Direct replacement is faster than delete and add.
  1448. updates_[index] = update;
  1449. }
  1450. else
  1451. {
  1452. index = updates_.Add(update);
  1453. updateCount_ += 1;
  1454. updateIndex_.Add(update.Entry.Name, index);
  1455. }
  1456. }
  1457. /// <summary>
  1458. /// Add a new entry to the archive.
  1459. /// </summary>
  1460. /// <param name="fileName">The name of the file to add.</param>
  1461. /// <param name="compressionMethod">The compression method to use.</param>
  1462. /// <param name="useUnicodeText">Ensure Unicode text is used for name and comment for this entry.</param>
  1463. /// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
  1464. /// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
  1465. /// <exception cref="ArgumentOutOfRangeException">Compression method is not supported.</exception>
  1466. public void Add(string fileName, CompressionMethod compressionMethod, bool useUnicodeText)
  1467. {
  1468. if (fileName == null)
  1469. {
  1470. throw new ArgumentNullException("fileName");
  1471. }
  1472. if (isDisposed_)
  1473. {
  1474. throw new ObjectDisposedException("ZipFile");
  1475. }
  1476. if (!ZipEntry.IsCompressionMethodSupported(compressionMethod))
  1477. {
  1478. throw new ArgumentOutOfRangeException("compressionMethod");
  1479. }
  1480. CheckUpdating();
  1481. contentsEdited_ = true;
  1482. ZipEntry entry = EntryFactory.MakeFileEntry(fileName);
  1483. entry.IsUnicodeText = useUnicodeText;
  1484. entry.CompressionMethod = compressionMethod;
  1485. AddUpdate(new ZipUpdate(fileName, entry));
  1486. }
  1487. /// <summary>
  1488. /// Add a new entry to the archive.
  1489. /// </summary>
  1490. /// <param name="fileName">The name of the file to add.</param>
  1491. /// <param name="compressionMethod">The compression method to use.</param>
  1492. /// <exception cref="ArgumentNullException">ZipFile has been closed.</exception>
  1493. /// <exception cref="ArgumentOutOfRangeException">The compression method is not supported.</exception>
  1494. public void Add(string fileName, CompressionMethod compressionMethod)
  1495. {
  1496. if (fileName == null)
  1497. {
  1498. throw new ArgumentNullException("fileName");
  1499. }
  1500. if (!ZipEntry.IsCompressionMethodSupported(compressionMethod))
  1501. {
  1502. throw new ArgumentOutOfRangeException("compressionMethod");
  1503. }
  1504. CheckUpdating();
  1505. contentsEdited_ = true;
  1506. ZipEntry entry = EntryFactory.MakeFileEntry(fileName);
  1507. entry.CompressionMethod = compressionMethod;
  1508. AddUpdate(new ZipUpdate(fileName, entry));
  1509. }
  1510. /// <summary>
  1511. /// Add a file to the archive.
  1512. /// </summary>
  1513. /// <param name="fileName">The name of the file to add.</param>
  1514. /// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
  1515. public void Add(string fileName)
  1516. {
  1517. if (fileName == null)
  1518. {
  1519. throw new ArgumentNullException("fileName");
  1520. }
  1521. CheckUpdating();
  1522. AddUpdate(new ZipUpdate(fileName, EntryFactory.MakeFileEntry(fileName)));
  1523. }
  1524. /// <summary>
  1525. /// Add a file to the archive.
  1526. /// </summary>
  1527. /// <param name="fileName">The name of the file to add.</param>
  1528. /// <param name="entryName">The name to use for the <see cref="ZipEntry"/> on the Zip file created.</param>
  1529. /// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
  1530. public void Add(string fileName, string entryName)
  1531. {
  1532. if (fileName == null)
  1533. {
  1534. throw new ArgumentNullException("fileName");
  1535. }
  1536. if (entryName == null)
  1537. {
  1538. throw new ArgumentNullException("entryName");
  1539. }
  1540. CheckUpdating();
  1541. AddUpdate(new ZipUpdate(fileName, EntryFactory.MakeFileEntry(entryName)));
  1542. }
  1543. /// <summary>
  1544. /// Add a file entry with data.
  1545. /// </summary>
  1546. /// <param name="dataSource">The source of the data for this entry.</param>
  1547. /// <param name="entryName">The name to give to the entry.</param>
  1548. public void Add(IStaticDataSource dataSource, string entryName)
  1549. {
  1550. if (dataSource == null)
  1551. {
  1552. throw new ArgumentNullException("dataSource");
  1553. }
  1554. if (entryName == null)
  1555. {
  1556. throw new ArgumentNullException("entryName");
  1557. }
  1558. CheckUpdating();
  1559. AddUpdate(new ZipUpdate(dataSource, EntryFactory.MakeFileEntry(entryName, false)));
  1560. }
  1561. /// <summary>
  1562. /// Add a file entry with data.
  1563. /// </summary>
  1564. /// <param name="dataSource">The source of the data for this entry.</param>
  1565. /// <param name="entryName">The name to give to the entry.</param>
  1566. /// <param name="compressionMethod">The compression method to use.</param>
  1567. public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod)
  1568. {
  1569. if (dataSource == null)
  1570. {
  1571. throw new ArgumentNullException("dataSource");
  1572. }
  1573. if (entryName == null)
  1574. {
  1575. throw new ArgumentNullException("entryName");
  1576. }
  1577. CheckUpdating();
  1578. ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false);
  1579. entry.CompressionMethod = compressionMethod;
  1580. AddUpdate(new ZipUpdate(dataSource, entry));
  1581. }
  1582. /// <summary>
  1583. /// Add a file entry with data.
  1584. /// </summary>
  1585. /// <param name="dataSource">The source of the data for this entry.</param>
  1586. /// <param name="entryName">The name to give to the entry.</param>
  1587. /// <param name="compressionMethod">The compression method to use.</param>
  1588. /// <param name="useUnicodeText">Ensure Unicode text is used for name and comments for this entry.</param>
  1589. public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod, bool useUnicodeText)
  1590. {
  1591. if (dataSource == null)
  1592. {
  1593. throw new ArgumentNullException("dataSource");
  1594. }
  1595. if (entryName == null)
  1596. {
  1597. throw new ArgumentNullException("entryName");
  1598. }
  1599. CheckUpdating();
  1600. ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false);
  1601. entry.IsUnicodeText = useUnicodeText;
  1602. entry.CompressionMethod = compressionMethod;
  1603. AddUpdate(new ZipUpdate(dataSource, entry));
  1604. }
  1605. /// <summary>
  1606. /// Add a <see cref="ZipEntry"/> that contains no data.
  1607. /// </summary>
  1608. /// <param name="entry">The entry to add.</param>
  1609. /// <remarks>This can be used to add directories, volume labels, or empty file entries.</remarks>
  1610. public void Add(ZipEntry entry)
  1611. {
  1612. if (entry == null)
  1613. {
  1614. throw new ArgumentNullException("entry");
  1615. }
  1616. CheckUpdating();
  1617. if ((entry.Size != 0) || (entry.CompressedSize != 0))
  1618. {
  1619. throw new ZipException("Entry cannot have any data");
  1620. }
  1621. AddUpdate(new ZipUpdate(UpdateCommand.Add, entry));
  1622. }
  1623. /// <summary>
  1624. /// Add a directory entry to the archive.
  1625. /// </summary>
  1626. /// <param name="directoryName">The directory to add.</param>
  1627. public void AddDirectory(string directoryName)
  1628. {
  1629. if (directoryName == null)
  1630. {
  1631. throw new ArgumentNullException("directoryName");
  1632. }
  1633. CheckUpdating();
  1634. ZipEntry dirEntry = EntryFactory.MakeDirectoryEntry(directoryName);
  1635. AddUpdate(new ZipUpdate(UpdateCommand.Add, dirEntry));
  1636. }
  1637. #endregion
  1638. #region Modifying Entries
  1639. /* Modify not yet ready for public consumption.
  1640. Direct modification of an entry should not overwrite original data before its read.
  1641. Safe mode is trivial in this sense.
  1642. public void Modify(ZipEntry original, ZipEntry updated)
  1643. {
  1644. if ( original == null ) {
  1645. throw new ArgumentNullException("original");
  1646. }
  1647. if ( updated == null ) {
  1648. throw new ArgumentNullException("updated");
  1649. }
  1650. CheckUpdating();
  1651. contentsEdited_ = true;
  1652. updates_.Add(new ZipUpdate(original, updated));
  1653. }
  1654. */
  1655. #endregion
  1656. #region Deleting Entries
  1657. /// <summary>
  1658. /// Delete an entry by name
  1659. /// </summary>
  1660. /// <param name="fileName">The filename to delete</param>
  1661. /// <returns>True if the entry was found and deleted; false otherwise.</returns>
  1662. public bool Delete(string fileName)
  1663. {
  1664. if (fileName == null)
  1665. {
  1666. throw new ArgumentNullException("fileName");
  1667. }
  1668. CheckUpdating();
  1669. bool result = false;
  1670. int index = FindExistingUpdate(fileName);
  1671. if ((index >= 0) && (updates_[index] != null))
  1672. {
  1673. result = true;
  1674. contentsEdited_ = true;
  1675. updates_[index] = null;
  1676. updateCount_ -= 1;
  1677. }
  1678. else
  1679. {
  1680. throw new ZipException("Cannot find entry to delete");
  1681. }
  1682. return result;
  1683. }
  1684. /// <summary>
  1685. /// Delete a <see cref="ZipEntry"/> from the archive.
  1686. /// </summary>
  1687. /// <param name="entry">The entry to delete.</param>
  1688. public void Delete(ZipEntry entry)
  1689. {
  1690. if (entry == null)
  1691. {
  1692. throw new ArgumentNullException("entry");
  1693. }
  1694. CheckUpdating();
  1695. int index = FindExistingUpdate(entry);
  1696. if (index >= 0)
  1697. {
  1698. contentsEdited_ = true;
  1699. updates_[index] = null;
  1700. updateCount_ -= 1;
  1701. }
  1702. else
  1703. {
  1704. throw new ZipException("Cannot find entry to delete");
  1705. }
  1706. }
  1707. #endregion
  1708. #region Update Support
  1709. #region Writing Values/Headers
  1710. void WriteLEShort(int value)
  1711. {
  1712. baseStream_.WriteByte((byte)(value & 0xff));
  1713. baseStream_.WriteByte((byte)((value >> 8) & 0xff));
  1714. }
  1715. /// <summary>
  1716. /// Write an unsigned short in little endian byte order.
  1717. /// </summary>
  1718. void WriteLEUshort(ushort value)
  1719. {
  1720. baseStream_.WriteByte((byte)(value & 0xff));
  1721. baseStream_.WriteByte((byte)(value >> 8));
  1722. }
  1723. /// <summary>
  1724. /// Write an int in little endian byte order.
  1725. /// </summary>
  1726. void WriteLEInt(int value)
  1727. {
  1728. WriteLEShort(value & 0xffff);
  1729. WriteLEShort(value >> 16);
  1730. }
  1731. /// <summary>
  1732. /// Write an unsigned int in little endian byte order.
  1733. /// </summary>
  1734. void WriteLEUint(uint value)
  1735. {
  1736. WriteLEUshort((ushort)(value & 0xffff));
  1737. WriteLEUshort((ushort)(value >> 16));
  1738. }
  1739. /// <summary>
  1740. /// Write a long in little endian byte order.
  1741. /// </summary>
  1742. void WriteLeLong(long value)
  1743. {
  1744. WriteLEInt((int)(value & 0xffffffff));
  1745. WriteLEInt((int)(value >> 32));
  1746. }
  1747. void WriteLEUlong(ulong value)
  1748. {
  1749. WriteLEUint((uint)(value & 0xffffffff));
  1750. WriteLEUint((uint)(value >> 32));
  1751. }
  1752. void WriteLocalEntryHeader(ZipUpdate update)
  1753. {
  1754. ZipEntry entry = update.OutEntry;
  1755. // TODO: Local offset will require adjusting for multi-disk zip files.
  1756. entry.Offset = baseStream_.Position;
  1757. // TODO: Need to clear any entry flags that dont make sense or throw an exception here.
  1758. if (update.Command != UpdateCommand.Copy)
  1759. {
  1760. if (entry.CompressionMethod == CompressionMethod.Deflated)
  1761. {
  1762. if (entry.Size == 0)
  1763. {
  1764. // No need to compress - no data.
  1765. entry.CompressedSize = entry.Size;
  1766. entry.Crc = 0;
  1767. entry.CompressionMethod = CompressionMethod.Stored;
  1768. }
  1769. }
  1770. else if (entry.CompressionMethod == CompressionMethod.Stored)
  1771. {
  1772. entry.Flags &= ~(int)GeneralBitFlags.Descriptor;
  1773. }
  1774. if (HaveKeys)
  1775. {
  1776. entry.IsCrypted = true;
  1777. if (entry.Crc < 0)
  1778. {
  1779. entry.Flags |= (int)GeneralBitFlags.Descriptor;
  1780. }
  1781. }
  1782. else
  1783. {
  1784. entry.IsCrypted = false;
  1785. }
  1786. switch (useZip64_)
  1787. {
  1788. case UseZip64.Dynamic:
  1789. if (entry.Size < 0)
  1790. {
  1791. entry.ForceZip64();
  1792. }
  1793. break;
  1794. case UseZip64.On:
  1795. entry.ForceZip64();
  1796. break;
  1797. case UseZip64.Off:
  1798. // Do nothing. The entry itself may be using Zip64 independantly.
  1799. break;
  1800. }
  1801. }
  1802. // Write the local file header
  1803. WriteLEInt(ZipConstants.LocalHeaderSignature);
  1804. WriteLEShort(entry.Version);
  1805. WriteLEShort(entry.Flags);
  1806. WriteLEShort((byte)entry.CompressionMethod);
  1807. WriteLEInt((int)entry.DosTime);
  1808. if (!entry.HasCrc)
  1809. {
  1810. // Note patch address for updating CRC later.
  1811. update.CrcPatchOffset = baseStream_.Position;
  1812. WriteLEInt((int)0);
  1813. }
  1814. else
  1815. {
  1816. WriteLEInt(unchecked((int)entry.Crc));
  1817. }
  1818. if (entry.LocalHeaderRequiresZip64)
  1819. {
  1820. WriteLEInt(-1);
  1821. WriteLEInt(-1);
  1822. }
  1823. else
  1824. {
  1825. if ((entry.CompressedSize < 0) || (entry.Size < 0))
  1826. {
  1827. update.SizePatchOffset = baseStream_.Position;
  1828. }
  1829. WriteLEInt((int)entry.CompressedSize);
  1830. WriteLEInt((int)entry.Size);
  1831. }
  1832. byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
  1833. if (name.Length > 0xFFFF)
  1834. {
  1835. throw new ZipException("Entry name too long.");
  1836. }
  1837. ZipExtraData ed = new ZipExtraData(entry.ExtraData);
  1838. if (entry.LocalHeaderRequiresZip64)
  1839. {
  1840. ed.StartNewEntry();
  1841. // Local entry header always includes size and compressed size.
  1842. // NOTE the order of these fields is reversed when compared to the normal headers!
  1843. ed.AddLeLong(entry.Size);
  1844. ed.AddLeLong(entry.CompressedSize);
  1845. ed.AddNewEntry(1);
  1846. }
  1847. else
  1848. {
  1849. ed.Delete(1);
  1850. }
  1851. entry.ExtraData = ed.GetEntryData();
  1852. WriteLEShort(name.Length);
  1853. WriteLEShort(entry.ExtraData.Length);
  1854. if (name.Length > 0)
  1855. {
  1856. baseStream_.Write(name, 0, name.Length);
  1857. }
  1858. if (entry.LocalHeaderRequiresZip64)
  1859. {
  1860. if (!ed.Find(1))
  1861. {
  1862. throw new ZipException("Internal error cannot find extra data");
  1863. }
  1864. update.SizePatchOffset = baseStream_.Position + ed.CurrentReadIndex;
  1865. }
  1866. if (entry.ExtraData.Length > 0)
  1867. {
  1868. baseStream_.Write(entry.ExtraData, 0, entry.ExtraData.Length);
  1869. }
  1870. }
  1871. int WriteCentralDirectoryHeader(ZipEntry entry)
  1872. {
  1873. if (entry.CompressedSize < 0)
  1874. {
  1875. throw new ZipException("Attempt to write central directory entry with unknown csize");
  1876. }
  1877. if (entry.Size < 0)
  1878. {
  1879. throw new ZipException("Attempt to write central directory entry with unknown size");
  1880. }
  1881. if (entry.Crc < 0)
  1882. {
  1883. throw new ZipException("Attempt to write central directory entry with unknown crc");
  1884. }
  1885. // Write the central file header
  1886. WriteLEInt(ZipConstants.CentralHeaderSignature);
  1887. // Version made by
  1888. WriteLEShort(ZipConstants.VersionMadeBy);
  1889. // Version required to extract
  1890. WriteLEShort(entry.Version);
  1891. WriteLEShort(entry.Flags);
  1892. unchecked
  1893. {
  1894. WriteLEShort((byte)entry.CompressionMethod);
  1895. WriteLEInt((int)entry.DosTime);
  1896. WriteLEInt((int)entry.Crc);
  1897. }
  1898. if ((entry.IsZip64Forced()) || (entry.CompressedSize >= 0xffffffff))
  1899. {
  1900. WriteLEInt(-1);
  1901. }
  1902. else
  1903. {
  1904. WriteLEInt((int)(entry.CompressedSize & 0xffffffff));
  1905. }
  1906. if ((entry.IsZip64Forced()) || (entry.Size >= 0xffffffff))
  1907. {
  1908. WriteLEInt(-1);
  1909. }
  1910. else
  1911. {
  1912. WriteLEInt((int)entry.Size);
  1913. }
  1914. byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
  1915. if (name.Length > 0xFFFF)
  1916. {
  1917. throw new ZipException("Entry name is too long.");
  1918. }
  1919. WriteLEShort(name.Length);
  1920. // Central header extra data is different to local header version so regenerate.
  1921. ZipExtraData ed = new ZipExtraData(entry.ExtraData);
  1922. if (entry.CentralHeaderRequiresZip64)
  1923. {
  1924. ed.StartNewEntry();
  1925. if ((entry.Size >= 0xffffffff) || (useZip64_ == UseZip64.On))
  1926. {
  1927. ed.AddLeLong(entry.Size);
  1928. }
  1929. if ((entry.CompressedSize >= 0xffffffff) || (useZip64_ == UseZip64.On))
  1930. {
  1931. ed.AddLeLong(entry.CompressedSize);
  1932. }
  1933. if (entry.Offset >= 0xffffffff)
  1934. {
  1935. ed.AddLeLong(entry.Offset);
  1936. }
  1937. // Number of disk on which this file starts isnt supported and is never written here.
  1938. ed.AddNewEntry(1);
  1939. }
  1940. else
  1941. {
  1942. // Should have already be done when local header was added.
  1943. ed.Delete(1);
  1944. }
  1945. byte[] centralExtraData = ed.GetEntryData();
  1946. WriteLEShort(centralExtraData.Length);
  1947. WriteLEShort(entry.Comment != null ? entry.Comment.Length : 0);
  1948. WriteLEShort(0); // disk number
  1949. WriteLEShort(0); // internal file attributes
  1950. // External file attributes...
  1951. if (entry.ExternalFileAttributes != -1)
  1952. {
  1953. WriteLEInt(entry.ExternalFileAttributes);
  1954. }
  1955. else
  1956. {
  1957. if (entry.IsDirectory)
  1958. {
  1959. WriteLEUint(16);
  1960. }
  1961. else
  1962. {
  1963. WriteLEUint(0);
  1964. }
  1965. }
  1966. if (entry.Offset >= 0xffffffff)
  1967. {
  1968. WriteLEUint(0xffffffff);
  1969. }
  1970. else
  1971. {
  1972. WriteLEUint((uint)(int)entry.Offset);
  1973. }
  1974. if (name.Length > 0)
  1975. {
  1976. baseStream_.Write(name, 0, name.Length);
  1977. }
  1978. if (centralExtraData.Length > 0)
  1979. {
  1980. baseStream_.Write(centralExtraData, 0, centralExtraData.Length);
  1981. }
  1982. byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : new byte[0];
  1983. if (rawComment.Length > 0)
  1984. {
  1985. baseStream_.Write(rawComment, 0, rawComment.Length);
  1986. }
  1987. return ZipConstants.CentralHeaderBaseSize + name.Length + centralExtraData.Length + rawComment.Length;
  1988. }
  1989. #endregion
  1990. void PostUpdateCleanup()
  1991. {
  1992. updateDataSource_ = null;
  1993. updates_ = null;
  1994. updateIndex_ = null;
  1995. if (archiveStorage_ != null)
  1996. {
  1997. archiveStorage_.Dispose();
  1998. archiveStorage_ = null;
  1999. }
  2000. }
  2001. string GetTransformedFileName(string name)
  2002. {
  2003. INameTransform transform = NameTransform;
  2004. return (transform != null) ?
  2005. transform.TransformFile(name) :
  2006. name;
  2007. }
  2008. string GetTransformedDirectoryName(string name)
  2009. {
  2010. INameTransform transform = NameTransform;
  2011. return (transform != null) ?
  2012. transform.TransformDirectory(name) :
  2013. name;
  2014. }
  2015. /// <summary>
  2016. /// Get a raw memory buffer.
  2017. /// </summary>
  2018. /// <returns>Returns a raw memory buffer.</returns>
  2019. byte[] GetBuffer()
  2020. {
  2021. if (copyBuffer_ == null)
  2022. {
  2023. copyBuffer_ = new byte[bufferSize_];
  2024. }
  2025. return copyBuffer_;
  2026. }
  2027. void CopyDescriptorBytes(ZipUpdate update, Stream dest, Stream source)
  2028. {
  2029. int bytesToCopy = GetDescriptorSize(update);
  2030. if (bytesToCopy > 0)
  2031. {
  2032. byte[] buffer = GetBuffer();
  2033. while (bytesToCopy > 0)
  2034. {
  2035. int readSize = Math.Min(buffer.Length, bytesToCopy);
  2036. int bytesRead = source.Read(buffer, 0, readSize);
  2037. if (bytesRead > 0)
  2038. {
  2039. dest.Write(buffer, 0, bytesRead);
  2040. bytesToCopy -= bytesRead;
  2041. }
  2042. else
  2043. {
  2044. throw new ZipException("Unxpected end of stream");
  2045. }
  2046. }
  2047. }
  2048. }
  2049. void CopyBytes(ZipUpdate update, Stream destination, Stream source,
  2050. long bytesToCopy, bool updateCrc)
  2051. {
  2052. if (destination == source)
  2053. {
  2054. throw new InvalidOperationException("Destination and source are the same");
  2055. }
  2056. // NOTE: Compressed size is updated elsewhere.
  2057. Crc32 crc = new Crc32();
  2058. byte[] buffer = GetBuffer();
  2059. long targetBytes = bytesToCopy;
  2060. long totalBytesRead = 0;
  2061. int bytesRead;
  2062. do
  2063. {
  2064. int readSize = buffer.Length;
  2065. if (bytesToCopy < readSize)
  2066. {
  2067. readSize = (int)bytesToCopy;
  2068. }
  2069. bytesRead = source.Read(buffer, 0, readSize);
  2070. if (bytesRead > 0)
  2071. {
  2072. if (updateCrc)
  2073. {
  2074. crc.Update(buffer, 0, bytesRead);
  2075. }
  2076. destination.Write(buffer, 0, bytesRead);
  2077. bytesToCopy -= bytesRead;
  2078. totalBytesRead += bytesRead;
  2079. }
  2080. }
  2081. while ((bytesRead > 0) && (bytesToCopy > 0));
  2082. if (totalBytesRead != targetBytes)
  2083. {
  2084. throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", targetBytes, totalBytesRead));
  2085. }
  2086. if (updateCrc)
  2087. {
  2088. update.OutEntry.Crc = crc.Value;
  2089. }
  2090. }
  2091. /// <summary>
  2092. /// Get the size of the source descriptor for a <see cref="ZipUpdate"/>.
  2093. /// </summary>
  2094. /// <param name="update">The update to get the size for.</param>
  2095. /// <returns>The descriptor size, zero if there isnt one.</returns>
  2096. int GetDescriptorSize(ZipUpdate update)
  2097. {
  2098. int result = 0;
  2099. if ((update.Entry.Flags & (int)GeneralBitFlags.Descriptor) != 0)
  2100. {
  2101. result = ZipConstants.DataDescriptorSize - 4;
  2102. if (update.Entry.LocalHeaderRequiresZip64)
  2103. {
  2104. result = ZipConstants.Zip64DataDescriptorSize - 4;
  2105. }
  2106. }
  2107. return result;
  2108. }
  2109. void CopyDescriptorBytesDirect(ZipUpdate update, Stream stream, ref long destinationPosition, long sourcePosition)
  2110. {
  2111. int bytesToCopy = GetDescriptorSize(update);
  2112. while (bytesToCopy > 0)
  2113. {
  2114. int readSize = (int)bytesToCopy;
  2115. byte[] buffer = GetBuffer();
  2116. stream.Position = sourcePosition;
  2117. int bytesRead = stream.Read(buffer, 0, readSize);
  2118. if (bytesRead > 0)
  2119. {
  2120. stream.Position = destinationPosition;
  2121. stream.Write(buffer, 0, bytesRead);
  2122. bytesToCopy -= bytesRead;
  2123. destinationPosition += bytesRead;
  2124. sourcePosition += bytesRead;
  2125. }
  2126. else
  2127. {
  2128. throw new ZipException("Unxpected end of stream");
  2129. }
  2130. }
  2131. }
  2132. void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref long destinationPosition, ref long sourcePosition)
  2133. {
  2134. long bytesToCopy = update.Entry.CompressedSize;
  2135. // NOTE: Compressed size is updated elsewhere.
  2136. Crc32 crc = new Crc32();
  2137. byte[] buffer = GetBuffer();
  2138. long targetBytes = bytesToCopy;
  2139. long totalBytesRead = 0;
  2140. int bytesRead;
  2141. do
  2142. {
  2143. int readSize = buffer.Length;
  2144. if (bytesToCopy < readSize)
  2145. {
  2146. readSize = (int)bytesToCopy;
  2147. }
  2148. stream.Position = sourcePosition;
  2149. bytesRead = stream.Read(buffer, 0, readSize);
  2150. if (bytesRead > 0)
  2151. {
  2152. if (updateCrc)
  2153. {
  2154. crc.Update(buffer, 0, bytesRead);
  2155. }
  2156. stream.Position = destinationPosition;
  2157. stream.Write(buffer, 0, bytesRead);
  2158. destinationPosition += bytesRead;
  2159. sourcePosition += bytesRead;
  2160. bytesToCopy -= bytesRead;
  2161. totalBytesRead += bytesRead;
  2162. }
  2163. }
  2164. while ((bytesRead > 0) && (bytesToCopy > 0));
  2165. if (totalBytesRead != targetBytes)
  2166. {
  2167. throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", targetBytes, totalBytesRead));
  2168. }
  2169. if (updateCrc)
  2170. {
  2171. update.OutEntry.Crc = crc.Value;
  2172. }
  2173. }
  2174. int FindExistingUpdate(ZipEntry entry)
  2175. {
  2176. int result = -1;
  2177. string convertedName = GetTransformedFileName(entry.Name);
  2178. if (updateIndex_.ContainsKey(convertedName))
  2179. {
  2180. result = (int)updateIndex_[convertedName];
  2181. }
  2182. /*
  2183. // This is slow like the coming of the next ice age but takes less storage and may be useful
  2184. // for CF?
  2185. for (int index = 0; index < updates_.Count; ++index)
  2186. {
  2187. ZipUpdate zu = ( ZipUpdate )updates_[index];
  2188. if ( (zu.Entry.ZipFileIndex == entry.ZipFileIndex) &&
  2189. (string.Compare(convertedName, zu.Entry.Name, true, CultureInfo.InvariantCulture) == 0) ) {
  2190. result = index;
  2191. break;
  2192. }
  2193. }
  2194. */
  2195. return result;
  2196. }
  2197. int FindExistingUpdate(string fileName)
  2198. {
  2199. int result = -1;
  2200. string convertedName = GetTransformedFileName(fileName);
  2201. if (updateIndex_.ContainsKey(convertedName))
  2202. {
  2203. result = (int)updateIndex_[convertedName];
  2204. }
  2205. /*
  2206. // This is slow like the coming of the next ice age but takes less storage and may be useful
  2207. // for CF?
  2208. for ( int index = 0; index < updates_.Count; ++index ) {
  2209. if ( string.Compare(convertedName, (( ZipUpdate )updates_[index]).Entry.Name,
  2210. true, CultureInfo.InvariantCulture) == 0 ) {
  2211. result = index;
  2212. break;
  2213. }
  2214. }
  2215. */
  2216. return result;
  2217. }
  2218. /// <summary>
  2219. /// Get an output stream for the specified <see cref="ZipEntry"/>
  2220. /// </summary>
  2221. /// <param name="entry">The entry to get an output stream for.</param>
  2222. /// <returns>The output stream obtained for the entry.</returns>
  2223. Stream GetOutputStream(ZipEntry entry)
  2224. {
  2225. Stream result = baseStream_;
  2226. if (entry.IsCrypted == true)
  2227. {
  2228. #if NETCF_1_0
  2229. throw new ZipException("Encryption not supported for Compact Framework 1.0");
  2230. #else
  2231. result = CreateAndInitEncryptionStream(result, entry);
  2232. #endif
  2233. }
  2234. switch (entry.CompressionMethod)
  2235. {
  2236. case CompressionMethod.Stored:
  2237. result = new UncompressedStream(result);
  2238. break;
  2239. case CompressionMethod.Deflated:
  2240. DeflaterOutputStream dos = new DeflaterOutputStream(result, new Deflater(9, true));
  2241. dos.IsStreamOwner = false;
  2242. result = dos;
  2243. break;
  2244. default:
  2245. throw new ZipException("Unknown compression method " + entry.CompressionMethod);
  2246. }
  2247. return result;
  2248. }
  2249. void AddEntry(ZipFile workFile, ZipUpdate update)
  2250. {
  2251. Stream source = null;
  2252. if (update.Entry.IsFile)
  2253. {
  2254. source = update.GetSource();
  2255. if (source == null)
  2256. {
  2257. source = updateDataSource_.GetSource(update.Entry, update.Filename);
  2258. }
  2259. }
  2260. if (source != null)
  2261. {
  2262. using (source)
  2263. {
  2264. long sourceStreamLength = source.Length;
  2265. if (update.OutEntry.Size < 0)
  2266. {
  2267. update.OutEntry.Size = sourceStreamLength;
  2268. }
  2269. else
  2270. {
  2271. // Check for errant entries.
  2272. if (update.OutEntry.Size != sourceStreamLength)
  2273. {
  2274. throw new ZipException("Entry size/stream size mismatch");
  2275. }
  2276. }
  2277. workFile.WriteLocalEntryHeader(update);
  2278. long dataStart = workFile.baseStream_.Position;
  2279. using (Stream output = workFile.GetOutputStream(update.OutEntry))
  2280. {
  2281. CopyBytes(update, output, source, sourceStreamLength, true);
  2282. }
  2283. long dataEnd = workFile.baseStream_.Position;
  2284. update.OutEntry.CompressedSize = dataEnd - dataStart;
  2285. if ((update.OutEntry.Flags & (int)GeneralBitFlags.Descriptor) == (int)GeneralBitFlags.Descriptor)
  2286. {
  2287. ZipHelperStream helper = new ZipHelperStream(workFile.baseStream_);
  2288. helper.WriteDataDescriptor(update.OutEntry);
  2289. }
  2290. }
  2291. }
  2292. else
  2293. {
  2294. workFile.WriteLocalEntryHeader(update);
  2295. update.OutEntry.CompressedSize = 0;
  2296. }
  2297. }
  2298. void ModifyEntry(ZipFile workFile, ZipUpdate update)
  2299. {
  2300. workFile.WriteLocalEntryHeader(update);
  2301. long dataStart = workFile.baseStream_.Position;
  2302. // TODO: This is slow if the changes don't effect the data!!
  2303. if (update.Entry.IsFile && (update.Filename != null))
  2304. {
  2305. using (Stream output = workFile.GetOutputStream(update.OutEntry))
  2306. {
  2307. using (Stream source = this.GetInputStream(update.Entry))
  2308. {
  2309. CopyBytes(update, output, source, source.Length, true);
  2310. }
  2311. }
  2312. }
  2313. long dataEnd = workFile.baseStream_.Position;
  2314. update.Entry.CompressedSize = dataEnd - dataStart;
  2315. }
  2316. void CopyEntryDirect(ZipFile workFile, ZipUpdate update, ref long destinationPosition)
  2317. {
  2318. bool skipOver = false;
  2319. if (update.Entry.Offset == destinationPosition)
  2320. {
  2321. skipOver = true;
  2322. }
  2323. if (!skipOver)
  2324. {
  2325. baseStream_.Position = destinationPosition;
  2326. workFile.WriteLocalEntryHeader(update);
  2327. destinationPosition = baseStream_.Position;
  2328. }
  2329. long sourcePosition = 0;
  2330. const int NameLengthOffset = 26;
  2331. // TODO: Add base for SFX friendly handling
  2332. long entryDataOffset = update.Entry.Offset + NameLengthOffset;
  2333. baseStream_.Seek(entryDataOffset, SeekOrigin.Begin);
  2334. // Clumsy way of handling retrieving the original name and extra data length for now.
  2335. // TODO: Stop re-reading name and data length in CopyEntryDirect.
  2336. uint nameLength = ReadLEUshort();
  2337. uint extraLength = ReadLEUshort();
  2338. sourcePosition = baseStream_.Position + nameLength + extraLength;
  2339. if (skipOver)
  2340. {
  2341. if (update.OffsetBasedSize != -1)
  2342. destinationPosition += update.OffsetBasedSize;
  2343. else
  2344. // TODO: Find out why this calculation comes up 4 bytes short on some entries in ODT (Office Document Text) archives.
  2345. // WinZip produces a warning on these entries:
  2346. // "caution: value of lrec.csize (compressed size) changed from ..."
  2347. destinationPosition +=
  2348. (sourcePosition - entryDataOffset) + NameLengthOffset + // Header size
  2349. update.Entry.CompressedSize + GetDescriptorSize(update);
  2350. }
  2351. else
  2352. {
  2353. if (update.Entry.CompressedSize > 0)
  2354. {
  2355. CopyEntryDataDirect(update, baseStream_, false, ref destinationPosition, ref sourcePosition);
  2356. }
  2357. CopyDescriptorBytesDirect(update, baseStream_, ref destinationPosition, sourcePosition);
  2358. }
  2359. }
  2360. void CopyEntry(ZipFile workFile, ZipUpdate update)
  2361. {
  2362. workFile.WriteLocalEntryHeader(update);
  2363. if (update.Entry.CompressedSize > 0)
  2364. {
  2365. const int NameLengthOffset = 26;
  2366. long entryDataOffset = update.Entry.Offset + NameLengthOffset;
  2367. // TODO: This wont work for SFX files!
  2368. baseStream_.Seek(entryDataOffset, SeekOrigin.Begin);
  2369. uint nameLength = ReadLEUshort();
  2370. uint extraLength = ReadLEUshort();
  2371. baseStream_.Seek(nameLength + extraLength, SeekOrigin.Current);
  2372. CopyBytes(update, workFile.baseStream_, baseStream_, update.Entry.CompressedSize, false);
  2373. }
  2374. CopyDescriptorBytes(update, workFile.baseStream_, baseStream_);
  2375. }
  2376. void Reopen(Stream source)
  2377. {
  2378. if (source == null)
  2379. {
  2380. throw new ZipException("Failed to reopen archive - no source");
  2381. }
  2382. isNewArchive_ = false;
  2383. baseStream_ = source;
  2384. ReadEntries();
  2385. }
  2386. void Reopen()
  2387. {
  2388. if (Name == null)
  2389. {
  2390. throw new InvalidOperationException("Name is not known cannot Reopen");
  2391. }
  2392. Reopen(File.Open(Name, FileMode.Open, FileAccess.Read, FileShare.Read));
  2393. }
  2394. void UpdateCommentOnly()
  2395. {
  2396. long baseLength = baseStream_.Length;
  2397. ZipHelperStream updateFile = null;
  2398. if (archiveStorage_.UpdateMode == FileUpdateMode.Safe)
  2399. {
  2400. Stream copyStream = archiveStorage_.MakeTemporaryCopy(baseStream_);
  2401. updateFile = new ZipHelperStream(copyStream);
  2402. updateFile.IsStreamOwner = true;
  2403. baseStream_.Close();
  2404. baseStream_ = null;
  2405. }
  2406. else
  2407. {
  2408. if (archiveStorage_.UpdateMode == FileUpdateMode.Direct)
  2409. {
  2410. // TODO: archiveStorage wasnt originally intended for this use.
  2411. // Need to revisit this to tidy up handling as archive storage currently doesnt
  2412. // handle the original stream well.
  2413. // The problem is when using an existing zip archive with an in memory archive storage.
  2414. // The open stream wont support writing but the memory storage should open the same file not an in memory one.
  2415. // Need to tidy up the archive storage interface and contract basically.
  2416. baseStream_ = archiveStorage_.OpenForDirectUpdate(baseStream_);
  2417. updateFile = new ZipHelperStream(baseStream_);
  2418. }
  2419. else
  2420. {
  2421. baseStream_.Close();
  2422. baseStream_ = null;
  2423. updateFile = new ZipHelperStream(Name);
  2424. }
  2425. }
  2426. using (updateFile)
  2427. {
  2428. long locatedCentralDirOffset =
  2429. updateFile.LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature,
  2430. baseLength, ZipConstants.EndOfCentralRecordBaseSize, 0xffff);
  2431. if (locatedCentralDirOffset < 0)
  2432. {
  2433. throw new ZipException("Cannot find central directory");
  2434. }
  2435. const int CentralHeaderCommentSizeOffset = 16;
  2436. updateFile.Position += CentralHeaderCommentSizeOffset;
  2437. byte[] rawComment = newComment_.RawComment;
  2438. updateFile.WriteLEShort(rawComment.Length);
  2439. updateFile.Write(rawComment, 0, rawComment.Length);
  2440. updateFile.SetLength(updateFile.Position);
  2441. }
  2442. if (archiveStorage_.UpdateMode == FileUpdateMode.Safe)
  2443. {
  2444. Reopen(archiveStorage_.ConvertTemporaryToFinal());
  2445. }
  2446. else
  2447. {
  2448. ReadEntries();
  2449. }
  2450. }
  2451. /// <summary>
  2452. /// Class used to sort updates.
  2453. /// </summary>
  2454. class UpdateComparer : IComparer
  2455. {
  2456. /// <summary>
  2457. /// Compares two objects and returns a value indicating whether one is
  2458. /// less than, equal to or greater than the other.
  2459. /// </summary>
  2460. /// <param name="x">First object to compare</param>
  2461. /// <param name="y">Second object to compare.</param>
  2462. /// <returns>Compare result.</returns>
  2463. public int Compare(
  2464. object x,
  2465. object y)
  2466. {
  2467. ZipUpdate zx = x as ZipUpdate;
  2468. ZipUpdate zy = y as ZipUpdate;
  2469. int result;
  2470. if (zx == null)
  2471. {
  2472. if (zy == null)
  2473. {
  2474. result = 0;
  2475. }
  2476. else
  2477. {
  2478. result = -1;
  2479. }
  2480. }
  2481. else if (zy == null)
  2482. {
  2483. result = 1;
  2484. }
  2485. else
  2486. {
  2487. int xCmdValue = ((zx.Command == UpdateCommand.Copy) || (zx.Command == UpdateCommand.Modify)) ? 0 : 1;
  2488. int yCmdValue = ((zy.Command == UpdateCommand.Copy) || (zy.Command == UpdateCommand.Modify)) ? 0 : 1;
  2489. result = xCmdValue - yCmdValue;
  2490. if (result == 0)
  2491. {
  2492. long offsetDiff = zx.Entry.Offset - zy.Entry.Offset;
  2493. if (offsetDiff < 0)
  2494. {
  2495. result = -1;
  2496. }
  2497. else if (offsetDiff == 0)
  2498. {
  2499. result = 0;
  2500. }
  2501. else
  2502. {
  2503. result = 1;
  2504. }
  2505. }
  2506. }
  2507. return result;
  2508. }
  2509. }
  2510. void RunUpdates()
  2511. {
  2512. long sizeEntries = 0;
  2513. long endOfStream = 0;
  2514. bool directUpdate = false;
  2515. long destinationPosition = 0; // NOT SFX friendly
  2516. ZipFile workFile;
  2517. if (IsNewArchive)
  2518. {
  2519. workFile = this;
  2520. workFile.baseStream_.Position = 0;
  2521. directUpdate = true;
  2522. }
  2523. else if (archiveStorage_.UpdateMode == FileUpdateMode.Direct)
  2524. {
  2525. workFile = this;
  2526. workFile.baseStream_.Position = 0;
  2527. directUpdate = true;
  2528. // Sort the updates by offset within copies/modifies, then adds.
  2529. // This ensures that data required by copies will not be overwritten.
  2530. updates_.Sort(new UpdateComparer());
  2531. }
  2532. else
  2533. {
  2534. workFile = ZipFile.Create(archiveStorage_.GetTemporaryOutput());
  2535. workFile.UseZip64 = UseZip64;
  2536. if (key != null)
  2537. {
  2538. workFile.key = (byte[])key.Clone();
  2539. }
  2540. }
  2541. try
  2542. {
  2543. foreach (ZipUpdate update in updates_)
  2544. {
  2545. if (update != null)
  2546. {
  2547. switch (update.Command)
  2548. {
  2549. case UpdateCommand.Copy:
  2550. if (directUpdate)
  2551. {
  2552. CopyEntryDirect(workFile, update, ref destinationPosition);
  2553. }
  2554. else
  2555. {
  2556. CopyEntry(workFile, update);
  2557. }
  2558. break;
  2559. case UpdateCommand.Modify:
  2560. // TODO: Direct modifying of an entry will take some legwork.
  2561. ModifyEntry(workFile, update);
  2562. break;
  2563. case UpdateCommand.Add:
  2564. if (!IsNewArchive && directUpdate)
  2565. {
  2566. workFile.baseStream_.Position = destinationPosition;
  2567. }
  2568. AddEntry(workFile, update);
  2569. if (directUpdate)
  2570. {
  2571. destinationPosition = workFile.baseStream_.Position;
  2572. }
  2573. break;
  2574. }
  2575. }
  2576. }
  2577. if (!IsNewArchive && directUpdate)
  2578. {
  2579. workFile.baseStream_.Position = destinationPosition;
  2580. }
  2581. long centralDirOffset = workFile.baseStream_.Position;
  2582. foreach (ZipUpdate update in updates_)
  2583. {
  2584. if (update != null)
  2585. {
  2586. sizeEntries += workFile.WriteCentralDirectoryHeader(update.OutEntry);
  2587. }
  2588. }
  2589. byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_);
  2590. using (ZipHelperStream zhs = new ZipHelperStream(workFile.baseStream_))
  2591. {
  2592. zhs.WriteEndOfCentralDirectory(updateCount_, sizeEntries, centralDirOffset, theComment);
  2593. }
  2594. endOfStream = workFile.baseStream_.Position;
  2595. // And now patch entries...
  2596. foreach (ZipUpdate update in updates_)
  2597. {
  2598. if (update != null)
  2599. {
  2600. // If the size of the entry is zero leave the crc as 0 as well.
  2601. // The calculated crc will be all bits on...
  2602. if ((update.CrcPatchOffset > 0) && (update.OutEntry.CompressedSize > 0))
  2603. {
  2604. workFile.baseStream_.Position = update.CrcPatchOffset;
  2605. workFile.WriteLEInt((int)update.OutEntry.Crc);
  2606. }
  2607. if (update.SizePatchOffset > 0)
  2608. {
  2609. workFile.baseStream_.Position = update.SizePatchOffset;
  2610. if (update.OutEntry.LocalHeaderRequiresZip64)
  2611. {
  2612. workFile.WriteLeLong(update.OutEntry.Size);
  2613. workFile.WriteLeLong(update.OutEntry.CompressedSize);
  2614. }
  2615. else
  2616. {
  2617. workFile.WriteLEInt((int)update.OutEntry.CompressedSize);
  2618. workFile.WriteLEInt((int)update.OutEntry.Size);
  2619. }
  2620. }
  2621. }
  2622. }
  2623. }
  2624. catch
  2625. {
  2626. workFile.Close();
  2627. if (!directUpdate && (workFile.Name != null))
  2628. {
  2629. File.Delete(workFile.Name);
  2630. }
  2631. throw;
  2632. }
  2633. if (directUpdate)
  2634. {
  2635. workFile.baseStream_.SetLength(endOfStream);
  2636. workFile.baseStream_.Flush();
  2637. isNewArchive_ = false;
  2638. ReadEntries();
  2639. }
  2640. else
  2641. {
  2642. baseStream_.Close();
  2643. Reopen(archiveStorage_.ConvertTemporaryToFinal());
  2644. }
  2645. }
  2646. void CheckUpdating()
  2647. {
  2648. if (updates_ == null)
  2649. {
  2650. throw new InvalidOperationException("BeginUpdate has not been called");
  2651. }
  2652. }
  2653. #endregion
  2654. #region ZipUpdate class
  2655. /// <summary>
  2656. /// Represents a pending update to a Zip file.
  2657. /// </summary>
  2658. class ZipUpdate
  2659. {
  2660. #region Constructors
  2661. public ZipUpdate(string fileName, ZipEntry entry)
  2662. {
  2663. command_ = UpdateCommand.Add;
  2664. entry_ = entry;
  2665. filename_ = fileName;
  2666. }
  2667. [Obsolete]
  2668. public ZipUpdate(string fileName, string entryName, CompressionMethod compressionMethod)
  2669. {
  2670. command_ = UpdateCommand.Add;
  2671. entry_ = new ZipEntry(entryName);
  2672. entry_.CompressionMethod = compressionMethod;
  2673. filename_ = fileName;
  2674. }
  2675. [Obsolete]
  2676. public ZipUpdate(string fileName, string entryName)
  2677. : this(fileName, entryName, CompressionMethod.Deflated)
  2678. {
  2679. // Do nothing.
  2680. }
  2681. [Obsolete]
  2682. public ZipUpdate(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod)
  2683. {
  2684. command_ = UpdateCommand.Add;
  2685. entry_ = new ZipEntry(entryName);
  2686. entry_.CompressionMethod = compressionMethod;
  2687. dataSource_ = dataSource;
  2688. }
  2689. public ZipUpdate(IStaticDataSource dataSource, ZipEntry entry)
  2690. {
  2691. command_ = UpdateCommand.Add;
  2692. entry_ = entry;
  2693. dataSource_ = dataSource;
  2694. }
  2695. public ZipUpdate(ZipEntry original, ZipEntry updated)
  2696. {
  2697. throw new ZipException("Modify not currently supported");
  2698. /*
  2699. command_ = UpdateCommand.Modify;
  2700. entry_ = ( ZipEntry )original.Clone();
  2701. outEntry_ = ( ZipEntry )updated.Clone();
  2702. */
  2703. }
  2704. public ZipUpdate(UpdateCommand command, ZipEntry entry)
  2705. {
  2706. command_ = command;
  2707. entry_ = (ZipEntry)entry.Clone();
  2708. }
  2709. /// <summary>
  2710. /// Copy an existing entry.
  2711. /// </summary>
  2712. /// <param name="entry">The existing entry to copy.</param>
  2713. public ZipUpdate(ZipEntry entry)
  2714. : this(UpdateCommand.Copy, entry)
  2715. {
  2716. // Do nothing.
  2717. }
  2718. #endregion
  2719. /// <summary>
  2720. /// Get the <see cref="ZipEntry"/> for this update.
  2721. /// </summary>
  2722. /// <remarks>This is the source or original entry.</remarks>
  2723. public ZipEntry Entry
  2724. {
  2725. get { return entry_; }
  2726. }
  2727. /// <summary>
  2728. /// Get the <see cref="ZipEntry"/> that will be written to the updated/new file.
  2729. /// </summary>
  2730. public ZipEntry OutEntry
  2731. {
  2732. get
  2733. {
  2734. if (outEntry_ == null)
  2735. {
  2736. outEntry_ = (ZipEntry)entry_.Clone();
  2737. }
  2738. return outEntry_;
  2739. }
  2740. }
  2741. /// <summary>
  2742. /// Get the command for this update.
  2743. /// </summary>
  2744. public UpdateCommand Command
  2745. {
  2746. get { return command_; }
  2747. }
  2748. /// <summary>
  2749. /// Get the filename if any for this update. Null if none exists.
  2750. /// </summary>
  2751. public string Filename
  2752. {
  2753. get { return filename_; }
  2754. }
  2755. /// <summary>
  2756. /// Get/set the location of the size patch for this update.
  2757. /// </summary>
  2758. public long SizePatchOffset
  2759. {
  2760. get { return sizePatchOffset_; }
  2761. set { sizePatchOffset_ = value; }
  2762. }
  2763. /// <summary>
  2764. /// Get /set the location of the crc patch for this update.
  2765. /// </summary>
  2766. public long CrcPatchOffset
  2767. {
  2768. get { return crcPatchOffset_; }
  2769. set { crcPatchOffset_ = value; }
  2770. }
  2771. /// <summary>
  2772. /// Get/set the size calculated by offset.
  2773. /// Specifically, the difference between this and next entry's starting offset.
  2774. /// </summary>
  2775. public long OffsetBasedSize
  2776. {
  2777. get { return _offsetBasedSize; }
  2778. set { _offsetBasedSize = value; }
  2779. }
  2780. public Stream GetSource()
  2781. {
  2782. Stream result = null;
  2783. if (dataSource_ != null)
  2784. {
  2785. result = dataSource_.GetSource();
  2786. }
  2787. return result;
  2788. }
  2789. #region Instance Fields
  2790. ZipEntry entry_;
  2791. ZipEntry outEntry_;
  2792. UpdateCommand command_;
  2793. IStaticDataSource dataSource_;
  2794. string filename_;
  2795. long sizePatchOffset_ = -1;
  2796. long crcPatchOffset_ = -1;
  2797. long _offsetBasedSize = -1;
  2798. #endregion
  2799. }
  2800. #endregion
  2801. #endregion
  2802. #region Disposing
  2803. #region IDisposable Members
  2804. void IDisposable.Dispose()
  2805. {
  2806. Close();
  2807. }
  2808. #endregion
  2809. void DisposeInternal(bool disposing)
  2810. {
  2811. if (!isDisposed_)
  2812. {
  2813. isDisposed_ = true;
  2814. entries_ = new ZipEntry[0];
  2815. if (IsStreamOwner && (baseStream_ != null))
  2816. {
  2817. lock (baseStream_)
  2818. {
  2819. baseStream_.Close();
  2820. }
  2821. }
  2822. PostUpdateCleanup();
  2823. }
  2824. }
  2825. /// <summary>
  2826. /// Releases the unmanaged resources used by the this instance and optionally releases the managed resources.
  2827. /// </summary>
  2828. /// <param name="disposing">true to release both managed and unmanaged resources;
  2829. /// false to release only unmanaged resources.</param>
  2830. protected virtual void Dispose(bool disposing)
  2831. {
  2832. DisposeInternal(disposing);
  2833. }
  2834. #endregion
  2835. #region Internal routines
  2836. #region Reading
  2837. /// <summary>
  2838. /// Read an unsigned short in little endian byte order.
  2839. /// </summary>
  2840. /// <returns>Returns the value read.</returns>
  2841. /// <exception cref="EndOfStreamException">
  2842. /// The stream ends prematurely
  2843. /// </exception>
  2844. ushort ReadLEUshort()
  2845. {
  2846. int data1 = baseStream_.ReadByte();
  2847. if (data1 < 0)
  2848. {
  2849. throw new EndOfStreamException("End of stream");
  2850. }
  2851. int data2 = baseStream_.ReadByte();
  2852. if (data2 < 0)
  2853. {
  2854. throw new EndOfStreamException("End of stream");
  2855. }
  2856. return unchecked((ushort)((ushort)data1 | (ushort)(data2 << 8)));
  2857. }
  2858. /// <summary>
  2859. /// Read a uint in little endian byte order.
  2860. /// </summary>
  2861. /// <returns>Returns the value read.</returns>
  2862. /// <exception cref="IOException">
  2863. /// An i/o error occurs.
  2864. /// </exception>
  2865. /// <exception cref="System.IO.EndOfStreamException">
  2866. /// The file ends prematurely
  2867. /// </exception>
  2868. uint ReadLEUint()
  2869. {
  2870. return (uint)(ReadLEUshort() | (ReadLEUshort() << 16));
  2871. }
  2872. ulong ReadLEUlong()
  2873. {
  2874. return ReadLEUint() | ((ulong)ReadLEUint() << 32);
  2875. }
  2876. #endregion
  2877. // NOTE this returns the offset of the first byte after the signature.
  2878. long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData)
  2879. {
  2880. using (ZipHelperStream les = new ZipHelperStream(baseStream_))
  2881. {
  2882. return les.LocateBlockWithSignature(signature, endLocation, minimumBlockSize, maximumVariableData);
  2883. }
  2884. }
  2885. /// <summary>
  2886. /// Search for and read the central directory of a zip file filling the entries array.
  2887. /// </summary>
  2888. /// <exception cref="System.IO.IOException">
  2889. /// An i/o error occurs.
  2890. /// </exception>
  2891. /// <exception cref="Externals.Compression.Zip.ZipException">
  2892. /// The central directory is malformed or cannot be found
  2893. /// </exception>
  2894. void ReadEntries()
  2895. {
  2896. // Search for the End Of Central Directory. When a zip comment is
  2897. // present the directory will start earlier
  2898. //
  2899. // The search is limited to 64K which is the maximum size of a trailing comment field to aid speed.
  2900. // This should be compatible with both SFX and ZIP files but has only been tested for Zip files
  2901. // If a SFX file has the Zip data attached as a resource and there are other resources occuring later then
  2902. // this could be invalid.
  2903. // Could also speed this up by reading memory in larger blocks.
  2904. if (baseStream_.CanSeek == false)
  2905. {
  2906. throw new ZipException("ZipFile stream must be seekable");
  2907. }
  2908. long locatedEndOfCentralDir = LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature,
  2909. baseStream_.Length, ZipConstants.EndOfCentralRecordBaseSize, 0xffff);
  2910. if (locatedEndOfCentralDir < 0)
  2911. {
  2912. throw new ZipException("Cannot find central directory");
  2913. }
  2914. // Read end of central directory record
  2915. ushort thisDiskNumber = ReadLEUshort();
  2916. ushort startCentralDirDisk = ReadLEUshort();
  2917. ulong entriesForThisDisk = ReadLEUshort();
  2918. ulong entriesForWholeCentralDir = ReadLEUshort();
  2919. ulong centralDirSize = ReadLEUint();
  2920. long offsetOfCentralDir = ReadLEUint();
  2921. uint commentSize = ReadLEUshort();
  2922. if (commentSize > 0)
  2923. {
  2924. byte[] comment = new byte[commentSize];
  2925. StreamUtils.ReadFully(baseStream_, comment);
  2926. comment_ = ZipConstants.ConvertToString(comment);
  2927. }
  2928. else
  2929. {
  2930. comment_ = string.Empty;
  2931. }
  2932. bool isZip64 = false;
  2933. // Check if zip64 header information is required.
  2934. if ((thisDiskNumber == 0xffff) ||
  2935. (startCentralDirDisk == 0xffff) ||
  2936. (entriesForThisDisk == 0xffff) ||
  2937. (entriesForWholeCentralDir == 0xffff) ||
  2938. (centralDirSize == 0xffffffff) ||
  2939. (offsetOfCentralDir == 0xffffffff))
  2940. {
  2941. isZip64 = true;
  2942. long offset = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000);
  2943. if (offset < 0)
  2944. {
  2945. throw new ZipException("Cannot find Zip64 locator");
  2946. }
  2947. // number of the disk with the start of the zip64 end of central directory 4 bytes
  2948. // relative offset of the zip64 end of central directory record 8 bytes
  2949. // total number of disks 4 bytes
  2950. ReadLEUint(); // startDisk64 is not currently used
  2951. ulong offset64 = ReadLEUlong();
  2952. uint totalDisks = ReadLEUint();
  2953. baseStream_.Position = (long)offset64;
  2954. long sig64 = ReadLEUint();
  2955. if (sig64 != ZipConstants.Zip64CentralFileHeaderSignature)
  2956. {
  2957. throw new ZipException(string.Format("Invalid Zip64 Central directory signature at {0:X}", offset64));
  2958. }
  2959. // NOTE: Record size = SizeOfFixedFields + SizeOfVariableData - 12.
  2960. ulong recordSize = ReadLEUlong();
  2961. int versionMadeBy = ReadLEUshort();
  2962. int versionToExtract = ReadLEUshort();
  2963. uint thisDisk = ReadLEUint();
  2964. uint centralDirDisk = ReadLEUint();
  2965. entriesForThisDisk = ReadLEUlong();
  2966. entriesForWholeCentralDir = ReadLEUlong();
  2967. centralDirSize = ReadLEUlong();
  2968. offsetOfCentralDir = (long)ReadLEUlong();
  2969. // NOTE: zip64 extensible data sector (variable size) is ignored.
  2970. }
  2971. entries_ = new ZipEntry[entriesForThisDisk];
  2972. // SFX/embedded support, find the offset of the first entry vis the start of the stream
  2973. // This applies to Zip files that are appended to the end of an SFX stub.
  2974. // Or are appended as a resource to an executable.
  2975. // Zip files created by some archivers have the offsets altered to reflect the true offsets
  2976. // and so dont require any adjustment here...
  2977. // TODO: Difficulty with Zip64 and SFX offset handling needs resolution - maths?
  2978. if (!isZip64 && (offsetOfCentralDir < locatedEndOfCentralDir - (4 + (long)centralDirSize)))
  2979. {
  2980. offsetOfFirstEntry = locatedEndOfCentralDir - (4 + (long)centralDirSize + offsetOfCentralDir);
  2981. if (offsetOfFirstEntry <= 0)
  2982. {
  2983. throw new ZipException("Invalid embedded zip archive");
  2984. }
  2985. }
  2986. baseStream_.Seek(offsetOfFirstEntry + offsetOfCentralDir, SeekOrigin.Begin);
  2987. for (ulong i = 0; i < entriesForThisDisk; i++)
  2988. {
  2989. if (ReadLEUint() != ZipConstants.CentralHeaderSignature)
  2990. {
  2991. throw new ZipException("Wrong Central Directory signature");
  2992. }
  2993. int versionMadeBy = ReadLEUshort();
  2994. int versionToExtract = ReadLEUshort();
  2995. int bitFlags = ReadLEUshort();
  2996. int method = ReadLEUshort();
  2997. uint dostime = ReadLEUint();
  2998. uint crc = ReadLEUint();
  2999. long csize = (long)ReadLEUint();
  3000. long size = (long)ReadLEUint();
  3001. int nameLen = ReadLEUshort();
  3002. int extraLen = ReadLEUshort();
  3003. int commentLen = ReadLEUshort();
  3004. int diskStartNo = ReadLEUshort(); // Not currently used
  3005. int internalAttributes = ReadLEUshort(); // Not currently used
  3006. uint externalAttributes = ReadLEUint();
  3007. long offset = ReadLEUint();
  3008. byte[] buffer = new byte[Math.Max(nameLen, commentLen)];
  3009. StreamUtils.ReadFully(baseStream_, buffer, 0, nameLen);
  3010. string name = ZipConstants.ConvertToStringExt(bitFlags, buffer, nameLen);
  3011. ZipEntry entry = new ZipEntry(name, versionToExtract, versionMadeBy, (CompressionMethod)method);
  3012. entry.Crc = crc & 0xffffffffL;
  3013. entry.Size = size & 0xffffffffL;
  3014. entry.CompressedSize = csize & 0xffffffffL;
  3015. entry.Flags = bitFlags;
  3016. entry.DosTime = (uint)dostime;
  3017. entry.ZipFileIndex = (long)i;
  3018. entry.Offset = offset;
  3019. entry.ExternalFileAttributes = (int)externalAttributes;
  3020. if ((bitFlags & 8) == 0)
  3021. {
  3022. entry.CryptoCheckValue = (byte)(crc >> 24);
  3023. }
  3024. else
  3025. {
  3026. entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
  3027. }
  3028. if (extraLen > 0)
  3029. {
  3030. byte[] extra = new byte[extraLen];
  3031. StreamUtils.ReadFully(baseStream_, extra);
  3032. entry.ExtraData = extra;
  3033. }
  3034. entry.ProcessExtraData(false);
  3035. if (commentLen > 0)
  3036. {
  3037. StreamUtils.ReadFully(baseStream_, buffer, 0, commentLen);
  3038. entry.Comment = ZipConstants.ConvertToStringExt(bitFlags, buffer, commentLen);
  3039. }
  3040. entries_[i] = entry;
  3041. }
  3042. }
  3043. /// <summary>
  3044. /// Locate the data for a given entry.
  3045. /// </summary>
  3046. /// <returns>
  3047. /// The start offset of the data.
  3048. /// </returns>
  3049. /// <exception cref="System.IO.EndOfStreamException">
  3050. /// The stream ends prematurely
  3051. /// </exception>
  3052. /// <exception cref="Externals.Compression.Zip.ZipException">
  3053. /// The local header signature is invalid, the entry and central header file name lengths are different
  3054. /// or the local and entry compression methods dont match
  3055. /// </exception>
  3056. long LocateEntry(ZipEntry entry)
  3057. {
  3058. return TestLocalHeader(entry, HeaderTest.Extract);
  3059. }
  3060. #if !NETCF_1_0
  3061. Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
  3062. {
  3063. CryptoStream result = null;
  3064. if ((entry.Version < ZipConstants.VersionStrongEncryption)
  3065. || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0)
  3066. {
  3067. PkzipClassicManaged classicManaged = new PkzipClassicManaged();
  3068. OnKeysRequired(entry.Name);
  3069. if (HaveKeys == false)
  3070. {
  3071. throw new ZipException("No password available for encrypted stream");
  3072. }
  3073. result = new CryptoStream(baseStream, classicManaged.CreateDecryptor(key, null), CryptoStreamMode.Read);
  3074. CheckClassicPassword(result, entry);
  3075. }
  3076. else
  3077. {
  3078. #if !NET_1_1 && !NETCF_2_0
  3079. if (entry.Version == ZipConstants.VERSION_AES)
  3080. {
  3081. //
  3082. OnKeysRequired(entry.Name);
  3083. if (HaveKeys == false)
  3084. {
  3085. throw new ZipException("No password available for AES encrypted stream");
  3086. }
  3087. int saltLen = entry.AESSaltLen;
  3088. byte[] saltBytes = new byte[saltLen];
  3089. int saltIn = baseStream.Read(saltBytes, 0, saltLen);
  3090. if (saltIn != saltLen)
  3091. throw new ZipException("AES Salt expected " + saltLen + " got " + saltIn);
  3092. //
  3093. byte[] pwdVerifyRead = new byte[2];
  3094. baseStream.Read(pwdVerifyRead, 0, 2);
  3095. int blockSize = entry.AESKeySize / 8; // bits to bytes
  3096. ZipAESTransform decryptor = new ZipAESTransform(rawPassword_, saltBytes, blockSize, false);
  3097. byte[] pwdVerifyCalc = decryptor.PwdVerifier;
  3098. if (pwdVerifyCalc[0] != pwdVerifyRead[0] || pwdVerifyCalc[1] != pwdVerifyRead[1])
  3099. throw new Exception("Invalid password for AES");
  3100. result = new ZipAESStream(baseStream, decryptor, CryptoStreamMode.Read);
  3101. }
  3102. else
  3103. #endif
  3104. {
  3105. throw new ZipException("Decryption method not supported");
  3106. }
  3107. }
  3108. return result;
  3109. }
  3110. Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
  3111. {
  3112. CryptoStream result = null;
  3113. if ((entry.Version < ZipConstants.VersionStrongEncryption)
  3114. || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0)
  3115. {
  3116. PkzipClassicManaged classicManaged = new PkzipClassicManaged();
  3117. OnKeysRequired(entry.Name);
  3118. if (HaveKeys == false)
  3119. {
  3120. throw new ZipException("No password available for encrypted stream");
  3121. }
  3122. // Closing a CryptoStream will close the base stream as well so wrap it in an UncompressedStream
  3123. // which doesnt do this.
  3124. result = new CryptoStream(new UncompressedStream(baseStream),
  3125. classicManaged.CreateEncryptor(key, null), CryptoStreamMode.Write);
  3126. if ((entry.Crc < 0) || (entry.Flags & 8) != 0)
  3127. {
  3128. WriteEncryptionHeader(result, entry.DosTime << 16);
  3129. }
  3130. else
  3131. {
  3132. WriteEncryptionHeader(result, entry.Crc);
  3133. }
  3134. }
  3135. return result;
  3136. }
  3137. static void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEntry entry)
  3138. {
  3139. byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
  3140. StreamUtils.ReadFully(classicCryptoStream, cryptbuffer);
  3141. if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue)
  3142. {
  3143. throw new ZipException("Invalid password");
  3144. }
  3145. }
  3146. #endif
  3147. static void WriteEncryptionHeader(Stream stream, long crcValue)
  3148. {
  3149. byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize];
  3150. Random rnd = new Random();
  3151. rnd.NextBytes(cryptBuffer);
  3152. cryptBuffer[11] = (byte)(crcValue >> 24);
  3153. stream.Write(cryptBuffer, 0, cryptBuffer.Length);
  3154. }
  3155. #endregion
  3156. #region Instance Fields
  3157. bool isDisposed_;
  3158. string name_;
  3159. string comment_;
  3160. string rawPassword_;
  3161. Stream baseStream_;
  3162. bool isStreamOwner;
  3163. long offsetOfFirstEntry;
  3164. ZipEntry[] entries_;
  3165. byte[] key;
  3166. bool isNewArchive_;
  3167. // Default is dynamic which is not backwards compatible and can cause problems
  3168. // with XP's built in compression which cant read Zip64 archives.
  3169. // However it does avoid the situation were a large file is added and cannot be completed correctly.
  3170. // Hint: Set always ZipEntry size before they are added to an archive and this setting isnt needed.
  3171. UseZip64 useZip64_ = UseZip64.Dynamic;
  3172. #region Zip Update Instance Fields
  3173. ArrayList updates_;
  3174. long updateCount_; // Count is managed manually as updates_ can contain nulls!
  3175. Hashtable updateIndex_;
  3176. IArchiveStorage archiveStorage_;
  3177. IDynamicDataSource updateDataSource_;
  3178. bool contentsEdited_;
  3179. int bufferSize_ = DefaultBufferSize;
  3180. byte[] copyBuffer_;
  3181. ZipString newComment_;
  3182. bool commentEdited_;
  3183. IEntryFactory updateEntryFactory_ = new ZipEntryFactory();
  3184. #endregion
  3185. #endregion
  3186. #region Support Classes
  3187. /// <summary>
  3188. /// Represents a string from a <see cref="ZipFile"/> which is stored as an array of bytes.
  3189. /// </summary>
  3190. class ZipString
  3191. {
  3192. #region Constructors
  3193. /// <summary>
  3194. /// Initialise a <see cref="ZipString"/> with a string.
  3195. /// </summary>
  3196. /// <param name="comment">The textual string form.</param>
  3197. public ZipString(string comment)
  3198. {
  3199. comment_ = comment;
  3200. isSourceString_ = true;
  3201. }
  3202. /// <summary>
  3203. /// Initialise a <see cref="ZipString"/> using a string in its binary 'raw' form.
  3204. /// </summary>
  3205. /// <param name="rawString"></param>
  3206. public ZipString(byte[] rawString)
  3207. {
  3208. rawComment_ = rawString;
  3209. }
  3210. #endregion
  3211. /// <summary>
  3212. /// Get a value indicating the original source of data for this instance.
  3213. /// True if the source was a string; false if the source was binary data.
  3214. /// </summary>
  3215. public bool IsSourceString
  3216. {
  3217. get { return isSourceString_; }
  3218. }
  3219. /// <summary>
  3220. /// Get the length of the comment when represented as raw bytes.
  3221. /// </summary>
  3222. public int RawLength
  3223. {
  3224. get
  3225. {
  3226. MakeBytesAvailable();
  3227. return rawComment_.Length;
  3228. }
  3229. }
  3230. /// <summary>
  3231. /// Get the comment in its 'raw' form as plain bytes.
  3232. /// </summary>
  3233. public byte[] RawComment
  3234. {
  3235. get
  3236. {
  3237. MakeBytesAvailable();
  3238. return (byte[])rawComment_.Clone();
  3239. }
  3240. }
  3241. /// <summary>
  3242. /// Reset the comment to its initial state.
  3243. /// </summary>
  3244. public void Reset()
  3245. {
  3246. if (isSourceString_)
  3247. {
  3248. rawComment_ = null;
  3249. }
  3250. else
  3251. {
  3252. comment_ = null;
  3253. }
  3254. }
  3255. void MakeTextAvailable()
  3256. {
  3257. if (comment_ == null)
  3258. {
  3259. comment_ = ZipConstants.ConvertToString(rawComment_);
  3260. }
  3261. }
  3262. void MakeBytesAvailable()
  3263. {
  3264. if (rawComment_ == null)
  3265. {
  3266. rawComment_ = ZipConstants.ConvertToArray(comment_);
  3267. }
  3268. }
  3269. /// <summary>
  3270. /// Implicit conversion of comment to a string.
  3271. /// </summary>
  3272. /// <param name="zipString">The <see cref="ZipString"/> to convert to a string.</param>
  3273. /// <returns>The textual equivalent for the input value.</returns>
  3274. static public implicit operator string(ZipString zipString)
  3275. {
  3276. zipString.MakeTextAvailable();
  3277. return zipString.comment_;
  3278. }
  3279. #region Instance Fields
  3280. string comment_;
  3281. byte[] rawComment_;
  3282. bool isSourceString_;
  3283. #endregion
  3284. }
  3285. /// <summary>
  3286. /// An <see cref="IEnumerator">enumerator</see> for <see cref="ZipEntry">Zip entries</see>
  3287. /// </summary>
  3288. class ZipEntryEnumerator : IEnumerator
  3289. {
  3290. #region Constructors
  3291. public ZipEntryEnumerator(ZipEntry[] entries)
  3292. {
  3293. array = entries;
  3294. }
  3295. #endregion
  3296. #region IEnumerator Members
  3297. public object Current
  3298. {
  3299. get
  3300. {
  3301. return array[index];
  3302. }
  3303. }
  3304. public void Reset()
  3305. {
  3306. index = -1;
  3307. }
  3308. public bool MoveNext()
  3309. {
  3310. return (++index < array.Length);
  3311. }
  3312. #endregion
  3313. #region Instance Fields
  3314. ZipEntry[] array;
  3315. int index = -1;
  3316. #endregion
  3317. }
  3318. /// <summary>
  3319. /// An <see cref="UncompressedStream"/> is a stream that you can write uncompressed data
  3320. /// to and flush, but cannot read, seek or do anything else to.
  3321. /// </summary>
  3322. class UncompressedStream : Stream
  3323. {
  3324. #region Constructors
  3325. public UncompressedStream(Stream baseStream)
  3326. {
  3327. baseStream_ = baseStream;
  3328. }
  3329. #endregion
  3330. /// <summary>
  3331. /// Close this stream instance.
  3332. /// </summary>
  3333. public override void Close()
  3334. {
  3335. // Do nothing
  3336. }
  3337. /// <summary>
  3338. /// Gets a value indicating whether the current stream supports reading.
  3339. /// </summary>
  3340. public override bool CanRead
  3341. {
  3342. get
  3343. {
  3344. return false;
  3345. }
  3346. }
  3347. /// <summary>
  3348. /// Write any buffered data to underlying storage.
  3349. /// </summary>
  3350. public override void Flush()
  3351. {
  3352. baseStream_.Flush();
  3353. }
  3354. /// <summary>
  3355. /// Gets a value indicating whether the current stream supports writing.
  3356. /// </summary>
  3357. public override bool CanWrite
  3358. {
  3359. get
  3360. {
  3361. return baseStream_.CanWrite;
  3362. }
  3363. }
  3364. /// <summary>
  3365. /// Gets a value indicating whether the current stream supports seeking.
  3366. /// </summary>
  3367. public override bool CanSeek
  3368. {
  3369. get
  3370. {
  3371. return false;
  3372. }
  3373. }
  3374. /// <summary>
  3375. /// Get the length in bytes of the stream.
  3376. /// </summary>
  3377. public override long Length
  3378. {
  3379. get
  3380. {
  3381. return 0;
  3382. }
  3383. }
  3384. /// <summary>
  3385. /// Gets or sets the position within the current stream.
  3386. /// </summary>
  3387. public override long Position
  3388. {
  3389. get
  3390. {
  3391. return baseStream_.Position;
  3392. }
  3393. set
  3394. {
  3395. }
  3396. }
  3397. /// <summary>
  3398. /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
  3399. /// </summary>
  3400. /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
  3401. /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
  3402. /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
  3403. /// <returns>
  3404. /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
  3405. /// </returns>
  3406. /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
  3407. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3408. /// <exception cref="T:System.NotSupportedException">The stream does not support reading. </exception>
  3409. /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
  3410. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3411. /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
  3412. public override int Read(byte[] buffer, int offset, int count)
  3413. {
  3414. return 0;
  3415. }
  3416. /// <summary>
  3417. /// Sets the position within the current stream.
  3418. /// </summary>
  3419. /// <param name="offset">A byte offset relative to the origin parameter.</param>
  3420. /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param>
  3421. /// <returns>
  3422. /// The new position within the current stream.
  3423. /// </returns>
  3424. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3425. /// <exception cref="T:System.NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
  3426. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3427. public override long Seek(long offset, SeekOrigin origin)
  3428. {
  3429. return 0;
  3430. }
  3431. /// <summary>
  3432. /// Sets the length of the current stream.
  3433. /// </summary>
  3434. /// <param name="value">The desired length of the current stream in bytes.</param>
  3435. /// <exception cref="T:System.NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
  3436. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3437. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3438. public override void SetLength(long value)
  3439. {
  3440. }
  3441. /// <summary>
  3442. /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
  3443. /// </summary>
  3444. /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
  3445. /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
  3446. /// <param name="count">The number of bytes to be written to the current stream.</param>
  3447. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3448. /// <exception cref="T:System.NotSupportedException">The stream does not support writing. </exception>
  3449. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3450. /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
  3451. /// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
  3452. /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
  3453. public override void Write(byte[] buffer, int offset, int count)
  3454. {
  3455. baseStream_.Write(buffer, offset, count);
  3456. }
  3457. #region Instance Fields
  3458. Stream baseStream_;
  3459. #endregion
  3460. }
  3461. /// <summary>
  3462. /// A <see cref="PartialInputStream"/> is an <see cref="InflaterInputStream"/>
  3463. /// whose data is only a part or subsection of a file.
  3464. /// </summary>
  3465. class PartialInputStream : Stream
  3466. {
  3467. #region Constructors
  3468. /// <summary>
  3469. /// Initialise a new instance of the <see cref="PartialInputStream"/> class.
  3470. /// </summary>
  3471. /// <param name="zipFile">The <see cref="ZipFile"/> containing the underlying stream to use for IO.</param>
  3472. /// <param name="start">The start of the partial data.</param>
  3473. /// <param name="length">The length of the partial data.</param>
  3474. public PartialInputStream(ZipFile zipFile, long start, long length)
  3475. {
  3476. start_ = start;
  3477. length_ = length;
  3478. // Although this is the only time the zipfile is used
  3479. // keeping a reference here prevents premature closure of
  3480. // this zip file and thus the baseStream_.
  3481. // Code like this will cause apparently random failures depending
  3482. // on the size of the files and when garbage is collected.
  3483. //
  3484. // ZipFile z = new ZipFile (stream);
  3485. // Stream reader = z.GetInputStream(0);
  3486. // uses reader here....
  3487. zipFile_ = zipFile;
  3488. baseStream_ = zipFile_.baseStream_;
  3489. readPos_ = start;
  3490. end_ = start + length;
  3491. }
  3492. #endregion
  3493. /// <summary>
  3494. /// Read a byte from this stream.
  3495. /// </summary>
  3496. /// <returns>Returns the byte read or -1 on end of stream.</returns>
  3497. public override int ReadByte()
  3498. {
  3499. if (readPos_ >= end_)
  3500. {
  3501. // -1 is the correct value at end of stream.
  3502. return -1;
  3503. }
  3504. lock (baseStream_)
  3505. {
  3506. baseStream_.Seek(readPos_++, SeekOrigin.Begin);
  3507. return baseStream_.ReadByte();
  3508. }
  3509. }
  3510. /// <summary>
  3511. /// Close this <see cref="PartialInputStream">partial input stream</see>.
  3512. /// </summary>
  3513. /// <remarks>
  3514. /// The underlying stream is not closed. Close the parent ZipFile class to do that.
  3515. /// </remarks>
  3516. public override void Close()
  3517. {
  3518. // Do nothing at all!
  3519. }
  3520. /// <summary>
  3521. /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
  3522. /// </summary>
  3523. /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
  3524. /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
  3525. /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
  3526. /// <returns>
  3527. /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
  3528. /// </returns>
  3529. /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
  3530. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3531. /// <exception cref="T:System.NotSupportedException">The stream does not support reading. </exception>
  3532. /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
  3533. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3534. /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
  3535. public override int Read(byte[] buffer, int offset, int count)
  3536. {
  3537. lock (baseStream_)
  3538. {
  3539. if (count > end_ - readPos_)
  3540. {
  3541. count = (int)(end_ - readPos_);
  3542. if (count == 0)
  3543. {
  3544. return 0;
  3545. }
  3546. }
  3547. baseStream_.Seek(readPos_, SeekOrigin.Begin);
  3548. int readCount = baseStream_.Read(buffer, offset, count);
  3549. if (readCount > 0)
  3550. {
  3551. readPos_ += readCount;
  3552. }
  3553. return readCount;
  3554. }
  3555. }
  3556. /// <summary>
  3557. /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
  3558. /// </summary>
  3559. /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
  3560. /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
  3561. /// <param name="count">The number of bytes to be written to the current stream.</param>
  3562. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3563. /// <exception cref="T:System.NotSupportedException">The stream does not support writing. </exception>
  3564. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3565. /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
  3566. /// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
  3567. /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
  3568. public override void Write(byte[] buffer, int offset, int count)
  3569. {
  3570. throw new NotSupportedException();
  3571. }
  3572. /// <summary>
  3573. /// When overridden in a derived class, sets the length of the current stream.
  3574. /// </summary>
  3575. /// <param name="value">The desired length of the current stream in bytes.</param>
  3576. /// <exception cref="T:System.NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
  3577. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3578. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3579. public override void SetLength(long value)
  3580. {
  3581. throw new NotSupportedException();
  3582. }
  3583. /// <summary>
  3584. /// When overridden in a derived class, sets the position within the current stream.
  3585. /// </summary>
  3586. /// <param name="offset">A byte offset relative to the origin parameter.</param>
  3587. /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param>
  3588. /// <returns>
  3589. /// The new position within the current stream.
  3590. /// </returns>
  3591. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3592. /// <exception cref="T:System.NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
  3593. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3594. public override long Seek(long offset, SeekOrigin origin)
  3595. {
  3596. long newPos = readPos_;
  3597. switch (origin)
  3598. {
  3599. case SeekOrigin.Begin:
  3600. newPos = start_ + offset;
  3601. break;
  3602. case SeekOrigin.Current:
  3603. newPos = readPos_ + offset;
  3604. break;
  3605. case SeekOrigin.End:
  3606. newPos = end_ + offset;
  3607. break;
  3608. }
  3609. if (newPos < start_)
  3610. {
  3611. throw new ArgumentException("Negative position is invalid");
  3612. }
  3613. if (newPos >= end_)
  3614. {
  3615. throw new IOException("Cannot seek past end");
  3616. }
  3617. readPos_ = newPos;
  3618. return readPos_;
  3619. }
  3620. /// <summary>
  3621. /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
  3622. /// </summary>
  3623. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3624. public override void Flush()
  3625. {
  3626. // Nothing to do.
  3627. }
  3628. /// <summary>
  3629. /// Gets or sets the position within the current stream.
  3630. /// </summary>
  3631. /// <value></value>
  3632. /// <returns>The current position within the stream.</returns>
  3633. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  3634. /// <exception cref="T:System.NotSupportedException">The stream does not support seeking. </exception>
  3635. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3636. public override long Position
  3637. {
  3638. get { return readPos_ - start_; }
  3639. set
  3640. {
  3641. long newPos = start_ + value;
  3642. if (newPos < start_)
  3643. {
  3644. throw new ArgumentException("Negative position is invalid");
  3645. }
  3646. if (newPos >= end_)
  3647. {
  3648. throw new InvalidOperationException("Cannot seek past end");
  3649. }
  3650. readPos_ = newPos;
  3651. }
  3652. }
  3653. /// <summary>
  3654. /// Gets the length in bytes of the stream.
  3655. /// </summary>
  3656. /// <value></value>
  3657. /// <returns>A long value representing the length of the stream in bytes.</returns>
  3658. /// <exception cref="T:System.NotSupportedException">A class derived from Stream does not support seeking. </exception>
  3659. /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
  3660. public override long Length
  3661. {
  3662. get { return length_; }
  3663. }
  3664. /// <summary>
  3665. /// Gets a value indicating whether the current stream supports writing.
  3666. /// </summary>
  3667. /// <value>false</value>
  3668. /// <returns>true if the stream supports writing; otherwise, false.</returns>
  3669. public override bool CanWrite
  3670. {
  3671. get { return false; }
  3672. }
  3673. /// <summary>
  3674. /// Gets a value indicating whether the current stream supports seeking.
  3675. /// </summary>
  3676. /// <value>true</value>
  3677. /// <returns>true if the stream supports seeking; otherwise, false.</returns>
  3678. public override bool CanSeek
  3679. {
  3680. get { return true; }
  3681. }
  3682. /// <summary>
  3683. /// Gets a value indicating whether the current stream supports reading.
  3684. /// </summary>
  3685. /// <value>true.</value>
  3686. /// <returns>true if the stream supports reading; otherwise, false.</returns>
  3687. public override bool CanRead
  3688. {
  3689. get { return true; }
  3690. }
  3691. #if !NET_1_0 && !NET_1_1 && !NETCF_1_0
  3692. /// <summary>
  3693. /// Gets a value that determines whether the current stream can time out.
  3694. /// </summary>
  3695. /// <value></value>
  3696. /// <returns>A value that determines whether the current stream can time out.</returns>
  3697. public override bool CanTimeout
  3698. {
  3699. get { return baseStream_.CanTimeout; }
  3700. }
  3701. #endif
  3702. #region Instance Fields
  3703. ZipFile zipFile_;
  3704. Stream baseStream_;
  3705. long start_;
  3706. long length_;
  3707. long readPos_;
  3708. long end_;
  3709. #endregion
  3710. }
  3711. #endregion
  3712. }
  3713. #endregion
  3714. #region DataSources
  3715. /// <summary>
  3716. /// Provides a static way to obtain a source of data for an entry.
  3717. /// </summary>
  3718. internal interface IStaticDataSource
  3719. {
  3720. /// <summary>
  3721. /// Get a source of data by creating a new stream.
  3722. /// </summary>
  3723. /// <returns>Returns a <see cref="Stream"/> to use for compression input.</returns>
  3724. /// <remarks>Ideally a new stream is created and opened to achieve this, to avoid locking problems.</remarks>
  3725. Stream GetSource();
  3726. }
  3727. /// <summary>
  3728. /// Represents a source of data that can dynamically provide
  3729. /// multiple <see cref="Stream">data sources</see> based on the parameters passed.
  3730. /// </summary>
  3731. internal interface IDynamicDataSource
  3732. {
  3733. /// <summary>
  3734. /// Get a data source.
  3735. /// </summary>
  3736. /// <param name="entry">The <see cref="ZipEntry"/> to get a source for.</param>
  3737. /// <param name="name">The name for data if known.</param>
  3738. /// <returns>Returns a <see cref="Stream"/> to use for compression input.</returns>
  3739. /// <remarks>Ideally a new stream is created and opened to achieve this, to avoid locking problems.</remarks>
  3740. Stream GetSource(ZipEntry entry, string name);
  3741. }
  3742. /// <summary>
  3743. /// Default implementation of a <see cref="IStaticDataSource"/> for use with files stored on disk.
  3744. /// </summary>
  3745. internal class StaticDiskDataSource : IStaticDataSource
  3746. {
  3747. /// <summary>
  3748. /// Initialise a new instnace of <see cref="StaticDiskDataSource"/>
  3749. /// </summary>
  3750. /// <param name="fileName">The name of the file to obtain data from.</param>
  3751. public StaticDiskDataSource(string fileName)
  3752. {
  3753. fileName_ = fileName;
  3754. }
  3755. #region IDataSource Members
  3756. /// <summary>
  3757. /// Get a <see cref="Stream"/> providing data.
  3758. /// </summary>
  3759. /// <returns>Returns a <see cref="Stream"/> provising data.</returns>
  3760. public Stream GetSource()
  3761. {
  3762. return File.Open(fileName_, FileMode.Open, FileAccess.Read, FileShare.Read);
  3763. }
  3764. #endregion
  3765. #region Instance Fields
  3766. string fileName_;
  3767. #endregion
  3768. }
  3769. /// <summary>
  3770. /// Default implementation of <see cref="IDynamicDataSource"/> for files stored on disk.
  3771. /// </summary>
  3772. internal class DynamicDiskDataSource : IDynamicDataSource
  3773. {
  3774. /// <summary>
  3775. /// Initialise a default instance of <see cref="DynamicDiskDataSource"/>.
  3776. /// </summary>
  3777. public DynamicDiskDataSource()
  3778. {
  3779. }
  3780. #region IDataSource Members
  3781. /// <summary>
  3782. /// Get a <see cref="Stream"/> providing data for an entry.
  3783. /// </summary>
  3784. /// <param name="entry">The entry to provide data for.</param>
  3785. /// <param name="name">The file name for data if known.</param>
  3786. /// <returns>Returns a stream providing data; or null if not available</returns>
  3787. public Stream GetSource(ZipEntry entry, string name)
  3788. {
  3789. Stream result = null;
  3790. if (name != null)
  3791. {
  3792. result = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
  3793. }
  3794. return result;
  3795. }
  3796. #endregion
  3797. }
  3798. #endregion
  3799. #region Archive Storage
  3800. /// <summary>
  3801. /// Defines facilities for data storage when updating Zip Archives.
  3802. /// </summary>
  3803. internal interface IArchiveStorage
  3804. {
  3805. /// <summary>
  3806. /// Get the <see cref="FileUpdateMode"/> to apply during updates.
  3807. /// </summary>
  3808. FileUpdateMode UpdateMode { get; }
  3809. /// <summary>
  3810. /// Get an empty <see cref="Stream"/> that can be used for temporary output.
  3811. /// </summary>
  3812. /// <returns>Returns a temporary output <see cref="Stream"/></returns>
  3813. /// <seealso cref="ConvertTemporaryToFinal"></seealso>
  3814. Stream GetTemporaryOutput();
  3815. /// <summary>
  3816. /// Convert a temporary output stream to a final stream.
  3817. /// </summary>
  3818. /// <returns>The resulting final <see cref="Stream"/></returns>
  3819. /// <seealso cref="GetTemporaryOutput"/>
  3820. Stream ConvertTemporaryToFinal();
  3821. /// <summary>
  3822. /// Make a temporary copy of the original stream.
  3823. /// </summary>
  3824. /// <param name="stream">The <see cref="Stream"/> to copy.</param>
  3825. /// <returns>Returns a temporary output <see cref="Stream"/> that is a copy of the input.</returns>
  3826. Stream MakeTemporaryCopy(Stream stream);
  3827. /// <summary>
  3828. /// Return a stream suitable for performing direct updates on the original source.
  3829. /// </summary>
  3830. /// <param name="stream">The current stream.</param>
  3831. /// <returns>Returns a stream suitable for direct updating.</returns>
  3832. /// <remarks>This may be the current stream passed.</remarks>
  3833. Stream OpenForDirectUpdate(Stream stream);
  3834. /// <summary>
  3835. /// Dispose of this instance.
  3836. /// </summary>
  3837. void Dispose();
  3838. }
  3839. /// <summary>
  3840. /// An abstract <see cref="IArchiveStorage"/> suitable for extension by inheritance.
  3841. /// </summary>
  3842. abstract internal class BaseArchiveStorage : IArchiveStorage
  3843. {
  3844. #region Constructors
  3845. /// <summary>
  3846. /// Initializes a new instance of the <see cref="BaseArchiveStorage"/> class.
  3847. /// </summary>
  3848. /// <param name="updateMode">The update mode.</param>
  3849. protected BaseArchiveStorage(FileUpdateMode updateMode)
  3850. {
  3851. updateMode_ = updateMode;
  3852. }
  3853. #endregion
  3854. #region IArchiveStorage Members
  3855. /// <summary>
  3856. /// Gets a temporary output <see cref="Stream"/>
  3857. /// </summary>
  3858. /// <returns>Returns the temporary output stream.</returns>
  3859. /// <seealso cref="ConvertTemporaryToFinal"></seealso>
  3860. public abstract Stream GetTemporaryOutput();
  3861. /// <summary>
  3862. /// Converts the temporary <see cref="Stream"/> to its final form.
  3863. /// </summary>
  3864. /// <returns>Returns a <see cref="Stream"/> that can be used to read
  3865. /// the final storage for the archive.</returns>
  3866. /// <seealso cref="GetTemporaryOutput"/>
  3867. public abstract Stream ConvertTemporaryToFinal();
  3868. /// <summary>
  3869. /// Make a temporary copy of a <see cref="Stream"/>.
  3870. /// </summary>
  3871. /// <param name="stream">The <see cref="Stream"/> to make a copy of.</param>
  3872. /// <returns>Returns a temporary output <see cref="Stream"/> that is a copy of the input.</returns>
  3873. public abstract Stream MakeTemporaryCopy(Stream stream);
  3874. /// <summary>
  3875. /// Return a stream suitable for performing direct updates on the original source.
  3876. /// </summary>
  3877. /// <param name="stream">The <see cref="Stream"/> to open for direct update.</param>
  3878. /// <returns>Returns a stream suitable for direct updating.</returns>
  3879. public abstract Stream OpenForDirectUpdate(Stream stream);
  3880. /// <summary>
  3881. /// Disposes this instance.
  3882. /// </summary>
  3883. public abstract void Dispose();
  3884. /// <summary>
  3885. /// Gets the update mode applicable.
  3886. /// </summary>
  3887. /// <value>The update mode.</value>
  3888. public FileUpdateMode UpdateMode
  3889. {
  3890. get
  3891. {
  3892. return updateMode_;
  3893. }
  3894. }
  3895. #endregion
  3896. #region Instance Fields
  3897. FileUpdateMode updateMode_;
  3898. #endregion
  3899. }
  3900. /// <summary>
  3901. /// An <see cref="IArchiveStorage"/> implementation suitable for hard disks.
  3902. /// </summary>
  3903. internal class DiskArchiveStorage : BaseArchiveStorage
  3904. {
  3905. #region Constructors
  3906. /// <summary>
  3907. /// Initializes a new instance of the <see cref="DiskArchiveStorage"/> class.
  3908. /// </summary>
  3909. /// <param name="file">The file.</param>
  3910. /// <param name="updateMode">The update mode.</param>
  3911. public DiskArchiveStorage(ZipFile file, FileUpdateMode updateMode)
  3912. : base(updateMode)
  3913. {
  3914. if (file.Name == null)
  3915. {
  3916. throw new ZipException("Cant handle non file archives");
  3917. }
  3918. fileName_ = file.Name;
  3919. }
  3920. /// <summary>
  3921. /// Initializes a new instance of the <see cref="DiskArchiveStorage"/> class.
  3922. /// </summary>
  3923. /// <param name="file">The file.</param>
  3924. public DiskArchiveStorage(ZipFile file)
  3925. : this(file, FileUpdateMode.Safe)
  3926. {
  3927. }
  3928. #endregion
  3929. #region IArchiveStorage Members
  3930. /// <summary>
  3931. /// Gets a temporary output <see cref="Stream"/> for performing updates on.
  3932. /// </summary>
  3933. /// <returns>Returns the temporary output stream.</returns>
  3934. public override Stream GetTemporaryOutput()
  3935. {
  3936. if (temporaryName_ != null)
  3937. {
  3938. temporaryName_ = GetTempFileName(temporaryName_, true);
  3939. temporaryStream_ = File.Open(temporaryName_, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  3940. }
  3941. else
  3942. {
  3943. // Determine where to place files based on internal strategy.
  3944. // Currently this is always done in system temp directory.
  3945. temporaryName_ = Path.GetTempFileName();
  3946. temporaryStream_ = File.Open(temporaryName_, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  3947. }
  3948. return temporaryStream_;
  3949. }
  3950. /// <summary>
  3951. /// Converts a temporary <see cref="Stream"/> to its final form.
  3952. /// </summary>
  3953. /// <returns>Returns a <see cref="Stream"/> that can be used to read
  3954. /// the final storage for the archive.</returns>
  3955. public override Stream ConvertTemporaryToFinal()
  3956. {
  3957. if (temporaryStream_ == null)
  3958. {
  3959. throw new ZipException("No temporary stream has been created");
  3960. }
  3961. Stream result = null;
  3962. string moveTempName = GetTempFileName(fileName_, false);
  3963. bool newFileCreated = false;
  3964. try
  3965. {
  3966. temporaryStream_.Close();
  3967. File.Move(fileName_, moveTempName);
  3968. File.Move(temporaryName_, fileName_);
  3969. newFileCreated = true;
  3970. File.Delete(moveTempName);
  3971. result = File.Open(fileName_, FileMode.Open, FileAccess.Read, FileShare.Read);
  3972. }
  3973. catch (Exception)
  3974. {
  3975. result = null;
  3976. // Try to roll back changes...
  3977. if (!newFileCreated)
  3978. {
  3979. File.Move(moveTempName, fileName_);
  3980. File.Delete(temporaryName_);
  3981. }
  3982. throw;
  3983. }
  3984. return result;
  3985. }
  3986. /// <summary>
  3987. /// Make a temporary copy of a stream.
  3988. /// </summary>
  3989. /// <param name="stream">The <see cref="Stream"/> to copy.</param>
  3990. /// <returns>Returns a temporary output <see cref="Stream"/> that is a copy of the input.</returns>
  3991. public override Stream MakeTemporaryCopy(Stream stream)
  3992. {
  3993. stream.Close();
  3994. temporaryName_ = GetTempFileName(fileName_, true);
  3995. File.Copy(fileName_, temporaryName_, true);
  3996. temporaryStream_ = new FileStream(temporaryName_,
  3997. FileMode.Open,
  3998. FileAccess.ReadWrite);
  3999. return temporaryStream_;
  4000. }
  4001. /// <summary>
  4002. /// Return a stream suitable for performing direct updates on the original source.
  4003. /// </summary>
  4004. /// <param name="stream">The current stream.</param>
  4005. /// <returns>Returns a stream suitable for direct updating.</returns>
  4006. /// <remarks>If the current stream is not null this is used as is.</remarks>
  4007. public override Stream OpenForDirectUpdate(Stream stream)
  4008. {
  4009. Stream result;
  4010. if ((stream == null) || !stream.CanWrite)
  4011. {
  4012. if (stream != null)
  4013. {
  4014. stream.Close();
  4015. }
  4016. result = new FileStream(fileName_,
  4017. FileMode.Open,
  4018. FileAccess.ReadWrite);
  4019. }
  4020. else
  4021. {
  4022. result = stream;
  4023. }
  4024. return result;
  4025. }
  4026. /// <summary>
  4027. /// Disposes this instance.
  4028. /// </summary>
  4029. public override void Dispose()
  4030. {
  4031. if (temporaryStream_ != null)
  4032. {
  4033. temporaryStream_.Close();
  4034. }
  4035. }
  4036. #endregion
  4037. #region Internal routines
  4038. static string GetTempFileName(string original, bool makeTempFile)
  4039. {
  4040. string result = null;
  4041. if (original == null)
  4042. {
  4043. result = Path.GetTempFileName();
  4044. }
  4045. else
  4046. {
  4047. int counter = 0;
  4048. int suffixSeed = DateTime.Now.Second;
  4049. while (result == null)
  4050. {
  4051. counter += 1;
  4052. string newName = string.Format("{0}.{1}{2}.tmp", original, suffixSeed, counter);
  4053. if (!File.Exists(newName))
  4054. {
  4055. if (makeTempFile)
  4056. {
  4057. try
  4058. {
  4059. // Try and create the file.
  4060. using (FileStream stream = File.Create(newName))
  4061. {
  4062. }
  4063. result = newName;
  4064. }
  4065. catch
  4066. {
  4067. suffixSeed = DateTime.Now.Second;
  4068. }
  4069. }
  4070. else
  4071. {
  4072. result = newName;
  4073. }
  4074. }
  4075. }
  4076. }
  4077. return result;
  4078. }
  4079. #endregion
  4080. #region Instance Fields
  4081. Stream temporaryStream_;
  4082. string fileName_;
  4083. string temporaryName_;
  4084. #endregion
  4085. }
  4086. /// <summary>
  4087. /// An <see cref="IArchiveStorage"/> implementation suitable for in memory streams.
  4088. /// </summary>
  4089. internal class MemoryArchiveStorage : BaseArchiveStorage
  4090. {
  4091. #region Constructors
  4092. /// <summary>
  4093. /// Initializes a new instance of the <see cref="MemoryArchiveStorage"/> class.
  4094. /// </summary>
  4095. public MemoryArchiveStorage()
  4096. : base(FileUpdateMode.Direct)
  4097. {
  4098. }
  4099. /// <summary>
  4100. /// Initializes a new instance of the <see cref="MemoryArchiveStorage"/> class.
  4101. /// </summary>
  4102. /// <param name="updateMode">The <see cref="FileUpdateMode"/> to use</param>
  4103. /// <remarks>This constructor is for testing as memory streams dont really require safe mode.</remarks>
  4104. public MemoryArchiveStorage(FileUpdateMode updateMode)
  4105. : base(updateMode)
  4106. {
  4107. }
  4108. #endregion
  4109. #region Properties
  4110. /// <summary>
  4111. /// Get the stream returned by <see cref="ConvertTemporaryToFinal"/> if this was in fact called.
  4112. /// </summary>
  4113. public MemoryStream FinalStream
  4114. {
  4115. get { return finalStream_; }
  4116. }
  4117. #endregion
  4118. #region IArchiveStorage Members
  4119. /// <summary>
  4120. /// Gets the temporary output <see cref="Stream"/>
  4121. /// </summary>
  4122. /// <returns>Returns the temporary output stream.</returns>
  4123. public override Stream GetTemporaryOutput()
  4124. {
  4125. temporaryStream_ = new MemoryStream();
  4126. return temporaryStream_;
  4127. }
  4128. /// <summary>
  4129. /// Converts the temporary <see cref="Stream"/> to its final form.
  4130. /// </summary>
  4131. /// <returns>Returns a <see cref="Stream"/> that can be used to read
  4132. /// the final storage for the archive.</returns>
  4133. public override Stream ConvertTemporaryToFinal()
  4134. {
  4135. if (temporaryStream_ == null)
  4136. {
  4137. throw new ZipException("No temporary stream has been created");
  4138. }
  4139. finalStream_ = new MemoryStream(temporaryStream_.ToArray());
  4140. return finalStream_;
  4141. }
  4142. /// <summary>
  4143. /// Make a temporary copy of the original stream.
  4144. /// </summary>
  4145. /// <param name="stream">The <see cref="Stream"/> to copy.</param>
  4146. /// <returns>Returns a temporary output <see cref="Stream"/> that is a copy of the input.</returns>
  4147. public override Stream MakeTemporaryCopy(Stream stream)
  4148. {
  4149. temporaryStream_ = new MemoryStream();
  4150. stream.Position = 0;
  4151. StreamUtils.Copy(stream, temporaryStream_, new byte[4096]);
  4152. return temporaryStream_;
  4153. }
  4154. /// <summary>
  4155. /// Return a stream suitable for performing direct updates on the original source.
  4156. /// </summary>
  4157. /// <param name="stream">The original source stream</param>
  4158. /// <returns>Returns a stream suitable for direct updating.</returns>
  4159. /// <remarks>If the <paramref name="stream"/> passed is not null this is used;
  4160. /// otherwise a new <see cref="MemoryStream"/> is returned.</remarks>
  4161. public override Stream OpenForDirectUpdate(Stream stream)
  4162. {
  4163. Stream result;
  4164. if ((stream == null) || !stream.CanWrite)
  4165. {
  4166. result = new MemoryStream();
  4167. if (stream != null)
  4168. {
  4169. stream.Position = 0;
  4170. StreamUtils.Copy(stream, result, new byte[4096]);
  4171. stream.Close();
  4172. }
  4173. }
  4174. else
  4175. {
  4176. result = stream;
  4177. }
  4178. return result;
  4179. }
  4180. /// <summary>
  4181. /// Disposes this instance.
  4182. /// </summary>
  4183. public override void Dispose()
  4184. {
  4185. if (temporaryStream_ != null)
  4186. {
  4187. temporaryStream_.Close();
  4188. }
  4189. }
  4190. #endregion
  4191. #region Instance Fields
  4192. MemoryStream temporaryStream_;
  4193. MemoryStream finalStream_;
  4194. #endregion
  4195. }
  4196. #endregion
  4197. }