ZipEntryFactory.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // ZipEntryFactory.cs
  2. //
  3. // Copyright 2006 John Reilly
  4. //
  5. // Copyright (C) 2001 Free Software Foundation, Inc.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. //
  21. // Linking this library statically or dynamically with other modules is
  22. // making a combined work based on this library. Thus, the terms and
  23. // conditions of the GNU General Public License cover the whole
  24. // combination.
  25. //
  26. // As a special exception, the copyright holders of this library give you
  27. // permission to link this library with independent modules to produce an
  28. // executable, regardless of the license terms of these independent
  29. // modules, and to copy and distribute the resulting executable under
  30. // terms of your choice, provided that you also meet, for each linked
  31. // independent module, the terms and conditions of the license of that
  32. // module. An independent module is a module which is not derived from
  33. // or based on this library. If you modify this library, you may extend
  34. // this exception to your version of the library, but you are not
  35. // obligated to do so. If you do not wish to do so, delete this
  36. // exception statement from your version.
  37. // HISTORY
  38. // 2012-11-29 Z-1684 Added MakeFileEntry(string fileName, string entryName, bool useFileSystem)
  39. using System;
  40. using System.IO;
  41. using CommonMPQ.SharpZipLib.Core;
  42. namespace CommonMPQ.SharpZipLib.Zip
  43. {
  44. /// <summary>
  45. /// Basic implementation of <see cref="IEntryFactory"></see>
  46. /// </summary>
  47. public class ZipEntryFactory : IEntryFactory
  48. {
  49. #region Enumerations
  50. /// <summary>
  51. /// Defines the possible values to be used for the <see cref="ZipEntry.DateTime"/>.
  52. /// </summary>
  53. public enum TimeSetting
  54. {
  55. /// <summary>
  56. /// Use the recorded LastWriteTime value for the file.
  57. /// </summary>
  58. LastWriteTime,
  59. /// <summary>
  60. /// Use the recorded LastWriteTimeUtc value for the file
  61. /// </summary>
  62. LastWriteTimeUtc,
  63. /// <summary>
  64. /// Use the recorded CreateTime value for the file.
  65. /// </summary>
  66. CreateTime,
  67. /// <summary>
  68. /// Use the recorded CreateTimeUtc value for the file.
  69. /// </summary>
  70. CreateTimeUtc,
  71. /// <summary>
  72. /// Use the recorded LastAccessTime value for the file.
  73. /// </summary>
  74. LastAccessTime,
  75. /// <summary>
  76. /// Use the recorded LastAccessTimeUtc value for the file.
  77. /// </summary>
  78. LastAccessTimeUtc,
  79. /// <summary>
  80. /// Use a fixed value.
  81. /// </summary>
  82. /// <remarks>The actual <see cref="DateTime"/> value used can be
  83. /// specified via the <see cref="ZipEntryFactory(DateTime)"/> constructor or
  84. /// using the <see cref="ZipEntryFactory(TimeSetting)"/> with the setting set
  85. /// to <see cref="TimeSetting.Fixed"/> which will use the <see cref="DateTime"/> when this class was constructed.
  86. /// The <see cref="FixedDateTime"/> property can also be used to set this value.</remarks>
  87. Fixed,
  88. }
  89. #endregion
  90. #region Constructors
  91. /// <summary>
  92. /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
  93. /// </summary>
  94. /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
  95. public ZipEntryFactory()
  96. {
  97. nameTransform_ = new ZipNameTransform();
  98. }
  99. /// <summary>
  100. /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="TimeSetting"/>
  101. /// </summary>
  102. /// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating <see cref="ZipEntry">Zip entries</see>.</param>
  103. public ZipEntryFactory(TimeSetting timeSetting)
  104. {
  105. timeSetting_ = timeSetting;
  106. nameTransform_ = new ZipNameTransform();
  107. }
  108. /// <summary>
  109. /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="DateTime"/>
  110. /// </summary>
  111. /// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values to.</param>
  112. public ZipEntryFactory(DateTime time)
  113. {
  114. timeSetting_ = TimeSetting.Fixed;
  115. FixedDateTime = time;
  116. nameTransform_ = new ZipNameTransform();
  117. }
  118. #endregion
  119. #region Properties
  120. /// <summary>
  121. /// Get / set the <see cref="INameTransform"/> to be used when creating new <see cref="ZipEntry"/> values.
  122. /// </summary>
  123. /// <remarks>
  124. /// Setting this property to null will cause a default <see cref="ZipNameTransform">name transform</see> to be used.
  125. /// </remarks>
  126. public INameTransform NameTransform
  127. {
  128. get { return nameTransform_; }
  129. set
  130. {
  131. if (value == null) {
  132. nameTransform_ = new ZipNameTransform();
  133. }
  134. else {
  135. nameTransform_ = value;
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// Get / set the <see cref="TimeSetting"/> in use.
  141. /// </summary>
  142. public TimeSetting Setting
  143. {
  144. get { return timeSetting_; }
  145. set { timeSetting_ = value; }
  146. }
  147. /// <summary>
  148. /// Get / set the <see cref="DateTime"/> value to use when <see cref="Setting"/> is set to <see cref="TimeSetting.Fixed"/>
  149. /// </summary>
  150. public DateTime FixedDateTime
  151. {
  152. get { return fixedDateTime_; }
  153. set
  154. {
  155. if (value.Year < 1970) {
  156. throw new ArgumentException("Value is too old to be valid", "value");
  157. }
  158. fixedDateTime_ = value;
  159. }
  160. }
  161. /// <summary>
  162. /// A bitmask defining the attributes to be retrieved from the actual file.
  163. /// </summary>
  164. /// <remarks>The default is to get all possible attributes from the actual file.</remarks>
  165. public int GetAttributes
  166. {
  167. get { return getAttributes_; }
  168. set { getAttributes_ = value; }
  169. }
  170. /// <summary>
  171. /// A bitmask defining which attributes are to be set on.
  172. /// </summary>
  173. /// <remarks>By default no attributes are set on.</remarks>
  174. public int SetAttributes
  175. {
  176. get { return setAttributes_; }
  177. set { setAttributes_ = value; }
  178. }
  179. /// <summary>
  180. /// Get set a value indicating wether unidoce text should be set on.
  181. /// </summary>
  182. public bool IsUnicodeText
  183. {
  184. get { return isUnicodeText_; }
  185. set { isUnicodeText_ = value; }
  186. }
  187. #endregion
  188. #region IEntryFactory Members
  189. /// <summary>
  190. /// Make a new <see cref="ZipEntry"/> for a file.
  191. /// </summary>
  192. /// <param name="fileName">The name of the file to create a new entry for.</param>
  193. /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
  194. public ZipEntry MakeFileEntry(string fileName)
  195. {
  196. return MakeFileEntry(fileName, null, true);
  197. }
  198. /// <summary>
  199. /// Make a new <see cref="ZipEntry"/> for a file.
  200. /// </summary>
  201. /// <param name="fileName">The name of the file to create a new entry for.</param>
  202. /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
  203. /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
  204. public ZipEntry MakeFileEntry(string fileName, bool useFileSystem) {
  205. return MakeFileEntry(fileName, null, useFileSystem);
  206. }
  207. /// <summary>
  208. /// Make a new <see cref="ZipEntry"/> from a name.
  209. /// </summary>
  210. /// <param name="fileName">The name of the file to create a new entry for.</param>
  211. /// <param name="entryName">An alternative name to be used for the new entry. Null if not applicable.</param>
  212. /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
  213. /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
  214. public ZipEntry MakeFileEntry(string fileName, string entryName, bool useFileSystem)
  215. {
  216. ZipEntry result = new ZipEntry(nameTransform_.TransformFile(entryName != null && entryName.Length > 0 ? entryName : fileName));
  217. result.IsUnicodeText = isUnicodeText_;
  218. int externalAttributes = 0;
  219. bool useAttributes = (setAttributes_ != 0);
  220. FileInfo fi = null;
  221. if (useFileSystem)
  222. {
  223. fi = new FileInfo(fileName);
  224. }
  225. if ((fi != null) && fi.Exists)
  226. {
  227. switch (timeSetting_)
  228. {
  229. case TimeSetting.CreateTime:
  230. result.DateTime = fi.CreationTime;
  231. break;
  232. case TimeSetting.CreateTimeUtc:
  233. #if NETCF_1_0 || NETCF_2_0
  234. result.DateTime = fi.CreationTime.ToUniversalTime();
  235. #else
  236. result.DateTime = fi.CreationTimeUtc;
  237. #endif
  238. break;
  239. case TimeSetting.LastAccessTime:
  240. result.DateTime = fi.LastAccessTime;
  241. break;
  242. case TimeSetting.LastAccessTimeUtc:
  243. #if NETCF_1_0 || NETCF_2_0
  244. result.DateTime = fi.LastAccessTime.ToUniversalTime();
  245. #else
  246. result.DateTime = fi.LastAccessTimeUtc;
  247. #endif
  248. break;
  249. case TimeSetting.LastWriteTime:
  250. result.DateTime = fi.LastWriteTime;
  251. break;
  252. case TimeSetting.LastWriteTimeUtc:
  253. #if NETCF_1_0 || NETCF_2_0
  254. result.DateTime = fi.LastWriteTime.ToUniversalTime();
  255. #else
  256. result.DateTime = fi.LastWriteTimeUtc;
  257. #endif
  258. break;
  259. case TimeSetting.Fixed:
  260. result.DateTime = fixedDateTime_;
  261. break;
  262. default:
  263. throw new ZipException("Unhandled time setting in MakeFileEntry");
  264. }
  265. result.Size = fi.Length;
  266. useAttributes = true;
  267. externalAttributes = ((int)fi.Attributes & getAttributes_);
  268. }
  269. else
  270. {
  271. if (timeSetting_ == TimeSetting.Fixed)
  272. {
  273. result.DateTime = fixedDateTime_;
  274. }
  275. }
  276. if (useAttributes)
  277. {
  278. externalAttributes |= setAttributes_;
  279. result.ExternalFileAttributes = externalAttributes;
  280. }
  281. return result;
  282. }
  283. /// <summary>
  284. /// Make a new <see cref="ZipEntry"></see> for a directory.
  285. /// </summary>
  286. /// <param name="directoryName">The raw untransformed name for the new directory</param>
  287. /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
  288. public ZipEntry MakeDirectoryEntry(string directoryName)
  289. {
  290. return MakeDirectoryEntry(directoryName, true);
  291. }
  292. /// <summary>
  293. /// Make a new <see cref="ZipEntry"></see> for a directory.
  294. /// </summary>
  295. /// <param name="directoryName">The raw untransformed name for the new directory</param>
  296. /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
  297. /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
  298. public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)
  299. {
  300. ZipEntry result = new ZipEntry(nameTransform_.TransformDirectory(directoryName));
  301. result.IsUnicodeText = isUnicodeText_;
  302. result.Size = 0;
  303. int externalAttributes = 0;
  304. DirectoryInfo di = null;
  305. if (useFileSystem)
  306. {
  307. di = new DirectoryInfo(directoryName);
  308. }
  309. if ((di != null) && di.Exists)
  310. {
  311. switch (timeSetting_)
  312. {
  313. case TimeSetting.CreateTime:
  314. result.DateTime = di.CreationTime;
  315. break;
  316. case TimeSetting.CreateTimeUtc:
  317. #if NETCF_1_0 || NETCF_2_0
  318. result.DateTime = di.CreationTime.ToUniversalTime();
  319. #else
  320. result.DateTime = di.CreationTimeUtc;
  321. #endif
  322. break;
  323. case TimeSetting.LastAccessTime:
  324. result.DateTime = di.LastAccessTime;
  325. break;
  326. case TimeSetting.LastAccessTimeUtc:
  327. #if NETCF_1_0 || NETCF_2_0
  328. result.DateTime = di.LastAccessTime.ToUniversalTime();
  329. #else
  330. result.DateTime = di.LastAccessTimeUtc;
  331. #endif
  332. break;
  333. case TimeSetting.LastWriteTime:
  334. result.DateTime = di.LastWriteTime;
  335. break;
  336. case TimeSetting.LastWriteTimeUtc:
  337. #if NETCF_1_0 || NETCF_2_0
  338. result.DateTime = di.LastWriteTime.ToUniversalTime();
  339. #else
  340. result.DateTime = di.LastWriteTimeUtc;
  341. #endif
  342. break;
  343. case TimeSetting.Fixed:
  344. result.DateTime = fixedDateTime_;
  345. break;
  346. default:
  347. throw new ZipException("Unhandled time setting in MakeDirectoryEntry");
  348. }
  349. externalAttributes = ((int)di.Attributes & getAttributes_);
  350. }
  351. else
  352. {
  353. if (timeSetting_ == TimeSetting.Fixed)
  354. {
  355. result.DateTime = fixedDateTime_;
  356. }
  357. }
  358. // Always set directory attribute on.
  359. externalAttributes |= (setAttributes_ | 16);
  360. result.ExternalFileAttributes = externalAttributes;
  361. return result;
  362. }
  363. #endregion
  364. #region Instance Fields
  365. INameTransform nameTransform_;
  366. DateTime fixedDateTime_ = DateTime.Now;
  367. TimeSetting timeSetting_;
  368. bool isUnicodeText_;
  369. int getAttributes_ = -1;
  370. int setAttributes_;
  371. #endregion
  372. }
  373. }