老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)

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

這篇文章主要為大家展示了“C#如何使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C#如何使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能”這篇文章吧。

目前創(chuàng)新互聯(lián)建站已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、灌陽(yáng)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

具體代碼如下所示:

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
 {
  /// 
  /// 壓縮單個(gè)文件
  /// 
  /// 需壓縮的文件名
  /// 壓縮后的文件名(文件名都是絕對(duì)路徑)
  /// 壓縮等級(jí)(0-9)
  /// 壓縮密碼(解壓是需要的密碼)
  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);
     }
    }
   }
  }
  /// 
  /// 壓縮多個(gè)文件目錄
  /// 
  /// 需要壓縮的目錄
  /// 壓縮后的文件名
  /// 壓縮等級(jí)
  /// 密碼
  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();
  }
  /// 
  /// 往壓縮文件里面添加Entry
  /// 
  /// 文件路徑
  /// ZipOutputStream
  /// 基礎(chǔ)目錄
  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);
     }
    }
   }
  }
  /// 
  /// 解壓多個(gè)文件目錄
  /// 
  /// 壓縮文件絕對(duì)路徑
  /// 解壓文件目錄
  /// 密碼
  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();
  }

效果圖如下:

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

以上是“C#如何使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


當(dāng)前題目:C#如何使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能
地址分享:http://www.xueling.net.cn/article/piejjo.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP
主站蜘蛛池模板: 全部免费A片在线观看VR系列 | 色资源AV中文无码先锋 | 在线一区日韩 | 狠狠艹夜夜艹 | 天天人人| 一本一本久久A久久精品综合不卡 | 拍拍视频爽爽影院在线观看免费 | 一区二区三区高清在线 | 干片网在线观看 | 亚洲黄色a | 一级黄色片看看 | 色花堂国产精品第一页 | 国产一二三在线 | 黄色网址在线视频 | 毛茸茸xxxxx| 91亚洲国产成人精品一区二三 | 亚洲一区二区中文字幕在线观看 | 成人性视频欧美一区二区三区 | 中文字幕精品乱码中文字乱码 | 免费精品国产自产拍在线观看图片 | 91视视频在线观看入口直接观看 | 欧美日韩黄色大片 | 国产亚洲无线码一区二区 | 欧亚精品卡一卡二卡三 | 久久久久久久久福利 | 人妻大战黑人白浆狂泄 | 51嫩草亚洲精品永久 | freesexvideos性少妇kant | 天堂一区二区三区 | 国内精品久久99 | 亚洲国产一| 九色网站在线观看 | 国产sm免费视频专区 | 国产乡下妇女三片 | 黄色影片免费看 | 国产无套精品一区二区三区 | 免费国产精品久久久久久 | 凤隐天下60集全免费播放在线观看 | 日韩色性 | 黄页大全在线免费观看 | 男女啪啪免费观看无遮挡 |