ZipNameTransform.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // ZipNameTransform.cs
  2. //
  3. // Copyright 2005 John Reilly
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. //
  19. // Linking this library statically or dynamically with other modules is
  20. // making a combined work based on this library. Thus, the terms and
  21. // conditions of the GNU General Public License cover the whole
  22. // combination.
  23. //
  24. // As a special exception, the copyright holders of this library give you
  25. // permission to link this library with independent modules to produce an
  26. // executable, regardless of the license terms of these independent
  27. // modules, and to copy and distribute the resulting executable under
  28. // terms of your choice, provided that you also meet, for each linked
  29. // independent module, the terms and conditions of the license of that
  30. // module. An independent module is a module which is not derived from
  31. // or based on this library. If you modify this library, you may extend
  32. // this exception to your version of the library, but you are not
  33. // obligated to do so. If you do not wish to do so, delete this
  34. // exception statement from your version.
  35. using System;
  36. using System.IO;
  37. using System.Text;
  38. using CommonMPQ.SharpZipLib.Core;
  39. namespace CommonMPQ.SharpZipLib.Zip
  40. {
  41. /// <summary>
  42. /// ZipNameTransform transforms names as per the Zip file naming convention.
  43. /// </summary>
  44. /// <remarks>The use of absolute names is supported although its use is not valid
  45. /// according to Zip naming conventions, and should not be used if maximum compatability is desired.</remarks>
  46. public class ZipNameTransform : INameTransform
  47. {
  48. #region Constructors
  49. /// <summary>
  50. /// Initialize a new instance of <see cref="ZipNameTransform"></see>
  51. /// </summary>
  52. public ZipNameTransform()
  53. {
  54. }
  55. /// <summary>
  56. /// Initialize a new instance of <see cref="ZipNameTransform"></see>
  57. /// </summary>
  58. /// <param name="trimPrefix">The string to trim from the front of paths if found.</param>
  59. public ZipNameTransform(string trimPrefix)
  60. {
  61. TrimPrefix = trimPrefix;
  62. }
  63. #endregion
  64. /// <summary>
  65. /// Static constructor.
  66. /// </summary>
  67. static ZipNameTransform()
  68. {
  69. char[] invalidPathChars;
  70. #if NET_1_0 || NET_1_1 || NETCF_1_0
  71. invalidPathChars = Path.InvalidPathChars;
  72. #else
  73. invalidPathChars = Path.GetInvalidPathChars();
  74. #endif
  75. int howMany = invalidPathChars.Length + 2;
  76. InvalidEntryCharsRelaxed = new char[howMany];
  77. Array.Copy(invalidPathChars, 0, InvalidEntryCharsRelaxed, 0, invalidPathChars.Length);
  78. InvalidEntryCharsRelaxed[howMany - 1] = '*';
  79. InvalidEntryCharsRelaxed[howMany - 2] = '?';
  80. howMany = invalidPathChars.Length + 4;
  81. InvalidEntryChars = new char[howMany];
  82. Array.Copy(invalidPathChars, 0, InvalidEntryChars, 0, invalidPathChars.Length);
  83. InvalidEntryChars[howMany - 1] = ':';
  84. InvalidEntryChars[howMany - 2] = '\\';
  85. InvalidEntryChars[howMany - 3] = '*';
  86. InvalidEntryChars[howMany - 4] = '?';
  87. }
  88. /// <summary>
  89. /// Transform a windows directory name according to the Zip file naming conventions.
  90. /// </summary>
  91. /// <param name="name">The directory name to transform.</param>
  92. /// <returns>The transformed name.</returns>
  93. public string TransformDirectory(string name)
  94. {
  95. name = TransformFile(name);
  96. if (name.Length > 0) {
  97. if ( !name.EndsWith("/") ) {
  98. name += "/";
  99. }
  100. }
  101. else {
  102. throw new ZipException("Cannot have an empty directory name");
  103. }
  104. return name;
  105. }
  106. /// <summary>
  107. /// Transform a windows file name according to the Zip file naming conventions.
  108. /// </summary>
  109. /// <param name="name">The file name to transform.</param>
  110. /// <returns>The transformed name.</returns>
  111. public string TransformFile(string name)
  112. {
  113. if (name != null) {
  114. string lowerName = name.ToLower();
  115. if ( (trimPrefix_ != null) && (lowerName.IndexOf(trimPrefix_) == 0) ) {
  116. name = name.Substring(trimPrefix_.Length);
  117. }
  118. name = name.Replace(@"\", "/");
  119. name = WindowsPathUtils.DropPathRoot(name);
  120. // Drop any leading slashes.
  121. while ((name.Length > 0) && (name[0] == '/'))
  122. {
  123. name = name.Remove(0, 1);
  124. }
  125. // Drop any trailing slashes.
  126. while ((name.Length > 0) && (name[name.Length - 1] == '/'))
  127. {
  128. name = name.Remove(name.Length - 1, 1);
  129. }
  130. // Convert consecutive // characters to /
  131. int index = name.IndexOf("//");
  132. while (index >= 0)
  133. {
  134. name = name.Remove(index, 1);
  135. index = name.IndexOf("//");
  136. }
  137. name = MakeValidName(name, '_');
  138. }
  139. else {
  140. name = string.Empty;
  141. }
  142. return name;
  143. }
  144. /// <summary>
  145. /// Get/set the path prefix to be trimmed from paths if present.
  146. /// </summary>
  147. /// <remarks>The prefix is trimmed before any conversion from
  148. /// a windows path is done.</remarks>
  149. public string TrimPrefix
  150. {
  151. get { return trimPrefix_; }
  152. set {
  153. trimPrefix_ = value;
  154. if (trimPrefix_ != null) {
  155. trimPrefix_ = trimPrefix_.ToLower();
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// Force a name to be valid by replacing invalid characters with a fixed value
  161. /// </summary>
  162. /// <param name="name">The name to force valid</param>
  163. /// <param name="replacement">The replacement character to use.</param>
  164. /// <returns>Returns a valid name</returns>
  165. static string MakeValidName(string name, char replacement)
  166. {
  167. int index = name.IndexOfAny(InvalidEntryChars);
  168. if (index >= 0) {
  169. StringBuilder builder = new StringBuilder(name);
  170. while (index >= 0 ) {
  171. builder[index] = replacement;
  172. if (index >= name.Length) {
  173. index = -1;
  174. }
  175. else {
  176. index = name.IndexOfAny(InvalidEntryChars, index + 1);
  177. }
  178. }
  179. name = builder.ToString();
  180. }
  181. if (name.Length > 0xffff) {
  182. throw new PathTooLongException();
  183. }
  184. return name;
  185. }
  186. /// <summary>
  187. /// Test a name to see if it is a valid name for a zip entry.
  188. /// </summary>
  189. /// <param name="name">The name to test.</param>
  190. /// <param name="relaxed">If true checking is relaxed about windows file names and absolute paths.</param>
  191. /// <returns>Returns true if the name is a valid zip name; false otherwise.</returns>
  192. /// <remarks>Zip path names are actually in Unix format, and should only contain relative paths.
  193. /// This means that any path stored should not contain a drive or
  194. /// device letter, or a leading slash. All slashes should forward slashes '/'.
  195. /// An empty name is valid for a file where the input comes from standard input.
  196. /// A null name is not considered valid.
  197. /// </remarks>
  198. public static bool IsValidName(string name, bool relaxed)
  199. {
  200. bool result = (name != null);
  201. if ( result ) {
  202. if ( relaxed ) {
  203. result = name.IndexOfAny(InvalidEntryCharsRelaxed) < 0;
  204. }
  205. else {
  206. result =
  207. (name.IndexOfAny(InvalidEntryChars) < 0) &&
  208. (name.IndexOf('/') != 0);
  209. }
  210. }
  211. return result;
  212. }
  213. /// <summary>
  214. /// Test a name to see if it is a valid name for a zip entry.
  215. /// </summary>
  216. /// <param name="name">The name to test.</param>
  217. /// <returns>Returns true if the name is a valid zip name; false otherwise.</returns>
  218. /// <remarks>Zip path names are actually in unix format,
  219. /// and should only contain relative paths if a path is present.
  220. /// This means that the path stored should not contain a drive or
  221. /// device letter, or a leading slash. All slashes should forward slashes '/'.
  222. /// An empty name is valid where the input comes from standard input.
  223. /// A null name is not considered valid.
  224. /// </remarks>
  225. public static bool IsValidName(string name)
  226. {
  227. bool result =
  228. (name != null) &&
  229. (name.IndexOfAny(InvalidEntryChars) < 0) &&
  230. (name.IndexOf('/') != 0)
  231. ;
  232. return result;
  233. }
  234. #region Instance Fields
  235. string trimPrefix_;
  236. #endregion
  237. #region Class Fields
  238. static readonly char[] InvalidEntryChars;
  239. static readonly char[] InvalidEntryCharsRelaxed;
  240. #endregion
  241. }
  242. }