欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

c# 文件壓縮zip或?qū)ip文件解壓的方法

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

1.必須Dll:

ICSharpCode.SharpZipLib.dll??蓮腘utget程序包中獲取。

2.壓縮文件

/// <summary>
/// 壓縮文件成zip
/// </summary>
/// <param name="fileZip">壓縮成zip文件的絕對(duì)路徑</param>
/// <param name="fileName">被壓縮指定文件的名字</param>
/// <param name="zipFilePath"></param>
/// <returns></returns>
public bool CreateZipFile(string fileZip,string fileName, string zipFilePath)
  {
   bool isZip = false;
   if (!Directory.Exists(zipFilePath))
   {
    Logger.Info($"Cannot find directory {zipFilePath}", false, "FileToZip");
    return isZip;
   }
   try
   {
    string[] filenames = Directory.GetFiles(zipFilePath);
    using (ZipOutputStream s = new ZipOutputStream(File.Create(fileZip)))
    {
     s.SetLevel(9); // 壓縮級(jí)別 0-9
     //s.Password = "123"; //Zip壓縮文件密碼
     byte[] buffer = new byte[4096]; //緩沖區(qū)大小
     foreach (string file in filenames.ToList())
     {
      if (file== zipFilePath+fileName)//指定被壓縮文件的絕對(duì)路徑
      {
       ZipEntry entry = new ZipEntry(Path.GetFileName(file));
       entry.DateTime = DateTime.Now;
       s.PutNextEntry(entry);
       using (FileStream fs = File.OpenRead(file))
       {
        int sourceBytes;
        do
        {
         sourceBytes = fs.Read(buffer, 0, buffer.Length);
         s.Write(buffer, 0, sourceBytes);
        } while (sourceBytes > 0);
        fs.Close();
        fs.Dispose();
       }
       break;
      }
     }
     s.Finish();
     s.Close();
     isZip = true;
    }
   }
   catch (Exception ex)
   {
    Logger.Info($"Exception during processing {0}", false, "FileToZip");
   }
   return isZip;
  }

3.將zip文件解壓

/// <summary>
/// 解壓文件
/// </summary>
/// <param name="zipFilePath">壓縮文件的絕對(duì)路徑</param>
 public void UnZipFile(string zipFilePath)
  {
   if (!File.Exists(zipFilePath))
   {
    Logger.Info($"Cannot find file {zipFilePath}", false, "FileToZip");
    return;
   }
   using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
   {
    ZipEntry theEntry;
    while ((theEntry = s.GetNextEntry()) != null)
    {
     string directoryName = Path.GetDirectoryName(theEntry.Name);
     string fileName = Path.GetFileName(theEntry.Name);
     // create directory
     if (directoryName?.Length > 0)
     {
      Directory.CreateDirectory(directoryName);
     }
     if (!string.IsNullOrEmpty(fileName))
     {
      using (FileStream streamWriter = File.Create(theEntry.Name))
      {
       int size = 2048;
       byte[] data = new byte[2048];
       while (true)
       {
        size = s.Read(data, 0, data.Length);
        if (size > 0)
        {
         streamWriter.Write(data, 0, size);
        }
        else
        {
         break;
        }
       }
      }
     }
    }
   }
  }

4.其它:其中的Logger是Log4的用法。

以上這篇c# 文件壓縮zip或?qū)ip文件解壓的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。

上一篇:c#可以創(chuàng)建任意控件的拖動(dòng)方法

欄    目:C#教程

下一篇:C#中枚舉的特性 FlagAttribute詳解

本文標(biāo)題:c# 文件壓縮zip或?qū)ip文件解壓的方法

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5243.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有