欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能

 更新時(shí)間:2017年12月28日 10:12:38   作者:華臨天下  
這篇文章主要介紹了C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,需要的朋友可以參考下

下面給大家介紹C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,具體代碼如下所示:

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>
  /// 壓縮單個(gè)文件
  /// </summary>
  /// <param name="fileToZip">需壓縮的文件名</param>
  /// <param name="zipFile">壓縮后的文件名(文件名都是絕對(duì)路徑)</param>
  /// <param name="level">壓縮等級(jí)(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;//設(shè)置流的起始位置
    byte[] buffer = new byte[(int)fs.Length];
    fs.Read(buffer, 0, buffer.Length);//讀取的時(shí)候設(shè)置Position,寫(xiě)入的時(shí)候不需要設(shè)置
    fs.Close();
    using (FileStream zfstram = File.Create(zipFile))
    {
     using (ZipOutputStream zipstream = new ZipOutputStream(zfstram))
     {
      zipstream.Password = md5(password);//設(shè)置屬性的時(shí)候在PutNextEntry函數(shù)之前
      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>
  /// 壓縮多個(gè)文件目錄
  /// </summary>
  /// <param name="dirname">需要壓縮的目錄</param>
  /// <param name="zipFile">壓縮后的文件名</param>
  /// <param name="level">壓縮等級(jí)</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">基礎(chǔ)目錄</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)//如果是文件夾繼續(xù)遞歸
    {
     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>
  /// 解壓多個(gè)文件目錄
  /// </summary>
  /// <param name="zfile">壓縮文件絕對(duì)路徑</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('\\');//這邊判斷壓縮文件里面是否存在目錄,存在的話先創(chuàng)建目錄后繼續(xù)解壓
     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;
  }
 }
}

調(diào)用函數(shù)如下:

static void Main(string[] args)
  {
   var str = @"\學(xué)籍導(dǎo)入模板.xls";
   //var arr=str.Split('\\');
   var filePath = @"D:\程序文件\VS2010學(xué)習(xí)\StudyProgram\zip壓縮與解壓\File\學(xué)籍導(dǎo)入模板.xls";
   //ZipHelper.ZipFile(filePath, @"D:\程序文件\VS2010學(xué)習(xí)\StudyProgram\zip壓縮與解壓\File\test.zip", 6, "123");
   var dirPath = @"D:\程序文件\VS2010學(xué)習(xí)\StudyProgram\zip壓縮與解壓";
   //ZipHelper.ZipDir(dirPath + @"\File", dirPath + @"\File.zip", 6, "huage");
   ZipHelper.UnZip(dirPath + @"\File.zip", dirPath + @"\test", "huage");
   Console.ReadKey();
  }

效果圖如下:

總結(jié)

以上所述是小編給大家介紹的C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • C#中Mutex對(duì)象用法分析

    C#中Mutex對(duì)象用法分析

    這篇文章主要介紹了C#中Mutex對(duì)象用法,結(jié)合實(shí)例形式分析了Mutex對(duì)象的功能與線程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • C#基礎(chǔ)概念二十五問(wèn) 16-20

    C#基礎(chǔ)概念二十五問(wèn) 16-20

    C#基礎(chǔ)概念二十五問(wèn) 16-20...
    2007-04-04
  • async and await 的入門(mén)基礎(chǔ)操作

    async and await 的入門(mén)基礎(chǔ)操作

    本篇文章對(duì)async and await 的入門(mén)基礎(chǔ)操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#動(dòng)態(tài)繪制多條曲線的方法

    C#動(dòng)態(tài)繪制多條曲線的方法

    這篇文章主要為大家詳細(xì)介紹了C#動(dòng)態(tài)繪制多條曲線的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#?StartsWith?字符串的實(shí)例方法解析

    C#?StartsWith?字符串的實(shí)例方法解析

    這篇文章主要介紹了C#?StartsWith?字符串的實(shí)例方法,StartsWith?方法對(duì)于需要檢查字符串的前綴是否匹配特定模式的情況非常有用,你可以根據(jù)返回的布爾值,根據(jù)需要執(zhí)行相應(yīng)的邏輯操作,需要的朋友可以參考下
    2024-03-03
  • c#模擬銀行atm機(jī)示例分享

    c#模擬銀行atm機(jī)示例分享

    這篇文章主要介紹了c#模擬銀行atm機(jī)示例,實(shí)現(xiàn)了用戶登錄、用戶存款、用戶取款等功能,需要的朋友可以參考下
    2014-03-03
  • 詳解c# SpinWait

    詳解c# SpinWait

    這篇文章主要介紹了c# SpinWait的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c# 編程,感興趣的朋友可以了解下
    2020-10-10
  • C#管道式編程的介紹與實(shí)現(xiàn)

    C#管道式編程的介紹與實(shí)現(xiàn)

    這篇文章主要給大家介紹了關(guān)于C#管道式編程的介紹與實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Unity2021發(fā)布WebGL與網(wǎng)頁(yè)交互問(wèn)題的解決

    Unity2021發(fā)布WebGL與網(wǎng)頁(yè)交互問(wèn)題的解決

    本文主要介紹了Unity2021發(fā)布WebGL與網(wǎng)頁(yè)交互問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法

    關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法

    本篇文章小編為大家介紹,關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法。需要的朋友參考下
    2013-04-04

最新評(píng)論