Example #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);
                        }
                    }
                }
            }
        }
Example #2
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);
            }
        }
Example #3
0
        /// <summary>
        /// Archives the files and directories from the specified directory, uses the specified compression level, and optionally includes the base directory.
        /// </summary>
        /// <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>
        /// <exception cref="T:System.ArgumentException"><paramref name="sourceDirectoryName"/> is <see cref="F:System.String.Empty"/>, contains only white space, or contains at least one invalid character.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="sourceDirectoryName"/> is null.</exception>
        /// <exception cref="T:System.IO.PathTooLongException">In <paramref name="sourceDirectoryName"/> the specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must not exceed 248 characters, and file names must not exceed 260 characters.</exception>
        /// <exception cref="T:System.IO.DirectoryNotFoundException"><paramref name="sourceDirectoryName"/> is invalid or does not exist (for example, it is on an unmapped drive).</exception>
        /// <exception cref="T:System.NotSupportedException"><paramref name="sourceDirectoryName"/> contains an invalid format.-or-The zip archive does not support writing.</exception>
        public void CreateEntriesFromDirectory(string sourceDirectoryName, bool includeBaseDirectory, bool includeSubDirectories)
        {
            this.ThrowIfDisposed();

            if (sourceDirectoryName == null)
            {
                throw new ArgumentNullException("sourceDirectoryName");
            }

            if (this._mode == ZipArchiveMode.Read)
            {
                throw new NotSupportedException(CompressionConstants.CreateInReadMode);
            }

            sourceDirectoryName = Path.GetFullPath(sourceDirectoryName);

            if (!Directory.Exists(sourceDirectoryName))
            {
                throw new DirectoryNotFoundException(sourceDirectoryName);
            }

            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)
                {
                    this.CreateEntryFromFile(fileSystemInfo.FullName, entryName);
                }
                else
                {
                    DirectoryInfo possiblyEmptyDir = fileSystemInfo as DirectoryInfo;

                    if (possiblyEmptyDir != null && ZipHelper.IsDirEmpty(possiblyEmptyDir))
                    {
                        this.CreateEntryFromDirectory(possiblyEmptyDir.FullName, entryName + Path.DirectorySeparatorChar);
                    }
                }
            }

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

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