• C# 压缩解压文件夹


    1. SharpZipLib方法

    添加引用

    using ICSharpCode.SharpZipLib.Checksum;
    using ICSharpCode.SharpZipLib.Zip;
    

    方法

    /// <summary>
    /// 压缩多层目录
    /// </summary>
    /// <param name="strDirectory">The directory.</param>
    /// <param name="zipedFile">The ziped file.</param>
    public static void ZipFileDirectory(string strDirectory, string zipedFile)
    {
        using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
        {
            using (ZipOutputStream s = new ZipOutputStream(ZipFile))
            {
                ZipSetp(strDirectory, s, "");
            }
        }
    }
    
    /// <summary>
    /// 递归遍历目录
    /// </summary>
    /// <param name="strDirectory">The directory.</param>
    /// <param name="s">The ZipOutputStream Object.</param>
    /// <param name="parentPath">The parent path.</param>
    private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
    {
        if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
        {
            strDirectory += Path.DirectorySeparatorChar;
        }
        Crc32 crc = new Crc32();
        string[] filenames = Directory.GetFileSystemEntries(strDirectory);
    
        foreach (string file in filenames)// 遍历所有的文件和目录
        {
            if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
            {
                string pPath = parentPath;
                pPath += file.Substring(file.LastIndexOf("\") + 1);
                pPath += "\";
                ZipSetp(file, s, pPath);
            }
    
            else // 否则直接压缩文件
            {
                //打开压缩文件
                using (FileStream fs = File.OpenRead(file))
                {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    string fileName = parentPath + file.Substring(file.LastIndexOf("\") + 1);
                    ZipEntry entry = new ZipEntry(fileName);
                    entry.IsUnicodeText = true;
                    entry.DateTime = DateTime.Now;
                    entry.Size = fs.Length;
    
                    fs.Close();
    
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                }
            }
        }
    }
    

    2. DotNetZip方法

    添加引用

    using Ionic.Zip;
    

    方法

     /// <summary>
    /// 使用DotNetZip压缩文件
    /// </summary>
    /// <param name="directooutputFileName">生成的ZIP文件目录</param>
    /// <param name="directory">待压缩的文件夹</param>
    public static void ZipFolder(string directooutputFileName, string directory)
    {
        ZipFile zip = new ZipFile(Encoding.UTF8);
        zip.AddDirectory(directory);
        zip.Save(directooutputFileName);
        zip.Dispose();
    }
    
    /// <summary>
    /// 使用DotNetZip解压文件
    /// </summary>
    /// <param name="path">待解压的文件</param>
    /// <param name="folder">解压到目录</param>
    public static void UnZipFoler(string path, string folder)
    {
        ReadOptions options = new ReadOptions { StatusMessageWriter = System.Console.Out };
        using (ZipFile zip = ZipFile.Read(path, options))
        {
            zip.ExtractAll(folder);
        }
    }
    
  • 相关阅读:
    【笔记】Hierarchical Attention Networks for Document Classification
    Chart Parser 中 Earley's 算法的应用
    使用 JFlex 生成词法分析器的安装配置及简单示例
    UNIX 系统下退出 git commit 编辑器
    SQL语法
    MySQL 和 Javaweb 的报错合集
    最短路径(SP)问题相关算法与模板
    dfs | Security Badges
    redis哨兵机制图谱
    docker笔记
  • 原文地址:https://www.cnblogs.com/yincq/p/13614156.html
Copyright © 2020-2023  润新知