ZipEntryFactory.cs 10 KB

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