• SharpCompress压缩和解压缩,并解决压缩的中文乱码问题


    一.下载SharpCompress库

    二.解压缩

    (1)不带密码

            /// <summary>
            /// 解压缩(支持rar,zip)
            /// </summary>
            /// <param name="fromFilePath">待解压文件全路径</param>
            /// <param name="outFileDirectory">解压文件后目录</param>
            public static void Decompress(string fromFilePath, string outFileDirectory)
            {
                var archive = ArchiveFactory.Open(fromFilePath);
                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                    {
                        //Debug.WriteLine(entry.Key);
                        entry.WriteToDirectory(outFileDirectory, new ExtractionOptions { ExtractFullPath = true, Overwrite = true });
                    }
                }
            }
    

    (2)带密码

            /// <summary>
            /// 解压缩加密的包(支持rar,zip)
            /// </summary>
            /// <param name="fromFilePath">待解压文件全路径</param>
            /// <param name="outFileDirectory">解压文件后目录</param>
            /// <param name="password">密码</param>
            public static void DecompressWithPwd(string fromFilePath, string outFileDirectory, string password)
            {
                using (var archive = ArchiveFactory.Open(fromFilePath, new ReaderOptions { Password = password }))
                {
                    foreach (var entry in archive.Entries)
                    {
                        if (!entry.IsDirectory)
                        {
                            //Debug.WriteLine(entry.Key);
                            entry.WriteToDirectory(outFileDirectory, new ExtractionOptions { ExtractFullPath = true, Overwrite = true });
                        }
                    }
                }
            }

    三.压缩

     /// <summary>
            /// 压缩(zip格式)
            /// </summary>
            /// <param name="fromFileDirectory">待压缩目录</param>
            /// <param name="outFilePath">压缩后文全件路径</param>
            public static void ZipCompress(string fromFileDirectory, string outFilePath)
            {
                //解决中文乱码问题
                SharpCompress.Common.ArchiveEncoding ArchiveEncoding = new SharpCompress.Common.ArchiveEncoding();
                ArchiveEncoding.Default = Encoding.GetEncoding("utf-8");
                SharpCompress.Writers.WriterOptions options = new SharpCompress.Writers.WriterOptions(CompressionType.Deflate);
                options.ArchiveEncoding = ArchiveEncoding;
    
                using (var archive = ZipArchive.Create())
                {
                    archive.AddAllFromDirectory(fromFileDirectory);
                    using (var zip = File.OpenWrite(outFilePath))
                        archive.SaveTo(zip, options);
                }
            }
    

      

    四.调用

    //压缩
    string fromFileDirectory = @"C:	est我是目录";
    string outFilePath = @"C:压缩包1.zip";
    CompressHelper.ZipCompress(fromFileDirectory, outFilePath);
    
    //解压缩
    string fromFilePath = @"C:压缩包1.zip";
    string outFileDirectory = @"C:解压目录压缩包1";
    CompressHelper.Decompress(fromFilePath, outFileDirectory);
    

      

    五.中文乱码问题

    老版本直接加入下面一行代码就可以了

    SharpCompress.Common.ArchiveEncoding.Default = Encoding.GetEncoding ("utf-8");

    但是我使用的是0.23.0版本,.ArchiveEncoding已经从静态类改为普通类了,无法这样调用

    参考:https://github.com/adamhathcock/sharpcompress/issues/268

     最后发现:ArchiveEncoding类是OptionsBase的属性,而OptionsBase是WriterOptions的基类,ZipArchive的SaveTo方法带WriterOptions参数,那就ok了

    参考:

    wiki:https://github.com/adamhathcock/sharpcompress/wiki/API-Examples

    支持类型:https://github.com/adamhathcock/sharpcompress/wiki/Supported-Formats

  • 相关阅读:
    让c像python一样可以在命令行写代码并且编译
    动态链接库找不到 : error while loading shared libraries: libgsl.so.0: cannot open shared object file: No such file or directory
    为什么shell中变量赋值不能有空格
    在hyper安装openwrt
    linux扩展lvm磁盘
    docker的基本使用
    tmux与vim主题不一致
    linux centos cli all proxy
    couchDB入门
    tmux复制到windows剪贴板/粘贴板的坑
  • 原文地址:https://www.cnblogs.com/yaosj/p/10573571.html
Copyright © 2020-2023  润新知