zoukankan      html  css  js  c++  java
  • C#文件的压缩和解压(ZIP)使用DotNetZip封装类操作zip文件(创建/读取/更新)实例

    需要引用Ionic.Zip命名空间

    DLL下载地址在这里:http://dotnetzip.codeplex.com/

    文件压缩

         /// <summary>
            /// 压缩ZIP文件
            /// 支持多文件和多目录,或是多文件和多目录一起压缩
            /// </summary>
            /// <param name="list">待压缩的文件或目录集合</param>
            /// <param name="strZipName">压缩后的文件名</param>
            /// <param name="IsDirStruct">是否按目录结构压缩</param>
            /// <returns>成功:true/失败:false</returns>
            public static bool CompressMulti(List<string> list, string strZipName, bool IsDirStruct)
            {
                try
                {
                    using (ZipFile zip = new ZipFile(Encoding.Default))//设置编码,解决压缩文件时中文乱码
                    {
                        foreach (string path in list)
                        {
                            string fileName = Path.GetFileName(path);//取目录名称
                            //如果是目录
                            if (Directory.Exists(path))
                            {
                                if (IsDirStruct)//按目录结构压缩
                                {
                                    zip.AddDirectory(path, fileName);
                                }
                                else//目录下的文件都压缩到Zip的根目录
                                {
                                    zip.AddDirectory(path);
                                }
                            }
                            if (File.Exists(path))//如果是文件
                            {
                                zip.AddFile(path);
                            }
                        }
                        zip.Save(strZipName);//压缩
                        return true;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }

    文件解压

         /// <summary>
            /// 解压ZIP文件
            /// </summary>
            /// <param name="strZipPath">待解压的ZIP文件</param>
            /// <param name="strUnZipPath">解压的目录</param>
            /// <param name="overWrite">是否覆盖</param>
            /// <returns>成功:true/失败:false</returns>
            public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
            {
                try
                {
                    ReadOptions options = new ReadOptions();
                    options.Encoding = Encoding.Default;//设置编码,解决解压文件时中文乱码
                    using (ZipFile zip = ZipFile.Read(strZipPath, options))
                    {
                        foreach (ZipEntry entry in zip)
                        {
                            if (string.IsNullOrEmpty(strUnZipPath))
                            {
                                strUnZipPath = strZipPath.Split('.').First();
                            }
                            if (overWrite)
                            {
                                entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解压文件,如果已存在就覆盖
                            }
                            else
                            {
                                entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解压文件,如果已存在不覆盖
                            }
                        }
                        return true;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
  • 相关阅读:
    codevs 1576 最长严格上升子序列
    codevs 3415 最小和
    codevs 2102 石子归并 2
    洛谷 P1040 加分二叉树
    BZOJ 3038 上帝造题的七分钟二
    codevs 线段树练习ⅠⅡⅢ
    启动Tomcat提示:指定的服务未安装
    poj 1061 青蛙的约会 (扩展欧几里得模板)
    POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)
    HDU 5251 矩形面积(二维凸包旋转卡壳最小矩形覆盖问题) --2015年百度之星程序设计大赛
  • 原文地址:https://www.cnblogs.com/a849788087/p/5645844.html
Copyright © 2011-2022 走看看