C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能

 更新时间:2017年12月28日 10:12:38   作者:华临天下  
这篇文章主要介绍了C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能,需要的朋友可以参考下

下面给大家介绍C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能,具体代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.Security.Cryptography;
namespace zip压缩与解压
{
 public class ZipHelper
 {
  /// <summary>
  /// 压缩单个文件
  /// </summary>
  /// <param name="fileToZip">需压缩的文件名</param>
  /// <param name="zipFile">压缩后的文件名(文件名都是绝对路径)</param>
  /// <param name="level">压缩等级(0-9)</param>
  /// <param name="password">压缩密码(解压是需要的密码)</param>
  public static void ZipFile(string fileToZip, string zipFile, int level = 5, string password = "123")
  {
   if (!File.Exists(fileToZip))
    throw new FileNotFoundException("压缩文件" + fileToZip + "不存在");
   using (FileStream fs = File.OpenRead(fileToZip))
   {
    fs.Position = 0;//设置流的起始位置
    byte[] buffer = new byte[(int)fs.Length];
    fs.Read(buffer, 0, buffer.Length);//读取的时候设置Position,写入的时候不需要设置
    fs.Close();
    using (FileStream zfstram = File.Create(zipFile))
    {
     using (ZipOutputStream zipstream = new ZipOutputStream(zfstram))
     {
      zipstream.Password = md5(password);//设置属性的时候在PutNextEntry函数之前
      zipstream.SetLevel(level);
      string fileName = fileToZip.Substring(fileToZip.LastIndexOf('\\') + 1);
      ZipEntry entry = new ZipEntry(fileName);
      zipstream.PutNextEntry(entry);
      zipstream.Write(buffer, 0, buffer.Length);
     }
    }
   }
  }
  /// <summary>
  /// 压缩多个文件目录
  /// </summary>
  /// <param name="dirname">需要压缩的目录</param>
  /// <param name="zipFile">压缩后的文件名</param>
  /// <param name="level">压缩等级</param>
  /// <param name="password">密码</param>
  public static void ZipDir(string dirname, string zipFile, int level = 5, string password = "123")
  {
   ZipOutputStream zos = new ZipOutputStream(File.Create(zipFile));
   zos.Password = md5(password);
   zos.SetLevel(level);
   addZipEntry(dirname, zos, dirname);
   zos.Finish();
   zos.Close();
  }
  /// <summary>
  /// 往压缩文件里面添加Entry
  /// </summary>
  /// <param name="PathStr">文件路径</param>
  /// <param name="zos">ZipOutputStream</param>
  /// <param name="BaseDirName">基础目录</param>
  private static void addZipEntry(string PathStr, ZipOutputStream zos, string BaseDirName)
  {
   DirectoryInfo dir = new DirectoryInfo(PathStr);
   foreach (FileSystemInfo item in dir.GetFileSystemInfos())
   {
    if ((item.Attributes & FileAttributes.Directory) == FileAttributes.Directory)//如果是文件夹继续递归
    {
     addZipEntry(item.FullName, zos, BaseDirName);
    }
    else
    {
     FileInfo f_item = (FileInfo)item;
     using (FileStream fs = f_item.OpenRead())
     {
      byte[] buffer = new byte[(int)fs.Length];
      fs.Position = 0;
      fs.Read(buffer, 0, buffer.Length);
      fs.Close();
      ZipEntry z_entry = new ZipEntry(item.FullName.Replace(BaseDirName, ""));
      zos.PutNextEntry(z_entry);
      zos.Write(buffer, 0, buffer.Length);
     }
    }
   }
  }
  /// <summary>
  /// 解压多个文件目录
  /// </summary>
  /// <param name="zfile">压缩文件绝对路径</param>
  /// <param name="dirname">解压文件目录</param>
  /// <param name="password">密码</param>
  public static void UnZip(string zfile, string dirname, string password)
  {
   if (!Directory.Exists(dirname)) Directory.CreateDirectory(dirname);
   using (ZipInputStream zis = new ZipInputStream(File.OpenRead(zfile)))
   {
    zis.Password = md5(password);
    ZipEntry entry;
    while ((entry = zis.GetNextEntry()) != null)
    {
     var strArr = entry.Name.Split('\\');//这边判断压缩文件里面是否存在目录,存在的话先创建目录后继续解压
     if (strArr.Length > 2)  
      Directory.CreateDirectory(dirname + @"\" + strArr[1]);
     using (FileStream dir_fs = File.Create(dirname + entry.Name))
     {
      int size = 1024 * 2;
      byte[] buffer = new byte[size];
      while (true)
      {
       size = zis.Read(buffer, 0, buffer.Length);
       if (size > 0)
        dir_fs.Write(buffer, 0, size);
       else
        break;
      }
     }
    }
   }
  }
  private static string md5(string pwd)
  {
   var res = "";
   MD5 md = MD5.Create();
   byte[] s = md.ComputeHash(Encoding.Default.GetBytes(pwd));
   for (int i = 0; i < s.Length; i++)
    res = res + s[i].ToString("X");
   return res;
  }
 }
}

调用函数如下:

static void Main(string[] args)
  {
   var str = @"\学籍导入模板.xls";
   //var arr=str.Split('\\');
   var filePath = @"D:\程序文件\VS2010学习\StudyProgram\zip压缩与解压\File\学籍导入模板.xls";
   //ZipHelper.ZipFile(filePath, @"D:\程序文件\VS2010学习\StudyProgram\zip压缩与解压\File\test.zip", 6, "123");
   var dirPath = @"D:\程序文件\VS2010学习\StudyProgram\zip压缩与解压";
   //ZipHelper.ZipDir(dirPath + @"\File", dirPath + @"\File.zip", 6, "huage");
   ZipHelper.UnZip(dirPath + @"\File.zip", dirPath + @"\test", "huage");
   Console.ReadKey();
  }

效果图如下:

总结

以上所述是小编给大家介绍的C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • c#菜单动态合并的实现方法

    c#菜单动态合并的实现方法

    这篇文章主要介绍了c#菜单动态合并的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • 举例说明Java多线程编程中读写锁的使用

    举例说明Java多线程编程中读写锁的使用

    这篇文章主要介绍了举例说明Java多线程编程中读写锁的使用,文中的例子很好地说明了Java的自带读写锁ReentrantReadWriteLock的使用,需要的朋友可以参考下
    2016-02-02
  • C#文件操作类分享

    C#文件操作类分享

    这篇文章主要为大家分享了C#文件操作类的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#判断指定驱动器是否是Fat分区格式的方法

    C#判断指定驱动器是否是Fat分区格式的方法

    这篇文章主要介绍了C#判断指定驱动器是否是Fat分区格式的方法,涉及C#中DriveFormat属性的使用技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • c#分页显示服务器上指定目录下的所有图片示例

    c#分页显示服务器上指定目录下的所有图片示例

    这篇文章主要介绍了c#分页显示服务器上指定目录下的所有图片示例,需要的朋友可以参考下
    2014-05-05
  • C#匿名函数和匿名方法的使用

    C#匿名函数和匿名方法的使用

    本文主要介绍了C#匿名函数和匿名方法的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • C#数据结构之顺序表(SeqList)实例详解

    C#数据结构之顺序表(SeqList)实例详解

    这篇文章主要介绍了C#数据结构之顺序表(SeqList)实现方法,结合实例形式较为详细的分析了顺序表的定义、原理与具体实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • C#快速排序算法实例分析

    C#快速排序算法实例分析

    这篇文章主要介绍了C#快速排序算法,实例分析了C#排序方法的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#的winform如何嵌套另一个exe程序

    C#的winform如何嵌套另一个exe程序

    这篇文章主要介绍了C#的winform如何嵌套另一个exe程序问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 使用C#的正则表达式验证中文字符(实例代码)

    使用C#的正则表达式验证中文字符(实例代码)

    本文通过实例代码给大家介绍了使用C#的正则表达式验证中文字符的方法,需要的的朋友参考下吧
    2017-07-07

最新评论