zoukankan      html  css  js  c++  java
  • 关于DotNetZip的用法

    公司有个项目要求解压缩操作,一直在使用WinRar和Gzip一个有版权问题,一个效率存在问题,因此决定找一个开源高效的第三方插件。这个时候DotNetZip映入眼帘,他不仅仅能压缩,还能分卷压缩,对中文的至此也是很好。但这些都是在网络上查找时大家说的,基本没见代码如何写,DotNetZip本身使用简单,DLL也不是很大,所以我就使用到项目内。但在使用的过程中发现分卷压缩到底如何实现?解压中文依旧有问题?找了很多资料居然没这个方面的东西,没办法只好去官方查找资料。官方地址:http://dotnetzip.codeplex.com/

    在官方看到一些信息不过没看到关于分卷压缩的内容,只好一点一点看属性,个人感觉分卷压缩就是给他一个分卷值,之后在设定一个属性告诉他要分卷压缩即可,黄天不负有心人我找到分卷压缩的属性了。MaxOutputSegmentSize这个属性就是分卷压缩值,设置他的大小即可,他的单位是Kb。

    解压的时候对中文至此不是很好,找了好久也不知道如何解决,后来在坛子里发帖,也没得到很好的解答,官方的示例代码是这样写道

    using (ZipFile zip = new ZipFile())
    {
      zip.Encoding = System.Text.Encoding.GetEncoding("big5"); // chinese
      zip.AddDirectory(@"MyDocumentsProjectX");
      zip.Save(ZipFileToCreate);
    }

    可这个Encoding属性在1.9.1.8这个版本已经不存在了,所以针对中文乱码的问题依旧。找了好多文章看也没有这个版本如何解决的,最主要的是这个DLL的资料比较少。后来实在没有办法就去看看了源代码,终于找到了他的最新写法,好了废话不多说了直接看如何支持分卷压缩和中文不乱那么的代码吧,算是给自己提醒也是和大家分享下。

     public static bool ZipFolder(
                String sourceFilePath,
                String targetFileFullPath,
                Boolean isUsePassword,
                Int32 maxOutputSegmentSiez,
                out String errMessage)
            {
                try
                {
                    using (ZipFile zip = new ZipFile(Encoding.Default))
                    {
                        errMessage = String.Empty;
                        zip.Comment = "压缩文件时间" + System.DateTime.Now.ToString("G");
                        zip.Name = Guid.NewGuid().ToString().ToUpper() + ".zip";
                        if (isUsePassword)
                            zip.Password = D7.Base.ConstParameter.ZipPassWord;
                        zip.MaxOutputSegmentSize = maxOutputSegmentSiez * 1000;
                        zip.BufferSize = 1024;
                        zip.CaseSensitiveRetrieval = true;
                        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                        zip.AddDirectory(sourceFilePath);
                        zip.Save(targetFileFullPath);
                        return true;
                    }
                }
                catch (Exception ex) { errMessage = ex.Message; return false; }
            }

    public static bool ExtractFile(String sourceFileFullPath,
                String targetFilePath,
                out String errMessage)
            {
                return ExtractFile(sourceFileFullPath, targetFilePath, true, out errMessage);
            }


            public static bool ExtractFile(String sourceFileFullPath,
                String targetFilePath,
                Boolean isUsePassword,
                out String errMessage)
            {
                try
                {                
                    Encoding encoding = Utils.GetEncodingType(sourceFileFullPath);


                    errMessage = String.Empty;


                    var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding };


                    using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options))
                    {
                        if (isUsePassword)
                            zip.Password = D7.Base.ConstParameter.ZipPassWord;                    
                        zip.AlternateEncoding = encoding;                    
                        zip.ExtractAll(targetFilePath, ExtractExistingFileAction.OverwriteSilently);
                        return true;
                    }
                }
                catch (Exception ex) { errMessage = ex.Message; return false; }
            }

    这2个方法就是引用了Ionic.Zip.dll 1.9.1.8版本写的,DLL下载地址是http://dotnetzip.codeplex.com/releases/view/68268

    好了,解压和压缩都有了,可以分卷,可以加密码,支持中文。代码红色区域是如何支持中文的,其中Encoding encoding = Utils.GetEncodingType(sourceFileFullPath);

    使用了一个小方法获取文件的内部编码,之后在赋值给DotNetZip即可,代码网上有很多不贴了。

  • 相关阅读:
    spring boot RESTfuldemo测试类
    再谈Redirect(客户端重定向)和Dispatch(服务器端重定向)
    HTTP协议解析
    HTTP协议详解(真的很经典)
    JMeter进行简单的数据库(mysql)压力测试
    LoadRunner利用ODBC编写MySql脚本
    性能瓶颈的分析
    bug的处理流程
    Loadrunner11 录制手机App脚本多种方法介绍
    利用fiddler录制脚本
  • 原文地址:https://www.cnblogs.com/feb9903/p/3495260.html
Copyright © 2011-2022 走看看