TarArchive.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace ICSharpCode.SharpZipLib.Tar
  5. {
  6. /// <summary>
  7. /// Used to advise clients of 'events' while processing archives
  8. /// </summary>
  9. public delegate void ProgressMessageHandler(TarArchive archive, TarEntry entry, string message);
  10. /// <summary>
  11. /// The TarArchive class implements the concept of a
  12. /// 'Tape Archive'. A tar archive is a series of entries, each of
  13. /// which represents a file system object. Each entry in
  14. /// the archive consists of a header block followed by 0 or more data blocks.
  15. /// Directory entries consist only of the header block, and are followed by entries
  16. /// for the directory's contents. File entries consist of a
  17. /// header followed by the number of blocks needed to
  18. /// contain the file's contents. All entries are written on
  19. /// block boundaries. Blocks are 512 bytes long.
  20. ///
  21. /// TarArchives are instantiated in either read or write mode,
  22. /// based upon whether they are instantiated with an InputStream
  23. /// or an OutputStream. Once instantiated TarArchives read/write
  24. /// mode can not be changed.
  25. ///
  26. /// There is currently no support for random access to tar archives.
  27. /// However, it seems that subclassing TarArchive, and using the
  28. /// TarBuffer.CurrentRecord and TarBuffer.CurrentBlock
  29. /// properties, this would be rather trivial.
  30. /// </summary>
  31. public class TarArchive : IDisposable
  32. {
  33. /// <summary>
  34. /// Client hook allowing detailed information to be reported during processing
  35. /// </summary>
  36. public event ProgressMessageHandler ProgressMessageEvent;
  37. /// <summary>
  38. /// Raises the ProgressMessage event
  39. /// </summary>
  40. /// <param name="entry">The <see cref="TarEntry">TarEntry</see> for this event</param>
  41. /// <param name="message">message for this event. Null is no message</param>
  42. protected virtual void OnProgressMessageEvent(TarEntry entry, string message)
  43. {
  44. ProgressMessageHandler handler = ProgressMessageEvent;
  45. if (handler != null)
  46. {
  47. handler(this, entry, message);
  48. }
  49. }
  50. #region Constructors
  51. /// <summary>
  52. /// Constructor for a default <see cref="TarArchive"/>.
  53. /// </summary>
  54. protected TarArchive()
  55. {
  56. }
  57. /// <summary>
  58. /// Initalise a TarArchive for input.
  59. /// </summary>
  60. /// <param name="stream">The <see cref="TarInputStream"/> to use for input.</param>
  61. protected TarArchive(TarInputStream stream)
  62. {
  63. if (stream == null)
  64. {
  65. throw new ArgumentNullException(nameof(stream));
  66. }
  67. tarIn = stream;
  68. }
  69. /// <summary>
  70. /// Initialise a TarArchive for output.
  71. /// </summary>
  72. /// <param name="stream">The <see cref="TarOutputStream"/> to use for output.</param>
  73. protected TarArchive(TarOutputStream stream)
  74. {
  75. if (stream == null)
  76. {
  77. throw new ArgumentNullException(nameof(stream));
  78. }
  79. tarOut = stream;
  80. }
  81. #endregion Constructors
  82. #region Static factory methods
  83. /// <summary>
  84. /// The InputStream based constructors create a TarArchive for the
  85. /// purposes of extracting or listing a tar archive. Thus, use
  86. /// these constructors when you wish to extract files from or list
  87. /// the contents of an existing tar archive.
  88. /// </summary>
  89. /// <param name="inputStream">The stream to retrieve archive data from.</param>
  90. /// <returns>Returns a new <see cref="TarArchive"/> suitable for reading from.</returns>
  91. public static TarArchive CreateInputTarArchive(Stream inputStream)
  92. {
  93. if (inputStream == null)
  94. {
  95. throw new ArgumentNullException(nameof(inputStream));
  96. }
  97. var tarStream = inputStream as TarInputStream;
  98. TarArchive result;
  99. if (tarStream != null)
  100. {
  101. result = new TarArchive(tarStream);
  102. }
  103. else
  104. {
  105. result = CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor);
  106. }
  107. return result;
  108. }
  109. /// <summary>
  110. /// Create TarArchive for reading setting block factor
  111. /// </summary>
  112. /// <param name="inputStream">A stream containing the tar archive contents</param>
  113. /// <param name="blockFactor">The blocking factor to apply</param>
  114. /// <returns>Returns a <see cref="TarArchive"/> suitable for reading.</returns>
  115. public static TarArchive CreateInputTarArchive(Stream inputStream, int blockFactor)
  116. {
  117. if (inputStream == null)
  118. {
  119. throw new ArgumentNullException(nameof(inputStream));
  120. }
  121. if (inputStream is TarInputStream)
  122. {
  123. throw new ArgumentException("TarInputStream not valid");
  124. }
  125. return new TarArchive(new TarInputStream(inputStream, blockFactor));
  126. }
  127. /// <summary>
  128. /// Create a TarArchive for writing to, using the default blocking factor
  129. /// </summary>
  130. /// <param name="outputStream">The <see cref="Stream"/> to write to</param>
  131. /// <returns>Returns a <see cref="TarArchive"/> suitable for writing.</returns>
  132. public static TarArchive CreateOutputTarArchive(Stream outputStream)
  133. {
  134. if (outputStream == null)
  135. {
  136. throw new ArgumentNullException(nameof(outputStream));
  137. }
  138. var tarStream = outputStream as TarOutputStream;
  139. TarArchive result;
  140. if (tarStream != null)
  141. {
  142. result = new TarArchive(tarStream);
  143. }
  144. else
  145. {
  146. result = CreateOutputTarArchive(outputStream, TarBuffer.DefaultBlockFactor);
  147. }
  148. return result;
  149. }
  150. /// <summary>
  151. /// Create a <see cref="TarArchive">tar archive</see> for writing.
  152. /// </summary>
  153. /// <param name="outputStream">The stream to write to</param>
  154. /// <param name="blockFactor">The blocking factor to use for buffering.</param>
  155. /// <returns>Returns a <see cref="TarArchive"/> suitable for writing.</returns>
  156. public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockFactor)
  157. {
  158. if (outputStream == null)
  159. {
  160. throw new ArgumentNullException(nameof(outputStream));
  161. }
  162. if (outputStream is TarOutputStream)
  163. {
  164. throw new ArgumentException("TarOutputStream is not valid");
  165. }
  166. return new TarArchive(new TarOutputStream(outputStream, blockFactor));
  167. }
  168. #endregion Static factory methods
  169. /// <summary>
  170. /// Set the flag that determines whether existing files are
  171. /// kept, or overwritten during extraction.
  172. /// </summary>
  173. /// <param name="keepExistingFiles">
  174. /// If true, do not overwrite existing files.
  175. /// </param>
  176. public void SetKeepOldFiles(bool keepExistingFiles)
  177. {
  178. if (isDisposed)
  179. {
  180. throw new ObjectDisposedException("TarArchive");
  181. }
  182. keepOldFiles = keepExistingFiles;
  183. }
  184. /// <summary>
  185. /// Get/set the ascii file translation flag. If ascii file translation
  186. /// is true, then the file is checked to see if it a binary file or not.
  187. /// If the flag is true and the test indicates it is ascii text
  188. /// file, it will be translated. The translation converts the local
  189. /// operating system's concept of line ends into the UNIX line end,
  190. /// '\n', which is the defacto standard for a TAR archive. This makes
  191. /// text files compatible with UNIX.
  192. /// </summary>
  193. public bool AsciiTranslate
  194. {
  195. get
  196. {
  197. if (isDisposed)
  198. {
  199. throw new ObjectDisposedException("TarArchive");
  200. }
  201. return asciiTranslate;
  202. }
  203. set
  204. {
  205. if (isDisposed)
  206. {
  207. throw new ObjectDisposedException("TarArchive");
  208. }
  209. asciiTranslate = value;
  210. }
  211. }
  212. /// <summary>
  213. /// Set the ascii file translation flag.
  214. /// </summary>
  215. /// <param name= "translateAsciiFiles">
  216. /// If true, translate ascii text files.
  217. /// </param>
  218. [Obsolete("Use the AsciiTranslate property")]
  219. public void SetAsciiTranslation(bool translateAsciiFiles)
  220. {
  221. if (isDisposed)
  222. {
  223. throw new ObjectDisposedException("TarArchive");
  224. }
  225. asciiTranslate = translateAsciiFiles;
  226. }
  227. /// <summary>
  228. /// PathPrefix is added to entry names as they are written if the value is not null.
  229. /// A slash character is appended after PathPrefix
  230. /// </summary>
  231. public string PathPrefix
  232. {
  233. get
  234. {
  235. if (isDisposed)
  236. {
  237. throw new ObjectDisposedException("TarArchive");
  238. }
  239. return pathPrefix;
  240. }
  241. set
  242. {
  243. if (isDisposed)
  244. {
  245. throw new ObjectDisposedException("TarArchive");
  246. }
  247. pathPrefix = value;
  248. }
  249. }
  250. /// <summary>
  251. /// RootPath is removed from entry names if it is found at the
  252. /// beginning of the name.
  253. /// </summary>
  254. public string RootPath
  255. {
  256. get
  257. {
  258. if (isDisposed)
  259. {
  260. throw new ObjectDisposedException("TarArchive");
  261. }
  262. return rootPath;
  263. }
  264. set
  265. {
  266. if (isDisposed)
  267. {
  268. throw new ObjectDisposedException("TarArchive");
  269. }
  270. // Convert to forward slashes for matching. Trim trailing / for correct final path
  271. rootPath = value.Replace('\\', '/').TrimEnd('/');
  272. }
  273. }
  274. /// <summary>
  275. /// Set user and group information that will be used to fill in the
  276. /// tar archive's entry headers. This information is based on that available
  277. /// for the linux operating system, which is not always available on other
  278. /// operating systems. TarArchive allows the programmer to specify values
  279. /// to be used in their place.
  280. /// <see cref="ApplyUserInfoOverrides"/> is set to true by this call.
  281. /// </summary>
  282. /// <param name="userId">
  283. /// The user id to use in the headers.
  284. /// </param>
  285. /// <param name="userName">
  286. /// The user name to use in the headers.
  287. /// </param>
  288. /// <param name="groupId">
  289. /// The group id to use in the headers.
  290. /// </param>
  291. /// <param name="groupName">
  292. /// The group name to use in the headers.
  293. /// </param>
  294. public void SetUserInfo(int userId, string userName, int groupId, string groupName)
  295. {
  296. if (isDisposed)
  297. {
  298. throw new ObjectDisposedException("TarArchive");
  299. }
  300. this.userId = userId;
  301. this.userName = userName;
  302. this.groupId = groupId;
  303. this.groupName = groupName;
  304. applyUserInfoOverrides = true;
  305. }
  306. /// <summary>
  307. /// Get or set a value indicating if overrides defined by <see cref="SetUserInfo">SetUserInfo</see> should be applied.
  308. /// </summary>
  309. /// <remarks>If overrides are not applied then the values as set in each header will be used.</remarks>
  310. public bool ApplyUserInfoOverrides
  311. {
  312. get
  313. {
  314. if (isDisposed)
  315. {
  316. throw new ObjectDisposedException("TarArchive");
  317. }
  318. return applyUserInfoOverrides;
  319. }
  320. set
  321. {
  322. if (isDisposed)
  323. {
  324. throw new ObjectDisposedException("TarArchive");
  325. }
  326. applyUserInfoOverrides = value;
  327. }
  328. }
  329. /// <summary>
  330. /// Get the archive user id.
  331. /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
  332. /// on how to allow setting values on a per entry basis.
  333. /// </summary>
  334. /// <returns>
  335. /// The current user id.
  336. /// </returns>
  337. public int UserId
  338. {
  339. get
  340. {
  341. if (isDisposed)
  342. {
  343. throw new ObjectDisposedException("TarArchive");
  344. }
  345. return userId;
  346. }
  347. }
  348. /// <summary>
  349. /// Get the archive user name.
  350. /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
  351. /// on how to allow setting values on a per entry basis.
  352. /// </summary>
  353. /// <returns>
  354. /// The current user name.
  355. /// </returns>
  356. public string UserName
  357. {
  358. get
  359. {
  360. if (isDisposed)
  361. {
  362. throw new ObjectDisposedException("TarArchive");
  363. }
  364. return userName;
  365. }
  366. }
  367. /// <summary>
  368. /// Get the archive group id.
  369. /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
  370. /// on how to allow setting values on a per entry basis.
  371. /// </summary>
  372. /// <returns>
  373. /// The current group id.
  374. /// </returns>
  375. public int GroupId
  376. {
  377. get
  378. {
  379. if (isDisposed)
  380. {
  381. throw new ObjectDisposedException("TarArchive");
  382. }
  383. return groupId;
  384. }
  385. }
  386. /// <summary>
  387. /// Get the archive group name.
  388. /// See <see cref="ApplyUserInfoOverrides">ApplyUserInfoOverrides</see> for detail
  389. /// on how to allow setting values on a per entry basis.
  390. /// </summary>
  391. /// <returns>
  392. /// The current group name.
  393. /// </returns>
  394. public string GroupName
  395. {
  396. get
  397. {
  398. if (isDisposed)
  399. {
  400. throw new ObjectDisposedException("TarArchive");
  401. }
  402. return groupName;
  403. }
  404. }
  405. /// <summary>
  406. /// Get the archive's record size. Tar archives are composed of
  407. /// a series of RECORDS each containing a number of BLOCKS.
  408. /// This allowed tar archives to match the IO characteristics of
  409. /// the physical device being used. Archives are expected
  410. /// to be properly "blocked".
  411. /// </summary>
  412. /// <returns>
  413. /// The record size this archive is using.
  414. /// </returns>
  415. public int RecordSize
  416. {
  417. get
  418. {
  419. if (isDisposed)
  420. {
  421. throw new ObjectDisposedException("TarArchive");
  422. }
  423. if (tarIn != null)
  424. {
  425. return tarIn.RecordSize;
  426. }
  427. else if (tarOut != null)
  428. {
  429. return tarOut.RecordSize;
  430. }
  431. return TarBuffer.DefaultRecordSize;
  432. }
  433. }
  434. /// <summary>
  435. /// Sets the IsStreamOwner property on the underlying stream.
  436. /// Set this to false to prevent the Close of the TarArchive from closing the stream.
  437. /// </summary>
  438. public bool IsStreamOwner
  439. {
  440. set
  441. {
  442. if (tarIn != null)
  443. {
  444. tarIn.IsStreamOwner = value;
  445. }
  446. else
  447. {
  448. tarOut.IsStreamOwner = value;
  449. }
  450. }
  451. }
  452. /// <summary>
  453. /// Close the archive.
  454. /// </summary>
  455. [Obsolete("Use Close instead")]
  456. public void CloseArchive()
  457. {
  458. Close();
  459. }
  460. /// <summary>
  461. /// Perform the "list" command for the archive contents.
  462. ///
  463. /// NOTE That this method uses the <see cref="ProgressMessageEvent"> progress event</see> to actually list
  464. /// the contents. If the progress display event is not set, nothing will be listed!
  465. /// </summary>
  466. public void ListContents()
  467. {
  468. if (isDisposed)
  469. {
  470. throw new ObjectDisposedException("TarArchive");
  471. }
  472. while (true)
  473. {
  474. TarEntry entry = tarIn.GetNextEntry();
  475. if (entry == null)
  476. {
  477. break;
  478. }
  479. OnProgressMessageEvent(entry, null);
  480. }
  481. }
  482. /// <summary>
  483. /// Perform the "extract" command and extract the contents of the archive.
  484. /// </summary>
  485. /// <param name="destinationDirectory">
  486. /// The destination directory into which to extract.
  487. /// </param>
  488. public void ExtractContents(string destinationDirectory)
  489. {
  490. if (isDisposed)
  491. {
  492. throw new ObjectDisposedException("TarArchive");
  493. }
  494. while (true)
  495. {
  496. TarEntry entry = tarIn.GetNextEntry();
  497. if (entry == null)
  498. {
  499. break;
  500. }
  501. if (entry.TarHeader.TypeFlag == TarHeader.LF_LINK || entry.TarHeader.TypeFlag == TarHeader.LF_SYMLINK)
  502. continue;
  503. ExtractEntry(destinationDirectory, entry);
  504. }
  505. }
  506. /// <summary>
  507. /// Extract an entry from the archive. This method assumes that the
  508. /// tarIn stream has been properly set with a call to GetNextEntry().
  509. /// </summary>
  510. /// <param name="destDir">
  511. /// The destination directory into which to extract.
  512. /// </param>
  513. /// <param name="entry">
  514. /// The TarEntry returned by tarIn.GetNextEntry().
  515. /// </param>
  516. private void ExtractEntry(string destDir, TarEntry entry)
  517. {
  518. OnProgressMessageEvent(entry, null);
  519. string name = entry.Name;
  520. if (Path.IsPathRooted(name))
  521. {
  522. // NOTE:
  523. // for UNC names... \\machine\share\zoom\beet.txt gives \zoom\beet.txt
  524. name = name.Substring(Path.GetPathRoot(name).Length);
  525. }
  526. name = name.Replace('/', Path.DirectorySeparatorChar);
  527. string destFile = Path.Combine(destDir, name);
  528. if (entry.IsDirectory)
  529. {
  530. EnsureDirectoryExists(destFile);
  531. }
  532. else
  533. {
  534. string parentDirectory = Path.GetDirectoryName(destFile);
  535. EnsureDirectoryExists(parentDirectory);
  536. bool process = true;
  537. var fileInfo = new FileInfo(destFile);
  538. if (fileInfo.Exists)
  539. {
  540. if (keepOldFiles)
  541. {
  542. OnProgressMessageEvent(entry, "Destination file already exists");
  543. process = false;
  544. }
  545. else if ((fileInfo.Attributes & FileAttributes.ReadOnly) != 0)
  546. {
  547. OnProgressMessageEvent(entry, "Destination file already exists, and is read-only");
  548. process = false;
  549. }
  550. }
  551. if (process)
  552. {
  553. using (var outputStream = File.Create(destFile))
  554. {
  555. if (this.asciiTranslate)
  556. {
  557. // May need to translate the file.
  558. ExtractAndTranslateEntry(destFile, outputStream);
  559. }
  560. else
  561. {
  562. // If translation is disabled, just copy the entry across directly.
  563. tarIn.CopyEntryContents(outputStream);
  564. }
  565. }
  566. }
  567. }
  568. }
  569. // Extract a TAR entry, and perform an ASCII translation if required.
  570. private void ExtractAndTranslateEntry(string destFile, Stream outputStream)
  571. {
  572. bool asciiTrans = !IsBinary(destFile);
  573. if (asciiTrans)
  574. {
  575. using (var outw = new StreamWriter(outputStream, new UTF8Encoding(false), 1024, true))
  576. {
  577. byte[] rdbuf = new byte[32 * 1024];
  578. while (true)
  579. {
  580. int numRead = tarIn.Read(rdbuf, 0, rdbuf.Length);
  581. if (numRead <= 0)
  582. {
  583. break;
  584. }
  585. for (int off = 0, b = 0; b < numRead; ++b)
  586. {
  587. if (rdbuf[b] == 10)
  588. {
  589. string s = Encoding.ASCII.GetString(rdbuf, off, (b - off));
  590. outw.WriteLine(s);
  591. off = b + 1;
  592. }
  593. }
  594. }
  595. }
  596. }
  597. else
  598. {
  599. // No translation required.
  600. tarIn.CopyEntryContents(outputStream);
  601. }
  602. }
  603. /// <summary>
  604. /// Write an entry to the archive. This method will call the putNextEntry
  605. /// and then write the contents of the entry, and finally call closeEntry()
  606. /// for entries that are files. For directories, it will call putNextEntry(),
  607. /// and then, if the recurse flag is true, process each entry that is a
  608. /// child of the directory.
  609. /// </summary>
  610. /// <param name="sourceEntry">
  611. /// The TarEntry representing the entry to write to the archive.
  612. /// </param>
  613. /// <param name="recurse">
  614. /// If true, process the children of directory entries.
  615. /// </param>
  616. public void WriteEntry(TarEntry sourceEntry, bool recurse)
  617. {
  618. if (sourceEntry == null)
  619. {
  620. throw new ArgumentNullException(nameof(sourceEntry));
  621. }
  622. if (isDisposed)
  623. {
  624. throw new ObjectDisposedException("TarArchive");
  625. }
  626. try
  627. {
  628. if (recurse)
  629. {
  630. TarHeader.SetValueDefaults(sourceEntry.UserId, sourceEntry.UserName,
  631. sourceEntry.GroupId, sourceEntry.GroupName);
  632. }
  633. WriteEntryCore(sourceEntry, recurse);
  634. }
  635. finally
  636. {
  637. if (recurse)
  638. {
  639. TarHeader.RestoreSetValues();
  640. }
  641. }
  642. }
  643. /// <summary>
  644. /// Write an entry to the archive. This method will call the putNextEntry
  645. /// and then write the contents of the entry, and finally call closeEntry()
  646. /// for entries that are files. For directories, it will call putNextEntry(),
  647. /// and then, if the recurse flag is true, process each entry that is a
  648. /// child of the directory.
  649. /// </summary>
  650. /// <param name="sourceEntry">
  651. /// The TarEntry representing the entry to write to the archive.
  652. /// </param>
  653. /// <param name="recurse">
  654. /// If true, process the children of directory entries.
  655. /// </param>
  656. private void WriteEntryCore(TarEntry sourceEntry, bool recurse)
  657. {
  658. string tempFileName = null;
  659. string entryFilename = sourceEntry.File;
  660. var entry = (TarEntry)sourceEntry.Clone();
  661. if (applyUserInfoOverrides)
  662. {
  663. entry.GroupId = groupId;
  664. entry.GroupName = groupName;
  665. entry.UserId = userId;
  666. entry.UserName = userName;
  667. }
  668. OnProgressMessageEvent(entry, null);
  669. if (asciiTranslate && !entry.IsDirectory)
  670. {
  671. if (!IsBinary(entryFilename))
  672. {
  673. tempFileName = Path.GetTempFileName();
  674. using (StreamReader inStream = File.OpenText(entryFilename))
  675. {
  676. using (Stream outStream = File.Create(tempFileName))
  677. {
  678. while (true)
  679. {
  680. string line = inStream.ReadLine();
  681. if (line == null)
  682. {
  683. break;
  684. }
  685. byte[] data = Encoding.ASCII.GetBytes(line);
  686. outStream.Write(data, 0, data.Length);
  687. outStream.WriteByte((byte)'\n');
  688. }
  689. outStream.Flush();
  690. }
  691. }
  692. entry.Size = new FileInfo(tempFileName).Length;
  693. entryFilename = tempFileName;
  694. }
  695. }
  696. string newName = null;
  697. if (!String.IsNullOrEmpty(rootPath))
  698. {
  699. if (entry.Name.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase))
  700. {
  701. newName = entry.Name.Substring(rootPath.Length + 1);
  702. }
  703. }
  704. if (pathPrefix != null)
  705. {
  706. newName = (newName == null) ? pathPrefix + "/" + entry.Name : pathPrefix + "/" + newName;
  707. }
  708. if (newName != null)
  709. {
  710. entry.Name = newName;
  711. }
  712. tarOut.PutNextEntry(entry);
  713. if (entry.IsDirectory)
  714. {
  715. if (recurse)
  716. {
  717. TarEntry[] list = entry.GetDirectoryEntries();
  718. for (int i = 0; i < list.Length; ++i)
  719. {
  720. WriteEntryCore(list[i], recurse);
  721. }
  722. }
  723. }
  724. else
  725. {
  726. using (Stream inputStream = File.OpenRead(entryFilename))
  727. {
  728. byte[] localBuffer = new byte[32 * 1024];
  729. while (true)
  730. {
  731. int numRead = inputStream.Read(localBuffer, 0, localBuffer.Length);
  732. if (numRead <= 0)
  733. {
  734. break;
  735. }
  736. tarOut.Write(localBuffer, 0, numRead);
  737. }
  738. }
  739. if (!string.IsNullOrEmpty(tempFileName))
  740. {
  741. File.Delete(tempFileName);
  742. }
  743. tarOut.CloseEntry();
  744. }
  745. }
  746. /// <summary>
  747. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  748. /// </summary>
  749. public void Dispose()
  750. {
  751. Dispose(true);
  752. GC.SuppressFinalize(this);
  753. }
  754. /// <summary>
  755. /// Releases the unmanaged resources used by the FileStream and optionally releases the managed resources.
  756. /// </summary>
  757. /// <param name="disposing">true to release both managed and unmanaged resources;
  758. /// false to release only unmanaged resources.</param>
  759. protected virtual void Dispose(bool disposing)
  760. {
  761. if (!isDisposed)
  762. {
  763. isDisposed = true;
  764. if (disposing)
  765. {
  766. if (tarOut != null)
  767. {
  768. tarOut.Flush();
  769. tarOut.Dispose();
  770. }
  771. if (tarIn != null)
  772. {
  773. tarIn.Dispose();
  774. }
  775. }
  776. }
  777. }
  778. /// <summary>
  779. /// Closes the archive and releases any associated resources.
  780. /// </summary>
  781. public virtual void Close()
  782. {
  783. Dispose(true);
  784. }
  785. /// <summary>
  786. /// Ensures that resources are freed and other cleanup operations are performed
  787. /// when the garbage collector reclaims the <see cref="TarArchive"/>.
  788. /// </summary>
  789. ~TarArchive()
  790. {
  791. Dispose(false);
  792. }
  793. private static void EnsureDirectoryExists(string directoryName)
  794. {
  795. if (!Directory.Exists(directoryName))
  796. {
  797. try
  798. {
  799. Directory.CreateDirectory(directoryName);
  800. }
  801. catch (Exception e)
  802. {
  803. throw new TarException("Exception creating directory '" + directoryName + "', " + e.Message, e);
  804. }
  805. }
  806. }
  807. // TODO: TarArchive - Is there a better way to test for a text file?
  808. // It no longer reads entire files into memory but is still a weak test!
  809. // This assumes that byte values 0-7, 14-31 or 255 are binary
  810. // and that all non text files contain one of these values
  811. private static bool IsBinary(string filename)
  812. {
  813. using (FileStream fs = File.OpenRead(filename))
  814. {
  815. int sampleSize = Math.Min(4096, (int)fs.Length);
  816. byte[] content = new byte[sampleSize];
  817. int bytesRead = fs.Read(content, 0, sampleSize);
  818. for (int i = 0; i < bytesRead; ++i)
  819. {
  820. byte b = content[i];
  821. if ((b < 8) || ((b > 13) && (b < 32)) || (b == 255))
  822. {
  823. return true;
  824. }
  825. }
  826. }
  827. return false;
  828. }
  829. #region Instance Fields
  830. private bool keepOldFiles;
  831. private bool asciiTranslate;
  832. private int userId;
  833. private string userName = string.Empty;
  834. private int groupId;
  835. private string groupName = string.Empty;
  836. private string rootPath;
  837. private string pathPrefix;
  838. private bool applyUserInfoOverrides;
  839. private TarInputStream tarIn;
  840. private TarOutputStream tarOut;
  841. private bool isDisposed;
  842. #endregion Instance Fields
  843. }
  844. }