ZipEntryFactory.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using ICSharpCode.SharpZipLib.Core;
  2. using System;
  3. using System.IO;
  4. namespace ICSharpCode.SharpZipLib.Zip
  5. {
  6. /// <summary>
  7. /// Basic implementation of <see cref="IEntryFactory"></see>
  8. /// </summary>
  9. public class ZipEntryFactory : IEntryFactory
  10. {
  11. #region Enumerations
  12. /// <summary>
  13. /// Defines the possible values to be used for the <see cref="ZipEntry.DateTime"/>.
  14. /// </summary>
  15. public enum TimeSetting
  16. {
  17. /// <summary>
  18. /// Use the recorded LastWriteTime value for the file.
  19. /// </summary>
  20. LastWriteTime,
  21. /// <summary>
  22. /// Use the recorded LastWriteTimeUtc value for the file
  23. /// </summary>
  24. LastWriteTimeUtc,
  25. /// <summary>
  26. /// Use the recorded CreateTime value for the file.
  27. /// </summary>
  28. CreateTime,
  29. /// <summary>
  30. /// Use the recorded CreateTimeUtc value for the file.
  31. /// </summary>
  32. CreateTimeUtc,
  33. /// <summary>
  34. /// Use the recorded LastAccessTime value for the file.
  35. /// </summary>
  36. LastAccessTime,
  37. /// <summary>
  38. /// Use the recorded LastAccessTimeUtc value for the file.
  39. /// </summary>
  40. LastAccessTimeUtc,
  41. /// <summary>
  42. /// Use a fixed value.
  43. /// </summary>
  44. /// <remarks>The actual <see cref="DateTime"/> value used can be
  45. /// specified via the <see cref="ZipEntryFactory(DateTime)"/> constructor or
  46. /// using the <see cref="ZipEntryFactory(TimeSetting)"/> with the setting set
  47. /// to <see cref="TimeSetting.Fixed"/> which will use the <see cref="DateTime"/> when this class was constructed.
  48. /// The <see cref="FixedDateTime"/> property can also be used to set this value.</remarks>
  49. Fixed,
  50. }
  51. #endregion Enumerations
  52. #region Constructors
  53. /// <summary>
  54. /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
  55. /// </summary>
  56. /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
  57. public ZipEntryFactory()
  58. {
  59. nameTransform_ = new ZipNameTransform();
  60. isUnicodeText_ = ZipStrings.UseUnicode;
  61. }
  62. /// <summary>
  63. /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="TimeSetting"/>
  64. /// </summary>
  65. /// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating <see cref="ZipEntry">Zip entries</see>.</param>
  66. public ZipEntryFactory(TimeSetting timeSetting) : this()
  67. {
  68. timeSetting_ = timeSetting;
  69. }
  70. /// <summary>
  71. /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="DateTime"/>
  72. /// </summary>
  73. /// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values to.</param>
  74. public ZipEntryFactory(DateTime time) : this()
  75. {
  76. timeSetting_ = TimeSetting.Fixed;
  77. FixedDateTime = time;
  78. }
  79. #endregion Constructors
  80. #region Properties
  81. /// <summary>
  82. /// Get / set the <see cref="INameTransform"/> to be used when creating new <see cref="ZipEntry"/> values.
  83. /// </summary>
  84. /// <remarks>
  85. /// Setting this property to null will cause a default <see cref="ZipNameTransform">name transform</see> to be used.
  86. /// </remarks>
  87. public INameTransform NameTransform
  88. {
  89. get { return nameTransform_; }
  90. set
  91. {
  92. if (value == null)
  93. {
  94. nameTransform_ = new ZipNameTransform();
  95. }
  96. else
  97. {
  98. nameTransform_ = value;
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Get / set the <see cref="TimeSetting"/> in use.
  104. /// </summary>
  105. public TimeSetting Setting
  106. {
  107. get { return timeSetting_; }
  108. set { timeSetting_ = value; }
  109. }
  110. /// <summary>
  111. /// Get / set the <see cref="DateTime"/> value to use when <see cref="Setting"/> is set to <see cref="TimeSetting.Fixed"/>
  112. /// </summary>
  113. public DateTime FixedDateTime
  114. {
  115. get { return fixedDateTime_; }
  116. set
  117. {
  118. if (value.Year < 1970)
  119. {
  120. throw new ArgumentException("Value is too old to be valid", nameof(value));
  121. }
  122. fixedDateTime_ = value;
  123. }
  124. }
  125. /// <summary>
  126. /// A bitmask defining the attributes to be retrieved from the actual file.
  127. /// </summary>
  128. /// <remarks>The default is to get all possible attributes from the actual file.</remarks>
  129. public int GetAttributes
  130. {
  131. get { return getAttributes_; }
  132. set { getAttributes_ = value; }
  133. }
  134. /// <summary>
  135. /// A bitmask defining which attributes are to be set on.
  136. /// </summary>
  137. /// <remarks>By default no attributes are set on.</remarks>
  138. public int SetAttributes
  139. {
  140. get { return setAttributes_; }
  141. set { setAttributes_ = value; }
  142. }
  143. /// <summary>
  144. /// Get set a value indicating wether unidoce text should be set on.
  145. /// </summary>
  146. public bool IsUnicodeText
  147. {
  148. get { return isUnicodeText_; }
  149. set { isUnicodeText_ = value; }
  150. }
  151. #endregion Properties
  152. #region IEntryFactory Members
  153. /// <summary>
  154. /// Make a new <see cref="ZipEntry"/> for a file.
  155. /// </summary>
  156. /// <param name="fileName">The name of the file to create a new entry for.</param>
  157. /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
  158. public ZipEntry MakeFileEntry(string fileName)
  159. {
  160. return MakeFileEntry(fileName, null, true);
  161. }
  162. /// <summary>
  163. /// Make a new <see cref="ZipEntry"/> for a file.
  164. /// </summary>
  165. /// <param name="fileName">The name of the file to create a new entry for.</param>
  166. /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
  167. /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
  168. public ZipEntry MakeFileEntry(string fileName, bool useFileSystem)
  169. {
  170. return MakeFileEntry(fileName, null, useFileSystem);
  171. }
  172. /// <summary>
  173. /// Make a new <see cref="ZipEntry"/> from a name.
  174. /// </summary>
  175. /// <param name="fileName">The name of the file to create a new entry for.</param>
  176. /// <param name="entryName">An alternative name to be used for the new entry. Null if not applicable.</param>
  177. /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
  178. /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
  179. public ZipEntry MakeFileEntry(string fileName, string entryName, bool useFileSystem)
  180. {
  181. var result = new ZipEntry(nameTransform_.TransformFile(!string.IsNullOrEmpty(entryName) ? entryName : fileName));
  182. result.IsUnicodeText = isUnicodeText_;
  183. int externalAttributes = 0;
  184. bool useAttributes = (setAttributes_ != 0);
  185. FileInfo fi = null;
  186. if (useFileSystem)
  187. {
  188. fi = new FileInfo(fileName);
  189. }
  190. if ((fi != null) && fi.Exists)
  191. {
  192. switch (timeSetting_)
  193. {
  194. case TimeSetting.CreateTime:
  195. result.DateTime = fi.CreationTime;
  196. break;
  197. case TimeSetting.CreateTimeUtc:
  198. result.DateTime = fi.CreationTimeUtc;
  199. break;
  200. case TimeSetting.LastAccessTime:
  201. result.DateTime = fi.LastAccessTime;
  202. break;
  203. case TimeSetting.LastAccessTimeUtc:
  204. result.DateTime = fi.LastAccessTimeUtc;
  205. break;
  206. case TimeSetting.LastWriteTime:
  207. result.DateTime = fi.LastWriteTime;
  208. break;
  209. case TimeSetting.LastWriteTimeUtc:
  210. result.DateTime = fi.LastWriteTimeUtc;
  211. break;
  212. case TimeSetting.Fixed:
  213. result.DateTime = fixedDateTime_;
  214. break;
  215. default:
  216. throw new ZipException("Unhandled time setting in MakeFileEntry");
  217. }
  218. result.Size = fi.Length;
  219. useAttributes = true;
  220. externalAttributes = ((int)fi.Attributes & getAttributes_);
  221. }
  222. else
  223. {
  224. if (timeSetting_ == TimeSetting.Fixed)
  225. {
  226. result.DateTime = fixedDateTime_;
  227. }
  228. }
  229. if (useAttributes)
  230. {
  231. externalAttributes |= setAttributes_;
  232. result.ExternalFileAttributes = externalAttributes;
  233. }
  234. return result;
  235. }
  236. /// <summary>
  237. /// Make a new <see cref="ZipEntry"></see> for a directory.
  238. /// </summary>
  239. /// <param name="directoryName">The raw untransformed name for the new directory</param>
  240. /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
  241. public ZipEntry MakeDirectoryEntry(string directoryName)
  242. {
  243. return MakeDirectoryEntry(directoryName, true);
  244. }
  245. /// <summary>
  246. /// Make a new <see cref="ZipEntry"></see> for a directory.
  247. /// </summary>
  248. /// <param name="directoryName">The raw untransformed name for the new directory</param>
  249. /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
  250. /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
  251. public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)
  252. {
  253. var result = new ZipEntry(nameTransform_.TransformDirectory(directoryName));
  254. result.IsUnicodeText = isUnicodeText_;
  255. result.Size = 0;
  256. int externalAttributes = 0;
  257. DirectoryInfo di = null;
  258. if (useFileSystem)
  259. {
  260. di = new DirectoryInfo(directoryName);
  261. }
  262. if ((di != null) && di.Exists)
  263. {
  264. switch (timeSetting_)
  265. {
  266. case TimeSetting.CreateTime:
  267. result.DateTime = di.CreationTime;
  268. break;
  269. case TimeSetting.CreateTimeUtc:
  270. result.DateTime = di.CreationTimeUtc;
  271. break;
  272. case TimeSetting.LastAccessTime:
  273. result.DateTime = di.LastAccessTime;
  274. break;
  275. case TimeSetting.LastAccessTimeUtc:
  276. result.DateTime = di.LastAccessTimeUtc;
  277. break;
  278. case TimeSetting.LastWriteTime:
  279. result.DateTime = di.LastWriteTime;
  280. break;
  281. case TimeSetting.LastWriteTimeUtc:
  282. result.DateTime = di.LastWriteTimeUtc;
  283. break;
  284. case TimeSetting.Fixed:
  285. result.DateTime = fixedDateTime_;
  286. break;
  287. default:
  288. throw new ZipException("Unhandled time setting in MakeDirectoryEntry");
  289. }
  290. externalAttributes = ((int)di.Attributes & getAttributes_);
  291. }
  292. else
  293. {
  294. if (timeSetting_ == TimeSetting.Fixed)
  295. {
  296. result.DateTime = fixedDateTime_;
  297. }
  298. }
  299. // Always set directory attribute on.
  300. externalAttributes |= (setAttributes_ | 16);
  301. result.ExternalFileAttributes = externalAttributes;
  302. return result;
  303. }
  304. #endregion IEntryFactory Members
  305. #region Instance Fields
  306. private INameTransform nameTransform_;
  307. private DateTime fixedDateTime_ = DateTime.Now;
  308. private TimeSetting timeSetting_;
  309. private bool isUnicodeText_;
  310. private int getAttributes_ = -1;
  311. private int setAttributes_;
  312. #endregion Instance Fields
  313. }
  314. }