/// <summary> /// Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level and character encoding for entry names, and optionally includes the base directory. /// </summary> /// <param name="destinationArchiveFileName">The path of the archive to be created, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.</param> /// <param name="sourceDirectoryName">The path to the directory to be archived, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.</param> /// <param name="includeBaseDirectory">true to include the directory name from <paramref name="sourceDirectoryName"/> at the root of the archive; false to include only the contents of the directory.</param> /// <param name="includeSubDirectories">true to include all subdirectories from <paramref name="sourceDirectoryName"/>; false to include only the contents of the top directory.</param> /// <param name="entryNameEncoding">The encoding to use when reading or writing entry names in this archive. Specify a value for this parameter only when required for interoperability with ZIP archive tools and libraries that do not support UTF-8 encoding for entry names.</param> private static void DoCreateFromDirectory(string destinationArchiveFileName, string sourceDirectoryName, bool includeBaseDirectory, bool includeSubDirectories, Encoding entryNameEncoding) { if (sourceDirectoryName == null) { throw new ArgumentNullException("sourceDirectoryName"); } if (destinationArchiveFileName == null) { throw new ArgumentNullException("destinationArchiveFileName"); } sourceDirectoryName = Path.GetFullPath(sourceDirectoryName); if (!Directory.Exists(sourceDirectoryName)) { throw new DirectoryNotFoundException(sourceDirectoryName); } destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName); using (ZipArchive destination = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding)) { bool flag = true; DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectoryName); string directoryFullName = directoryInfo.FullName; if (includeBaseDirectory && directoryInfo.Parent != null) { directoryFullName = directoryInfo.Parent.FullName; } List <FileSystemInfo> list = new List <FileSystemInfo>(); if (includeSubDirectories) { list.AddRange(directoryInfo.GetDirectories("*", SearchOption.AllDirectories)); list.AddRange(directoryInfo.GetFiles("*", SearchOption.AllDirectories)); } else { list.AddRange(directoryInfo.GetFiles("*", SearchOption.TopDirectoryOnly)); } foreach (FileSystemInfo fileSystemInfo in list) { flag = false; int length = fileSystemInfo.FullName.Length - directoryFullName.Length; string entryName = fileSystemInfo.FullName.Substring(directoryFullName.Length, length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); if (fileSystemInfo is FileInfo) { destination.CreateEntryFromFile(fileSystemInfo.FullName, entryName); } else { DirectoryInfo possiblyEmptyDir = fileSystemInfo as DirectoryInfo; if (possiblyEmptyDir != null && ZipHelper.IsDirEmpty(possiblyEmptyDir)) { destination.CreateEntryFromDirectory(possiblyEmptyDir.FullName, entryName); } } } if (includeBaseDirectory & flag) { destination.CreateEntryFromDirectory(directoryInfo.FullName, directoryInfo.Name); } } }