• .NET 自带的压缩和解压【转】


    System.IO.Compression命名空间包含如下的基本的文件和流压缩和解压缩服务的类型, 还可以使用这些类型来读取和修改压缩文件的内容,主要有以下类:

    ZipFile ,ZipArchive , ZipArchiveEntry ,DeflateStream , GZipStream 

    1.使用 ZipFile 类创建和提取有一个 .zip 文件扩展名的压缩文件:必须引用System.IO.Compression.FileSystem 程序集。

     
    using System;
    using System.IO;
    using System.IO.Compression;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                string startPath = @"c:examplestart";
                string zipPath = @"c:example
    esult.zip";
                string extractPath = @"c:exampleextract";
    
                ZipFile.CreateFromDirectory(startPath, zipPath);
    
                ZipFile.ExtractToDirectory(zipPath, extractPath);
            }
        }
    }
    

      

    2.通过现存的.zip文件的内容重复存档并提取有一个 .txt 扩展名的文件。 它使用 ZipArchive 类访问现有的 .zip 文件与 ZipArchiveEntry 类来检查压缩文件的各个项,必须引用项目的System.IO.Compression.FileSystem 程序集。
    using System;
    using System.IO;
    using System.IO.Compression;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string zipPath = @"c:examplestart.zip";
                string extractPath = @"c:exampleextract";
    
                using (ZipArchive archive = ZipFile.OpenRead(zipPath))
                {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                        {
                            entry.ExtractToFile(Path.Combine(extractPath, entry.FullName));
                        }
                    }
                } 
            }
        }
    }
    
    3.使用 ZipArchive 类访问现有的 .zip 文件,然后添加新文件到压缩文件。 当添加到现有的 .zip 文件时,新文件获取压缩。
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (FileStream zipToOpen = new FileStream(@"c:usersexampleuser
    elease.zip", FileMode.Open))
                {
                    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                    {
                        ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
                        using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                        {
                                writer.WriteLine("Information about this package.");
                                writer.WriteLine("========================");
                        }
                    }
                }
            }
        }
    }
    

      

    4.使用 GZipStream 和 DeflateStream 类压缩和解压缩数据。 它们使用相同的压缩算法。 写入文件扩展名为 .gz 的压缩GZipStream 对象可以通过使用许多常用工具,除了 GZipStream提供的方法之外,被解压

    public class Program
        {
            private static string directoryPath = @"c:	emp";
            public static void Main()
            {
                DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
                Compress(directorySelected);
    
                foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
                {
                    Decompress(fileToDecompress);
                }
            }
    
            public static void Compress(DirectoryInfo directorySelected)
            {
                foreach (FileInfo fileToCompress in directorySelected.GetFiles())
                {
                    using (FileStream originalFileStream = fileToCompress.OpenRead())
                    {
                        if ((File.GetAttributes(fileToCompress.FullName) & 
                           FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                        {
                            using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                            {
                                using (GZipStream compressionStream = new GZipStream(compressedFileStream, 
                                   CompressionMode.Compress))
                                {
                                    originalFileStream.CopyTo(compressionStream);
    
                                }
                            }
                            FileInfo info = new FileInfo(directoryPath + "\" + fileToCompress.Name + ".gz");
                            Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                            fileToCompress.Name, fileToCompress.Length.ToString(), info.Length.ToString());
                        }
    
                    }
                }
            }
    
            public static void Decompress(FileInfo fileToDecompress)
            {
                using (FileStream originalFileStream = fileToDecompress.OpenRead())
                {
                    string currentFileName = fileToDecompress.FullName;
                    string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
    
                    using (FileStream decompressedFileStream = File.Create(newFileName))
                    {
                        using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                        {
                            decompressionStream.CopyTo(decompressedFileStream);
                            Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                        }
                    }
                }
            }
    

      

    【原文出处】 http://www.51aras.com/?id=6 
        
  • 相关阅读:
    编译gcc报错make[3]: Leaving directory `/usr/local/src/gcc-7.4.0/build/gcc' make[2]: *** [all-stage1-gcc] Error 2 处理
    ERROR 1176 (42000): Key 'XXX' doesn't exist in table 'XXX'报错处理
    /lib64/libc.so.6: version `GLIBC_2.18' not found报错解决
    Centos7上pkg-config的安装
    ERROR: Error in Log_event::read_log_event(): 'Found invalid event in binary log', data_len: 31, event_type: 35报错处理
    MySQL5.7主从复制slave报Last_Errno: 1146错误解决
    详述 hosts 文件的作用及修改 hosts 文件的方法
    Java Decompiler(Java反编译工具)
    使用Charles代理工具导致电脑无法正常访问网站(您的连接不是私密连接)
    阿里云服务器Svn-Server无法连接,阿里云服务器SVNServer配置
  • 原文地址:https://www.cnblogs.com/61007257Steven/p/11080982.html
Copyright © 2020-2023  润新知