欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-17 12:34:28
下面给大家介绍c#使用icsharpcode.sharpziplib.dll进行文件的压缩与解压功能,具体代码如下所示: using system; using...

下面给大家介绍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#使用icsharpcode.sharpziplib.dll进行文件的压缩与解压功能,希望对大家有所帮助

上一篇:

下一篇: