Esempio n. 1
0
        /// <summary>
        /// Transform a windows file name according to the Zip file naming conventions.
        /// </summary>
        /// <param name="name">The file name to transform.</param>
        /// <returns>The transformed name.</returns>
        public string TransformFile(string name)
        {
            if (name != null)
            {
                string lowerName = name.ToLower();
                if ((trimPrefix_ != null) && (lowerName.IndexOf(trimPrefix_) == 0))
                {
                    name = name.Substring(trimPrefix_.Length);
                }

                name = name.Replace(@"\", "/");
                name = WindowsPathUtils.DropPathRoot(name);

                // Drop any leading slashes.
                while ((name.Length > 0) && (name[0] == '/'))
                {
                    name = name.Remove(0, 1);
                }

                // Drop any trailing slashes.
                while ((name.Length > 0) && (name[name.Length - 1] == '/'))
                {
                    name = name.Remove(name.Length - 1, 1);
                }

                // Convert consecutive // characters to /
                int index = name.IndexOf("//");
                while (index >= 0)
                {
                    name  = name.Remove(index, 1);
                    index = name.IndexOf("//");
                }

                name = MakeValidName(name, '_');
            }
            else
            {
                name = string.Empty;
            }
            return(name);
        }
Esempio n. 2
0
        /// <summary>
        /// Force a name to be valid by replacing invalid characters with a fixed value
        /// </summary>
        /// <param name="name">The name to make valid</param>
        /// <param name="replacement">The replacement character to use for any invalid characters.</param>
        /// <returns>Returns a valid name</returns>
        public static string MakeValidName(string name, char replacement)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            name = WindowsPathUtils.DropPathRoot(name.Replace("/", @"\"));

            // Drop any leading slashes.
            while ((name.Length > 0) && (name[0] == '\\'))
            {
                name = name.Remove(0, 1);
            }

            // Drop any trailing slashes.
            while ((name.Length > 0) && (name[name.Length - 1] == '\\'))
            {
                name = name.Remove(name.Length - 1, 1);
            }

            // Convert consecutive \\ characters to \
            int index = name.IndexOf(@"\\");

            while (index >= 0)
            {
                name  = name.Remove(index, 1);
                index = name.IndexOf(@"\\");
            }

            // Convert any invalid characters using the replacement one.
            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();
            }

            // Check for names greater than MaxPath characters.
            // TODO: Were is CLR version of MaxPath defined?  Can't find it in Environment.
            if (name.Length > MaxPath)
            {
                throw new PathTooLongException();
            }

            return(name);
        }