FastZip.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. using System;
  2. using System.IO;
  3. using ICSharpCode.SharpZipLib.Core;
  4. using ICSharpCode.SharpZipLib.Zip.Compression;
  5. namespace ICSharpCode.SharpZipLib.Zip
  6. {
  7. /// <summary>
  8. /// FastZipEvents supports all events applicable to <see cref="FastZip">FastZip</see> operations.
  9. /// </summary>
  10. public class FastZipEvents
  11. {
  12. /// <summary>
  13. /// Delegate to invoke when processing directories.
  14. /// </summary>
  15. public event EventHandler<DirectoryEventArgs> ProcessDirectory;
  16. /// <summary>
  17. /// Delegate to invoke when processing files.
  18. /// </summary>
  19. public ProcessFileHandler ProcessFile;
  20. /// <summary>
  21. /// Delegate to invoke during processing of files.
  22. /// </summary>
  23. public ProgressHandler Progress;
  24. /// <summary>
  25. /// Delegate to invoke when processing for a file has been completed.
  26. /// </summary>
  27. public CompletedFileHandler CompletedFile;
  28. /// <summary>
  29. /// Delegate to invoke when processing directory failures.
  30. /// </summary>
  31. public DirectoryFailureHandler DirectoryFailure;
  32. /// <summary>
  33. /// Delegate to invoke when processing file failures.
  34. /// </summary>
  35. public FileFailureHandler FileFailure;
  36. /// <summary>
  37. /// Raise the <see cref="DirectoryFailure">directory failure</see> event.
  38. /// </summary>
  39. /// <param name="directory">The directory causing the failure.</param>
  40. /// <param name="e">The exception for this event.</param>
  41. /// <returns>A boolean indicating if execution should continue or not.</returns>
  42. public bool OnDirectoryFailure(string directory, Exception e)
  43. {
  44. bool result = false;
  45. DirectoryFailureHandler handler = DirectoryFailure;
  46. if (handler != null) {
  47. var args = new ScanFailureEventArgs(directory, e);
  48. handler(this, args);
  49. result = args.ContinueRunning;
  50. }
  51. return result;
  52. }
  53. /// <summary>
  54. /// Fires the <see cref="FileFailure"> file failure handler delegate</see>.
  55. /// </summary>
  56. /// <param name="file">The file causing the failure.</param>
  57. /// <param name="e">The exception for this failure.</param>
  58. /// <returns>A boolean indicating if execution should continue or not.</returns>
  59. public bool OnFileFailure(string file, Exception e)
  60. {
  61. FileFailureHandler handler = FileFailure;
  62. bool result = (handler != null);
  63. if (result) {
  64. var args = new ScanFailureEventArgs(file, e);
  65. handler(this, args);
  66. result = args.ContinueRunning;
  67. }
  68. return result;
  69. }
  70. /// <summary>
  71. /// Fires the <see cref="ProcessFile">ProcessFile delegate</see>.
  72. /// </summary>
  73. /// <param name="file">The file being processed.</param>
  74. /// <returns>A boolean indicating if execution should continue or not.</returns>
  75. public bool OnProcessFile(string file)
  76. {
  77. bool result = true;
  78. ProcessFileHandler handler = ProcessFile;
  79. if (handler != null) {
  80. var args = new ScanEventArgs(file);
  81. handler(this, args);
  82. result = args.ContinueRunning;
  83. }
  84. return result;
  85. }
  86. /// <summary>
  87. /// Fires the <see cref="CompletedFile"/> delegate
  88. /// </summary>
  89. /// <param name="file">The file whose processing has been completed.</param>
  90. /// <returns>A boolean indicating if execution should continue or not.</returns>
  91. public bool OnCompletedFile(string file)
  92. {
  93. bool result = true;
  94. CompletedFileHandler handler = CompletedFile;
  95. if (handler != null) {
  96. var args = new ScanEventArgs(file);
  97. handler(this, args);
  98. result = args.ContinueRunning;
  99. }
  100. return result;
  101. }
  102. /// <summary>
  103. /// Fires the <see cref="ProcessDirectory">process directory</see> delegate.
  104. /// </summary>
  105. /// <param name="directory">The directory being processed.</param>
  106. /// <param name="hasMatchingFiles">Flag indicating if the directory has matching files as determined by the current filter.</param>
  107. /// <returns>A <see cref="bool"/> of true if the operation should continue; false otherwise.</returns>
  108. public bool OnProcessDirectory(string directory, bool hasMatchingFiles)
  109. {
  110. bool result = true;
  111. EventHandler<DirectoryEventArgs> handler = ProcessDirectory;
  112. if (handler != null) {
  113. var args = new DirectoryEventArgs(directory, hasMatchingFiles);
  114. handler(this, args);
  115. result = args.ContinueRunning;
  116. }
  117. return result;
  118. }
  119. /// <summary>
  120. /// The minimum timespan between <see cref="Progress"/> events.
  121. /// </summary>
  122. /// <value>The minimum period of time between <see cref="Progress"/> events.</value>
  123. /// <seealso cref="Progress"/>
  124. /// <remarks>The default interval is three seconds.</remarks>
  125. public TimeSpan ProgressInterval {
  126. get { return progressInterval_; }
  127. set { progressInterval_ = value; }
  128. }
  129. #region Instance Fields
  130. TimeSpan progressInterval_ = TimeSpan.FromSeconds(3);
  131. #endregion
  132. }
  133. /// <summary>
  134. /// FastZip provides facilities for creating and extracting zip files.
  135. /// </summary>
  136. public class FastZip
  137. {
  138. #region Enumerations
  139. /// <summary>
  140. /// Defines the desired handling when overwriting files during extraction.
  141. /// </summary>
  142. public enum Overwrite
  143. {
  144. /// <summary>
  145. /// Prompt the user to confirm overwriting
  146. /// </summary>
  147. Prompt,
  148. /// <summary>
  149. /// Never overwrite files.
  150. /// </summary>
  151. Never,
  152. /// <summary>
  153. /// Always overwrite files.
  154. /// </summary>
  155. Always
  156. }
  157. #endregion
  158. #region Constructors
  159. /// <summary>
  160. /// Initialise a default instance of <see cref="FastZip"/>.
  161. /// </summary>
  162. public FastZip()
  163. {
  164. }
  165. /// <summary>
  166. /// Initialise a new instance of <see cref="FastZip"/>
  167. /// </summary>
  168. /// <param name="events">The <see cref="FastZipEvents">events</see> to use during operations.</param>
  169. public FastZip(FastZipEvents events)
  170. {
  171. events_ = events;
  172. }
  173. #endregion
  174. #region Properties
  175. /// <summary>
  176. /// Get/set a value indicating wether empty directories should be created.
  177. /// </summary>
  178. public bool CreateEmptyDirectories {
  179. get { return createEmptyDirectories_; }
  180. set { createEmptyDirectories_ = value; }
  181. }
  182. /// <summary>
  183. /// Get / set the password value.
  184. /// </summary>
  185. public string Password {
  186. get { return password_; }
  187. set { password_ = value; }
  188. }
  189. /// <summary>
  190. /// Get or set the <see cref="INameTransform"></see> active when creating Zip files.
  191. /// </summary>
  192. /// <seealso cref="EntryFactory"></seealso>
  193. public INameTransform NameTransform {
  194. get { return entryFactory_.NameTransform; }
  195. set {
  196. entryFactory_.NameTransform = value;
  197. }
  198. }
  199. /// <summary>
  200. /// Get or set the <see cref="IEntryFactory"></see> active when creating Zip files.
  201. /// </summary>
  202. public IEntryFactory EntryFactory {
  203. get { return entryFactory_; }
  204. set {
  205. if (value == null) {
  206. entryFactory_ = new ZipEntryFactory();
  207. } else {
  208. entryFactory_ = value;
  209. }
  210. }
  211. }
  212. /// <summary>
  213. /// Gets or sets the setting for <see cref="UseZip64">Zip64 handling when writing.</see>
  214. /// </summary>
  215. /// <remarks>
  216. /// The default value is dynamic which is not backwards compatible with old
  217. /// programs and can cause problems with XP's built in compression which cant
  218. /// read Zip64 archives. However it does avoid the situation were a large file
  219. /// is added and cannot be completed correctly.
  220. /// NOTE: Setting the size for entries before they are added is the best solution!
  221. /// By default the EntryFactory used by FastZip will set fhe file size.
  222. /// </remarks>
  223. public UseZip64 UseZip64 {
  224. get { return useZip64_; }
  225. set { useZip64_ = value; }
  226. }
  227. /// <summary>
  228. /// Get/set a value indicating wether file dates and times should
  229. /// be restored when extracting files from an archive.
  230. /// </summary>
  231. /// <remarks>The default value is false.</remarks>
  232. public bool RestoreDateTimeOnExtract {
  233. get {
  234. return restoreDateTimeOnExtract_;
  235. }
  236. set {
  237. restoreDateTimeOnExtract_ = value;
  238. }
  239. }
  240. /// <summary>
  241. /// Get/set a value indicating whether file attributes should
  242. /// be restored during extract operations
  243. /// </summary>
  244. public bool RestoreAttributesOnExtract {
  245. get { return restoreAttributesOnExtract_; }
  246. set { restoreAttributesOnExtract_ = value; }
  247. }
  248. /// <summary>
  249. /// Get/set the Compression Level that will be used
  250. /// when creating the zip
  251. /// </summary>
  252. public Deflater.CompressionLevel CompressionLevel{
  253. get { return compressionLevel_; }
  254. set { compressionLevel_ = value; }
  255. }
  256. #endregion
  257. #region Delegates
  258. /// <summary>
  259. /// Delegate called when confirming overwriting of files.
  260. /// </summary>
  261. public delegate bool ConfirmOverwriteDelegate(string fileName);
  262. #endregion
  263. #region CreateZip
  264. /// <summary>
  265. /// Create a zip file.
  266. /// </summary>
  267. /// <param name="zipFileName">The name of the zip file to create.</param>
  268. /// <param name="sourceDirectory">The directory to source files from.</param>
  269. /// <param name="recurse">True to recurse directories, false for no recursion.</param>
  270. /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
  271. /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
  272. public void CreateZip(string zipFileName, string sourceDirectory,
  273. bool recurse, string fileFilter, string directoryFilter)
  274. {
  275. CreateZip(File.Create(zipFileName), sourceDirectory, recurse, fileFilter, directoryFilter);
  276. }
  277. /// <summary>
  278. /// Create a zip file/archive.
  279. /// </summary>
  280. /// <param name="zipFileName">The name of the zip file to create.</param>
  281. /// <param name="sourceDirectory">The directory to obtain files and directories from.</param>
  282. /// <param name="recurse">True to recurse directories, false for no recursion.</param>
  283. /// <param name="fileFilter">The file filter to apply.</param>
  284. public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter)
  285. {
  286. CreateZip(File.Create(zipFileName), sourceDirectory, recurse, fileFilter, null);
  287. }
  288. /// <summary>
  289. /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
  290. /// </summary>
  291. /// <param name="outputStream">The stream to write archive data to.</param>
  292. /// <param name="sourceDirectory">The directory to source files from.</param>
  293. /// <param name="recurse">True to recurse directories, false for no recursion.</param>
  294. /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
  295. /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
  296. /// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
  297. public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
  298. {
  299. NameTransform = new ZipNameTransform(sourceDirectory);
  300. sourceDirectory_ = sourceDirectory;
  301. using (outputStream_ = new ZipOutputStream(outputStream)) {
  302. outputStream_.SetLevel((int)CompressionLevel);
  303. if (password_ != null) {
  304. outputStream_.Password = password_;
  305. }
  306. outputStream_.UseZip64 = UseZip64;
  307. var scanner = new FileSystemScanner(fileFilter, directoryFilter);
  308. scanner.ProcessFile += ProcessFile;
  309. if (this.CreateEmptyDirectories) {
  310. scanner.ProcessDirectory += ProcessDirectory;
  311. }
  312. if (events_ != null) {
  313. if (events_.FileFailure != null) {
  314. scanner.FileFailure += events_.FileFailure;
  315. }
  316. if (events_.DirectoryFailure != null) {
  317. scanner.DirectoryFailure += events_.DirectoryFailure;
  318. }
  319. }
  320. scanner.Scan(sourceDirectory, recurse);
  321. }
  322. }
  323. #endregion
  324. #region ExtractZip
  325. /// <summary>
  326. /// Extract the contents of a zip file.
  327. /// </summary>
  328. /// <param name="zipFileName">The zip file to extract from.</param>
  329. /// <param name="targetDirectory">The directory to save extracted information in.</param>
  330. /// <param name="fileFilter">A filter to apply to files.</param>
  331. public void ExtractZip(string zipFileName, string targetDirectory, string fileFilter)
  332. {
  333. ExtractZip(zipFileName, targetDirectory, Overwrite.Always, null, fileFilter, null, restoreDateTimeOnExtract_);
  334. }
  335. /// <summary>
  336. /// Extract the contents of a zip file.
  337. /// </summary>
  338. /// <param name="zipFileName">The zip file to extract from.</param>
  339. /// <param name="targetDirectory">The directory to save extracted information in.</param>
  340. /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
  341. /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
  342. /// <param name="fileFilter">A filter to apply to files.</param>
  343. /// <param name="directoryFilter">A filter to apply to directories.</param>
  344. /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param>
  345. public void ExtractZip(string zipFileName, string targetDirectory,
  346. Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
  347. string fileFilter, string directoryFilter, bool restoreDateTime)
  348. {
  349. Stream inputStream = File.Open(zipFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  350. ExtractZip(inputStream, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter, restoreDateTime, true);
  351. }
  352. /// <summary>
  353. /// Extract the contents of a zip file held in a stream.
  354. /// </summary>
  355. /// <param name="inputStream">The seekable input stream containing the zip to extract from.</param>
  356. /// <param name="targetDirectory">The directory to save extracted information in.</param>
  357. /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param>
  358. /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param>
  359. /// <param name="fileFilter">A filter to apply to files.</param>
  360. /// <param name="directoryFilter">A filter to apply to directories.</param>
  361. /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param>
  362. /// <param name="isStreamOwner">Flag indicating whether the inputStream will be closed by this method.</param>
  363. public void ExtractZip(Stream inputStream, string targetDirectory,
  364. Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,
  365. string fileFilter, string directoryFilter, bool restoreDateTime,
  366. bool isStreamOwner)
  367. {
  368. if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) {
  369. throw new ArgumentNullException("nameof(confirmDelegate)");
  370. }
  371. continueRunning_ = true;
  372. overwrite_ = overwrite;
  373. confirmDelegate_ = confirmDelegate;
  374. extractNameTransform_ = new WindowsNameTransform(targetDirectory);
  375. fileFilter_ = new NameFilter(fileFilter);
  376. directoryFilter_ = new NameFilter(directoryFilter);
  377. restoreDateTimeOnExtract_ = restoreDateTime;
  378. using (zipFile_ = new ZipFile(inputStream)) {
  379. if (password_ != null) {
  380. zipFile_.Password = password_;
  381. }
  382. zipFile_.IsStreamOwner = isStreamOwner;
  383. System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
  384. while (continueRunning_ && enumerator.MoveNext()) {
  385. var entry = (ZipEntry)enumerator.Current;
  386. if (entry.IsFile) {
  387. // TODO Path.GetDirectory can fail here on invalid characters.
  388. if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) {
  389. ExtractEntry(entry);
  390. }
  391. } else if (entry.IsDirectory) {
  392. if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) {
  393. ExtractEntry(entry);
  394. }
  395. } else {
  396. // Do nothing for volume labels etc...
  397. }
  398. }
  399. }
  400. }
  401. #endregion
  402. #region Internal Processing
  403. void ProcessDirectory(object sender, DirectoryEventArgs e)
  404. {
  405. if (!e.HasMatchingFiles && CreateEmptyDirectories) {
  406. if (events_ != null) {
  407. events_.OnProcessDirectory(e.Name, e.HasMatchingFiles);
  408. }
  409. if (e.ContinueRunning) {
  410. if (e.Name != sourceDirectory_) {
  411. ZipEntry entry = entryFactory_.MakeDirectoryEntry(e.Name);
  412. outputStream_.PutNextEntry(entry);
  413. }
  414. }
  415. }
  416. }
  417. void ProcessFile(object sender, ScanEventArgs e)
  418. {
  419. if ((events_ != null) && (events_.ProcessFile != null)) {
  420. events_.ProcessFile(sender, e);
  421. }
  422. if (e.ContinueRunning) {
  423. try {
  424. // The open below is equivalent to OpenRead which gaurantees that if opened the
  425. // file will not be changed by subsequent openers, but precludes opening in some cases
  426. // were it could succeed. ie the open may fail as its already open for writing and the share mode should reflect that.
  427. using (FileStream stream = File.Open(e.Name, FileMode.Open, FileAccess.Read, FileShare.Read)) {
  428. ZipEntry entry = entryFactory_.MakeFileEntry(e.Name);
  429. outputStream_.PutNextEntry(entry);
  430. AddFileContents(e.Name, stream);
  431. }
  432. } catch (Exception ex) {
  433. if (events_ != null) {
  434. continueRunning_ = events_.OnFileFailure(e.Name, ex);
  435. } else {
  436. continueRunning_ = false;
  437. throw;
  438. }
  439. }
  440. }
  441. }
  442. void AddFileContents(string name, Stream stream)
  443. {
  444. if (stream == null) {
  445. throw new ArgumentNullException("nameof(stream)");
  446. }
  447. if (buffer_ == null) {
  448. buffer_ = new byte[4096];
  449. }
  450. if ((events_ != null) && (events_.Progress != null)) {
  451. StreamUtils.Copy(stream, outputStream_, buffer_,
  452. events_.Progress, events_.ProgressInterval, this, name);
  453. } else {
  454. StreamUtils.Copy(stream, outputStream_, buffer_);
  455. }
  456. if (events_ != null) {
  457. continueRunning_ = events_.OnCompletedFile(name);
  458. }
  459. }
  460. void ExtractFileEntry(ZipEntry entry, string targetName)
  461. {
  462. bool proceed = true;
  463. if (overwrite_ != Overwrite.Always) {
  464. if (File.Exists(targetName)) {
  465. if ((overwrite_ == Overwrite.Prompt) && (confirmDelegate_ != null)) {
  466. proceed = confirmDelegate_(targetName);
  467. } else {
  468. proceed = false;
  469. }
  470. }
  471. }
  472. if (proceed) {
  473. if (events_ != null) {
  474. continueRunning_ = events_.OnProcessFile(entry.Name);
  475. }
  476. if (continueRunning_) {
  477. try {
  478. using (FileStream outputStream = File.Create(targetName)) {
  479. if (buffer_ == null) {
  480. buffer_ = new byte[4096];
  481. }
  482. if ((events_ != null) && (events_.Progress != null)) {
  483. StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_,
  484. events_.Progress, events_.ProgressInterval, this, entry.Name, entry.Size);
  485. } else {
  486. StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_);
  487. }
  488. if (events_ != null) {
  489. continueRunning_ = events_.OnCompletedFile(entry.Name);
  490. }
  491. }
  492. if (restoreDateTimeOnExtract_) {
  493. File.SetLastWriteTime(targetName, entry.DateTime);
  494. }
  495. if (RestoreAttributesOnExtract && entry.IsDOSEntry && (entry.ExternalFileAttributes != -1)) {
  496. var fileAttributes = (FileAttributes)entry.ExternalFileAttributes;
  497. // TODO: FastZip - Setting of other file attributes on extraction is a little trickier.
  498. fileAttributes &= (FileAttributes.Archive | FileAttributes.Normal | FileAttributes.ReadOnly | FileAttributes.Hidden);
  499. File.SetAttributes(targetName, fileAttributes);
  500. }
  501. } catch (Exception ex) {
  502. if (events_ != null) {
  503. continueRunning_ = events_.OnFileFailure(targetName, ex);
  504. } else {
  505. continueRunning_ = false;
  506. throw;
  507. }
  508. }
  509. }
  510. }
  511. }
  512. void ExtractEntry(ZipEntry entry)
  513. {
  514. bool doExtraction = entry.IsCompressionMethodSupported();
  515. string targetName = entry.Name;
  516. if (doExtraction) {
  517. if (entry.IsFile) {
  518. targetName = extractNameTransform_.TransformFile(targetName);
  519. } else if (entry.IsDirectory) {
  520. targetName = extractNameTransform_.TransformDirectory(targetName);
  521. }
  522. doExtraction = !(string.IsNullOrEmpty(targetName));
  523. }
  524. // TODO: Fire delegate/throw exception were compression method not supported, or name is invalid?
  525. string dirName = null;
  526. if (doExtraction) {
  527. if (entry.IsDirectory) {
  528. dirName = targetName;
  529. } else {
  530. dirName = Path.GetDirectoryName(Path.GetFullPath(targetName));
  531. }
  532. }
  533. if (doExtraction && !Directory.Exists(dirName)) {
  534. if (!entry.IsDirectory || CreateEmptyDirectories) {
  535. try {
  536. Directory.CreateDirectory(dirName);
  537. } catch (Exception ex) {
  538. doExtraction = false;
  539. if (events_ != null) {
  540. if (entry.IsDirectory) {
  541. continueRunning_ = events_.OnDirectoryFailure(targetName, ex);
  542. } else {
  543. continueRunning_ = events_.OnFileFailure(targetName, ex);
  544. }
  545. } else {
  546. continueRunning_ = false;
  547. throw;
  548. }
  549. }
  550. }
  551. }
  552. if (doExtraction && entry.IsFile) {
  553. ExtractFileEntry(entry, targetName);
  554. }
  555. }
  556. static int MakeExternalAttributes(FileInfo info)
  557. {
  558. return (int)info.Attributes;
  559. }
  560. static bool NameIsValid(string name)
  561. {
  562. return !string.IsNullOrEmpty(name) &&
  563. (name.IndexOfAny(Path.GetInvalidPathChars()) < 0);
  564. }
  565. #endregion
  566. #region Instance Fields
  567. bool continueRunning_;
  568. byte[] buffer_;
  569. ZipOutputStream outputStream_;
  570. ZipFile zipFile_;
  571. string sourceDirectory_;
  572. NameFilter fileFilter_;
  573. NameFilter directoryFilter_;
  574. Overwrite overwrite_;
  575. ConfirmOverwriteDelegate confirmDelegate_;
  576. bool restoreDateTimeOnExtract_;
  577. bool restoreAttributesOnExtract_;
  578. bool createEmptyDirectories_;
  579. FastZipEvents events_;
  580. IEntryFactory entryFactory_ = new ZipEntryFactory();
  581. INameTransform extractNameTransform_;
  582. UseZip64 useZip64_ = UseZip64.Dynamic;
  583. Deflater.CompressionLevel compressionLevel_ = Deflater.CompressionLevel.DEFAULT_COMPRESSION;
  584. string password_;
  585. #endregion
  586. }
  587. }