123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using System;
- using System.IO;
- using System.Text;
- using CommonMPQ.SharpZipLib.Core;
- namespace CommonMPQ.SharpZipLib.Zip
- {
-
-
-
- public class WindowsNameTransform : INameTransform
- {
-
-
-
-
- public WindowsNameTransform(string baseDirectory)
- {
- if ( baseDirectory == null ) {
- throw new ArgumentNullException("baseDirectory", "Directory name is invalid");
- }
- BaseDirectory = baseDirectory;
- }
-
-
-
-
- public WindowsNameTransform()
- {
-
- }
-
-
-
-
- public string BaseDirectory
- {
- get { return _baseDirectory; }
- set {
- if ( value == null ) {
- throw new ArgumentNullException("value");
- }
- _baseDirectory = Path.GetFullPath(value);
- }
- }
-
-
-
-
- public bool TrimIncomingPaths
- {
- get { return _trimIncomingPaths; }
- set { _trimIncomingPaths = value; }
- }
-
-
-
-
-
-
- public string TransformDirectory(string name)
- {
- name = TransformFile(name);
- if (name.Length > 0) {
- while ( name.EndsWith(Path.DirectorySeparatorChar.ToString()) ) {
- name = name.Remove(name.Length - 1, 1);
- }
- }
- else {
- throw new ZipException("Cannot have an empty directory name");
- }
- return name;
- }
-
-
-
-
-
-
- public string TransformFile(string name)
- {
- if (name != null) {
- name = MakeValidName(name, _replacementChar);
-
- if ( _trimIncomingPaths ) {
- name = Path.GetFileName(name);
- }
-
-
-
- if ( _baseDirectory != null ) {
- name = Path.Combine(_baseDirectory, name);
- }
- }
- else {
- name = string.Empty;
- }
- return name;
- }
-
-
-
-
-
-
-
- public static bool IsValidName(string name)
- {
- bool result =
- (name != null) &&
- (name.Length <= MaxPath) &&
- (string.Compare(name, MakeValidName(name, '_')) == 0)
- ;
- return result;
- }
-
-
-
- static WindowsNameTransform()
- {
- char[] invalidPathChars;
- #if NET_1_0 || NET_1_1 || NETCF_1_0
- invalidPathChars = Path.InvalidPathChars;
- #else
- invalidPathChars = Path.GetInvalidPathChars();
- #endif
- int howMany = invalidPathChars.Length + 3;
- InvalidEntryChars = new char[howMany];
- Array.Copy(invalidPathChars, 0, InvalidEntryChars, 0, invalidPathChars.Length);
- InvalidEntryChars[howMany - 1] = '*';
- InvalidEntryChars[howMany - 2] = '?';
- InvalidEntryChars[howMany - 3] = ':';
- }
-
-
-
-
-
-
- public static string MakeValidName(string name, char replacement)
- {
- if ( name == null ) {
- throw new ArgumentNullException("name");
- }
-
- name = WindowsPathUtils.DropPathRoot(name.Replace("/", Path.DirectorySeparatorChar.ToString()));
-
- while ( (name.Length > 0) && (name[0] == Path.DirectorySeparatorChar)) {
- name = name.Remove(0, 1);
- }
-
- while ( (name.Length > 0) && (name[name.Length - 1] == Path.DirectorySeparatorChar)) {
- name = name.Remove(name.Length - 1, 1);
- }
-
- int index = name.IndexOf(string.Format("{0}{0}", Path.DirectorySeparatorChar));
- while (index >= 0) {
- name = name.Remove(index, 1);
- index = name.IndexOf(Path.DirectorySeparatorChar);
- }
-
- index = name.IndexOfAny(InvalidEntryChars);
- if (index >= 0) {
- StringBuilder builder = new StringBuilder(name);
- while (index >= 0 ) {
- builder[index] = replacement;
- if (index >= name.Length) {
- index = -1;
- }
- else {
- index = name.IndexOfAny(InvalidEntryChars, index + 1);
- }
- }
- name = builder.ToString();
- }
-
-
-
- if ( name.Length > MaxPath ) {
- throw new PathTooLongException();
- }
-
- return name;
- }
-
-
-
- public char Replacement
- {
- get { return _replacementChar; }
- set {
- for ( int i = 0; i < InvalidEntryChars.Length; ++i ) {
- if ( InvalidEntryChars[i] == value ) {
- throw new ArgumentException("invalid path character");
- }
- }
- if ((value == Path.DirectorySeparatorChar) || (value == Path.AltDirectorySeparatorChar)) {
- throw new ArgumentException("invalid replacement character");
- }
-
- _replacementChar = value;
- }
- }
-
-
-
-
-
- const int MaxPath = 260;
-
- #region Instance Fields
- string _baseDirectory;
- bool _trimIncomingPaths;
- char _replacementChar = '_';
- #endregion
-
- #region Class Fields
- static readonly char[] InvalidEntryChars;
- #endregion
- }
- }
|