Esempio n. 1
0
        /// <summary>
        /// Creates a zip archive that contains the files and directories.
        /// </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="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>
        /// <param name="sourceFiles">The files to compress.</param>
        public static void CreateFromFiles(string destinationArchiveFileName, Encoding entryNameEncoding, params FileSystemInfo[] sourceFiles)
        {
            if (destinationArchiveFileName == null)
            {
                throw new ArgumentNullException("destinationArchiveFileName");
            }

            destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);

            if (sourceFiles == null)
            {
                sourceFiles = new FileSystemInfo[0];
            }

            using (ZipArchive destination = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding))
            {
                foreach (FileSystemInfo sourceFile in sourceFiles)
                {
                    if (sourceFile is FileInfo)
                    {
                        destination.CreateEntryFromFile(sourceFile.FullName, sourceFile.Name);
                    }
                    else
                    {
                        DirectoryInfo possiblyEmptyDir = sourceFile as DirectoryInfo;

                        if (possiblyEmptyDir != null && ZipHelper.IsDirEmpty(possiblyEmptyDir))
                        {
                            destination.CreateEntryFromDirectory(possiblyEmptyDir.FullName, sourceFile.Name);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a zip archive that contains the files and directories.
        /// </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="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>
        /// <param name="sourceFileNames">The files to compress.</param>
        public static void CreateFromFiles(string destinationArchiveFileName, Encoding entryNameEncoding, params string[] sourceFileNames)
        {
            if (destinationArchiveFileName == null)
            {
                throw new ArgumentNullException("destinationArchiveFileName");
            }

            destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);

            if (sourceFileNames == null)
            {
                sourceFileNames = new string[0];
            }

            using (ZipArchive destination = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding))
            {
                foreach (string sourceFileName in sourceFileNames)
                {
                    if (ZipHelper.IsFile(sourceFileName))
                    {
                        FileInfo sourceFile = new FileInfo(sourceFileName);
                        destination.CreateEntryFromFile(sourceFile.FullName, sourceFile.Name);
                    }
                    else
                    {
                        DirectoryInfo sourceFile = new DirectoryInfo(sourceFileName);
                        destination.CreateEntryFromDirectory(sourceFile.FullName, sourceFile.Name);
                    }
                }
            }
        }
Esempio n. 3
0
        private static void DoCreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, 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 fullName = directoryInfo.FullName;

                if (includeBaseDirectory && directoryInfo.Parent != null)
                {
                    fullName = 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 - fullName.Length;
                    string entryName = fileSystemInfo.FullName.Substring(fullName.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 + Path.DirectorySeparatorChar);
                        }
                    }
                }

                if (!includeBaseDirectory || !flag)
                {
                    return;
                }

                destination.CreateEntryFromDirectory(directoryInfo.FullName, directoryInfo.Name + Path.DirectorySeparatorChar);
            }
        }