.net 压缩文件夹和解压

1、使用 ICSharpCode.SharpZipLib 压缩还原可以实现功能,但在发布程序或者调试程序时报“不能加载这个dll库

2、使用 System.IO.Compression.FileSystem.dll 也可以实现,不过这是点Net 4.5 版本才能使用

3、还是第三种比较好用:使用 WindowsBase.dll 压缩和解压

添加引用:Windowsbase;使用命名空间 using System.IO.Packaging;

/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="SourceCompressFolder">要压缩的文件夹</param>
/// <param name="CompressFileName">压缩文件名称</param>
public static bool CompressFile(string SourceCompressFolder, string CompressFileName, bool IsOverride)
{
  if (SourceCompressFolder.EndsWith(@""))
    SourceCompressFolder = SourceCompressFolder.Remove(SourceCompressFolder.Length - 1);
  bool result = false;
  if (!Directory.Exists(SourceCompressFolder))
    return result;
  if (!IsOverride && File.Exists(CompressFileName))
    return result;
  try
  {
    using (Package package = Package.Open(CompressFileName, FileMode.Create))
    {
      var fileList = Directory.EnumerateFiles(SourceCompressFolder, "*", SearchOption.AllDirectories);
      foreach (string fileName in fileList)
      {
        string pathInPackage;
        pathInPackage = Path.GetDirectoryName(fileName).Replace(SourceCompressFolder, string.Empty) + "/" + Path.GetFileName(fileName);

        Uri partUriDocument = PackUriHelper.CreatePartUri(new Uri(pathInPackage, UriKind.Relative));
        PackagePart packagePartDocument = package.CreatePart(partUriDocument, "", CompressionOption.Maximum);
        using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
          fileStream.CopyTo(packagePartDocument.GetStream());
        }
      }
    }
    result = true;
  }
  catch (Exception e)
  {
    throw new Exception("Error zipping folder " + SourceCompressFolder, e);
  }
  return result;
}

/// <summary>
/// 解压文件夹
/// </summary>
/// <param name="TargetFolder">目标文件夹</param>
/// <param name="CompressFile">压缩包名称</param>
/// <param name="overrideExisting">是否替换</param>
/// <returns></returns>
public static bool UncompressFile(string TargetFolder, string CompressFile, bool  overrideExisting)
{
  bool result = false;
  try
  {
    if (!File.Exists(CompressFile))
      return  result;

    DirectoryInfo directoryInfo = new  DirectoryInfo(TargetFolder);
    if (!directoryInfo.Exists)
      directoryInfo.Create();

    using (Package package = Package.Open(CompressFile, FileMode.Open, FileAccess.Read))
    {
      foreach (PackagePart packagePart in package.GetParts())
      {
        ExtractPart(packagePart, TargetFolder, overrideExisting);
      }
    }

    result = true;
  }
  catch (Exception e)
  {
    throw  new Exception("Error unzipping file " + CompressFile, e);
  }

  return result;
}

public static void ExtractPart(PackagePart packagePart, string targetDirectory, bool overrideExisting)
{
  string stringPart = targetDirectory + packagePart.Uri.ToString();

  if (!Directory.Exists(Path.GetDirectoryName(stringPart)))
    Directory.CreateDirectory(Path.GetDirectoryName(stringPart));

  if (!overrideExisting && File.Exists(stringPart))
    return;
  using (FileStream fileStream = new  FileStream(stringPart, FileMode.Create))
  {
    packagePart.GetStream().CopyTo(fileStream);
  }
}

原文地址:https://www.cnblogs.com/wangye520/p/8609872.html