System.IO.Compression.ZipFile.EntryFromPath C# (CSharp) Méthode

EntryFromPath() private static méthode

private static EntryFromPath ( string entry, int offset, int length, char &buffer, bool appendPathSeparator = false ) : string
entry string
offset int
length int
buffer char
appendPathSeparator bool
Résultat string
        private static string EntryFromPath(string entry, int offset, int length, ref char[] buffer, bool appendPathSeparator = false)
        {
            Debug.Assert(length <= entry.Length - offset);
            Debug.Assert(buffer != null);

            // Remove any leading slashes from the entry name:
            while (length > 0)
            {
                if (entry[offset] != Path.DirectorySeparatorChar && 
                    entry[offset] != Path.AltDirectorySeparatorChar)
                    break;

                offset++;
                length--;
            }

            if (length == 0)
                return appendPathSeparator ? PathSeparator.ToString() : string.Empty;

            int resultLength = appendPathSeparator ? length + 1 : length;
            EnsureCapacity(ref buffer, resultLength);
            entry.CopyTo(offset, buffer, 0, length);

            // '/' is a more broadly recognized directory separator on all platforms (eg: mac, linux)
            // We don't use Path.DirectorySeparatorChar or AltDirectorySeparatorChar because this is
            // explicitly trying to standardize to '/'
            for (int i = 0; i < length; i++)
            {
                char ch = buffer[i];
                if (ch == Path.DirectorySeparatorChar || ch == Path.AltDirectorySeparatorChar)
                    buffer[i] = PathSeparator;
            }

            if (appendPathSeparator)
                buffer[length] = PathSeparator;

            return new string(buffer, 0, resultLength);
        }

Usage Example

        private static void DoCreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel?compressionLevel, bool includeBaseDirectory, Encoding entryNameEncoding)
        {
            string str;
            char   sPathSeperator;

            sourceDirectoryName        = Path.GetFullPath(sourceDirectoryName);
            destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);
            using (ZipArchive zipArchive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding))
            {
                bool          flag          = true;
                DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectoryName);
                string        fullName      = directoryInfo.FullName;
                if (includeBaseDirectory && directoryInfo.Parent != null)
                {
                    fullName = directoryInfo.Parent.FullName;
                }
                foreach (FileSystemInfo fileSystemInfo in directoryInfo.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
                {
                    flag = false;
                    int length = fileSystemInfo.FullName.Length - fullName.Length;
                    if (!LocalAppContextSwitches.ZipFileUseBackslash)
                    {
                        str = ZipFile.EntryFromPath(fileSystemInfo.FullName, fullName.Length, length);
                    }
                    else
                    {
                        str = fileSystemInfo.FullName.Substring(fullName.Length, length);
                        str = str.TrimStart(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
                    }
                    if (!(fileSystemInfo is FileInfo))
                    {
                        DirectoryInfo directoryInfo1 = fileSystemInfo as DirectoryInfo;
                        if (directoryInfo1 == null || !ZipFile.IsDirEmpty(directoryInfo1))
                        {
                            continue;
                        }
                        sPathSeperator = ZipFile.s_pathSeperator;
                        zipArchive.CreateEntry(string.Concat(str, sPathSeperator.ToString()));
                    }
                    else
                    {
                        ZipFileExtensions.DoCreateEntryFromFile(zipArchive, fileSystemInfo.FullName, str, compressionLevel);
                    }
                }
                if (includeBaseDirectory & flag)
                {
                    str            = (LocalAppContextSwitches.ZipFileUseBackslash ? directoryInfo.Name : ZipFile.EntryFromPath(directoryInfo.Name, 0, directoryInfo.Name.Length));
                    sPathSeperator = ZipFile.s_pathSeperator;
                    zipArchive.CreateEntry(string.Concat(str, sPathSeperator.ToString()));
                }
            }
        }