zoukankan      html  css  js  c++  java
  • c# 用DotNetZip来解压/压缩文件

    //https://archive.codeplex.com/?p=dotnetzip
    //最新在Nuget 下载DotNetZip
    
    using Ionic.Zip;
    
    
    private void button1_Click(object sender, EventArgs e)
            {
                string msg = "";
                ExtractFile("D:\1.zip", "d:\OK\");
                ExtractFile("D:\1.zip", "d:\OK\", false, out msg);
            }
    
    
    
            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 = "123";
                        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 targetFolderPath, Boolean isUsePassword, out String errMessage)
            {
                try
                {
                    Encoding encoding = Encoding.UTF8;
                    errMessage = String.Empty;
                    var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding };
                    using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options))
                    {
                        if (isUsePassword)
                        { //假设有密码123
                            zip.Password = "123";
                        }
                        zip.AlternateEncoding = encoding;
                        zip.ExtractAll(targetFolderPath, ExtractExistingFileAction.OverwriteSilently);//一次批量解压
                        return true;
                    }
                }
                catch (Exception ex) { errMessage = ex.Message; return false; }
            }
    
    
    
    
            public static bool ExtractFile(String sourceFileFullPath, String targetFolderPath)
            {
                try
                {
                    Encoding encoding = Encoding.UTF8;
    
                    var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding };
                    using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options))
                    {
                        zip.AlternateEncoding = encoding;
                        foreach (var f in zip.Entries)
                        {//一个个解压
                            f.Extract(targetFolderPath, ExtractExistingFileAction.OverwriteSilently);
                        }
    
                        return true;
                    }
                }
                catch (Exception ex)
                {
    
                    return false;
                }
            }
    

      

  • 相关阅读:
    java中子类与父类中隐含的this引用的分析
    c++中基类与派生类中隐含的this指针的分析
    c++中关于初始化型参列表的一些问题
    2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)
    2014牡丹江网络赛ZOJPretty Poem(暴力枚举)
    poj1949Chores(建图或者dp)
    poj 1950 Dessert(dfs枚举,模拟运算过程)
    java中自动装箱的问题
    hdu4292Food(最大流Dinic算法)
    codeforces Gargari and Permutations(DAG+BFS)
  • 原文地址:https://www.cnblogs.com/wgscd/p/10869090.html
Copyright © 2011-2022 走看看