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);
    }
}
posted @ 2020-09-04 15:30  柠檬山竹大西瓜  阅读(334)  评论(0编辑  收藏  举报