通過C#代碼 壓縮/解壓檔案

來源:互聯網
上載者:User

通過引用一DLL(ICSharpCode.dll)可以實現所述功能。。。

一、壓縮檔

using System;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections;

namespace wsUpFiles
{
 /// <summary>
 /// Common 的摘要說明。
 /// </summary>
 public class Common
 {
  public Common()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
  }

  /// <summary>
  /// 壓縮檔
  /// </summary>
  /// <param name="sourceFileNames">壓縮檔名稱集合</param>
  /// <param name="destFileName">壓縮後檔案名稱</param>
  /// <param name="password">密碼</param>
  public static void zipFile(string path,string destFileName)
  {
   Crc32 crc = new Crc32();
   ZipOutputStream s = new ZipOutputStream(File.Create(destFileName));
   s.Password="";
   s.SetLevel(6); // 0 - store only to 9 - means best compression
   //定義
   System.IO.DirectoryInfo myDir = new DirectoryInfo(path);
   if(myDir.Exists == true)
   {
    System.IO.FileInfo[] myFileAry = myDir.GetFiles();
    
    //迴圈提取檔案夾下每一個檔案,提取資訊,
    foreach (FileInfo objFiles in myFileAry)
    {
     FileStream fs = File.OpenRead(objFiles.FullName);
     byte[] buffer = new byte[fs.Length];
     fs.Read(buffer, 0, buffer.Length);
     ZipEntry entry = new ZipEntry(objFiles.FullName);
     entry.DateTime = DateTime.Now;
     entry.Size = fs.Length;
     fs.Close();
     crc.Reset();
     crc.Update(buffer);
     entry.Crc  = crc.Value;
     s.PutNextEntry(entry);
     s.Write(buffer, 0, buffer.Length);           
    }   
    s.Finish();
    s.Close();
   }
  }
  
 }
}

要對壓縮檔加密時,要s.Password = "aaa"; aaa為密碼。

二、解壓檔案

 /// <summary>
  /// 解壓檔案
  /// </summary>
  /// <param name="sourceFileName">被解壓檔案名稱</param>
  /// <param name="destPath">解壓後檔案目錄</param>
  /// <param name="password">密碼</param>
  public static void unzipFile(string sourceFileName,string destPath,string fileType)
  {
   ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFileName));
   ZipEntry theEntry;
   ArrayList al=new ArrayList();

   while ((theEntry = s.GetNextEntry()) != null)
   {   
    string fileName=Path.GetFileName(theEntry.Name);
    if(fileName!="")
    {
     fileName=destPath+"\\"+fileName;
     if(!Directory.Exists(destPath))
     {
      Directory.CreateDirectory(destPath);
     }
     FileStream streamWriter = File.Create(fileName);     
     int size = 2048;
     byte[] data = new byte[2048];
     s.Password="";
     while (true)
     {
      size = s.Read(data, 0, data.Length);
      if (size > 0)
      {
       streamWriter.Write(data, 0, size);
      }
      else
      {
       break;
      }
     }
     streamWriter.Close();
    }

   }
   s.Close();
   
  }

注意:程式的壓縮過的檔案,要通過系統上的工具解壓出來的路徑會相當多,因其在壓縮時保留了原來的絕對路徑,但壓縮的檔案中只包含所壓縮的目標檔案,當用程式解壓出來的檔案是相對的檔案路徑。


轉:http://blog.csdn.net/jinru2560/archive/2006/01/12/577493.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.