示例#1
0
        public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName)
        {
            if (!Directory.Exists(destinationDirectoryName))
            {
                Directory.CreateDirectory(destinationDirectoryName);
            }

            using (var arc = MyZipArchive.OpenOnFile(sourceArchiveFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                foreach (var fileName in arc.FileNames)
                {
                    using (var inStream = arc.GetFile(fileName).GetStream(FileMode.Open, FileAccess.Read))
                    {
                        var fullFilePath      = Path.Combine(destinationDirectoryName, fileName);
                        var fullDirectoryPath = Path.GetDirectoryName(fullFilePath);
                        if (!Directory.Exists(fullDirectoryPath))
                        {
                            Directory.CreateDirectory(fullDirectoryPath);
                        }

                        using (var outStream = File.Open(fullFilePath, FileMode.Create, FileAccess.Write))
                        {
                            inStream.CopyTo(outStream, 0x1000);
                        }
                    }
                }
            }
        }
示例#2
0
        public static void CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, DeflateOptionEnum compressionLevel, bool includeBaseDirectory, string[] ignoredExtensions = null, bool includeHidden = true)
        {
            // Some bug in ZipArchive throws exception when file already exists and we try to create it.
            if (File.Exists(destinationArchiveFileName))
            {
                File.Delete(destinationArchiveFileName);
            }

            int len = sourceDirectoryName.Length + 1;

            using (var arc = MyZipArchive.OpenOnFile(destinationArchiveFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                var rootDir = new DirectoryInfo(sourceDirectoryName);
                foreach (var fileInfo in rootDir.GetFiles("*", SearchOption.AllDirectories))
                {
                    if (!includeHidden && IsHidden(fileInfo, rootDir))
                    {
                        continue;
                    }

                    var file = fileInfo.FullName;
                    if (ignoredExtensions != null && ignoredExtensions.Contains(Path.GetExtension(file), StringComparer.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }
                    using (var inStream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        using (var outStream = arc.AddFile(file.Substring(len), CompressionMethodEnum.Deflated, compressionLevel).GetStream(FileMode.Open, FileAccess.Write))
                        {
                            inStream.CopyTo(outStream, 0x1000);
                        }
                    }
                }
            }
        }