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

IsDirEmpty() private static méthode

private static IsDirEmpty ( DirectoryInfo possiblyEmptyDir ) : bool
possiblyEmptyDir DirectoryInfo
Résultat bool
        private static bool IsDirEmpty(DirectoryInfo possiblyEmptyDir)
        {
            using (IEnumerator<string> enumerator = Directory.EnumerateFileSystemEntries(possiblyEmptyDir.FullName).GetEnumerator())
                return !enumerator.MoveNext();
        }
    }  // class ZipFile

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()));
                }
            }
        }