.net 2.0 C#实现压缩/解压功能实现

需要引入ICSharpCode控件

1.压缩类

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace WindowsApplication1
{
 ///


 /// ZipClass の概要の説明です。
 ///

 public class ZipClass
 {
  public ZipClass()
  {
   //
   // TODO: コンストラクタ ロジックをここに追加してください。
   //
  }

  public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
  {
   //如果文件没有找到,???
   if (!System.IO.File.Exists(FileToZip))
   {
    throw new System.IO.FileNotFoundException("指定要??的文件: " + FileToZip + " 不存在!");
   }

   System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
   System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
   ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
   ZipEntry ZipEntry = new ZipEntry("ZippedFile");
   ZipStream.PutNextEntry(ZipEntry);
   ZipStream.SetLevel(CompressionLevel);
   byte[] buffer = new byte[BlockSize];
   System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
   ZipStream.Write(buffer, 0, size);
   try
   {
    while (size < StreamToZip.Length)
    {
     int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
     ZipStream.Write(buffer, 0, sizeRead);
     size += sizeRead;
    }
   }
   catch (System.Exception ex)
   {
    throw ex;
   }
   ZipStream.Finish();
   ZipStream.Close();
   StreamToZip.Close();
  }

  public static void ZipFileDictory(string[] args)
  {
   string[] filenames = Directory.GetFiles(args[0]);
   

   Crc32 crc = new Crc32();
   ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));           
   s.SetLevel(6);
   //FileInfo fi=new FileInfo();
  
   foreach (string file in filenames)
   { string file1;
    //打???文件
    FileStream fs = File.OpenRead(file);
    file1=file.Substring (file.LastIndexOf('//')+1);     //添加
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    ZipEntry entry = new ZipEntry(file1);

    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();
  }

  ///


  /// ??文件
  ///

  /// 要?行??的文件名
  /// ??后生成的??文件名
  public static void ZipFile(string FileToZip, string ZipedFile)
  {
   //如果文件没有找到,???
   if (!File.Exists(FileToZip))
   {
    throw new System.IO.FileNotFoundException("指定要??的文件: " + FileToZip + " 不存在!");
   }           
   FileStream fs = File.OpenRead(FileToZip);
   byte[] buffer = new byte[fs.Length];
   fs.Read(buffer, 0, buffer.Length);
   fs.Close();

   FileStream ZipFile = File.Create(ZipedFile);
   ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
   ZipEntry ZipEntry = new ZipEntry("LogBackUp.csv");
   ZipStream.PutNextEntry(ZipEntry);
   ZipStream.SetLevel(6);
       
   ZipStream.Write(buffer, 0, buffer.Length);           
   ZipStream.Finish();
   ZipStream.Close();
  }


 }
}
2.解压类

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace WebApplication3
{
 ///


 /// UnZipClass の概要の説明です。
 ///

 public class UnZipClass: System.Web.UI.Page
 {
  public UnZipClass()
  {
   //
   // TODO: コンストラクタ ロジックをここに追加してください。
   //
  }
  ///


  /// 解?功能(解???文件到指定目?)
  ///

  /// 待解?的文件
  /// 指定解?目?目?
  public static void UnZip(string[] args)
  {
   ZipInputStream s = new ZipInputStream(File.OpenRead(@args[0].Trim()));           
   ZipEntry theEntry;
   string directoryName = Path.GetDirectoryName(@args[1].Trim());
       
   if (!Directory.Exists(@args[1].Trim()))
   {
    Directory.CreateDirectory(directoryName);
   }
   while ((theEntry = s.GetNextEntry()) != null) 
   {
    //;
    string fileName = Path.GetFileName(theEntry.Name);

    if (fileName != String.Empty)
    {           
     FileStream streamWriter = File.Create(@args[1].Trim() + fileName);

     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;
      }
     }

     streamWriter.Close();
    }
   }
   s.Close();
  }
 }

}

前台代码:

  1     
 2   


 3   

 4          5            Height="44px" Text="压缩文件/文件夹示例" Width="366px">
 6       
 7           
 8               
 9                   
16                                 
24               
25               
26                   
34                   
42            
43               
44    
45           

10                       
11           

12           
13             

14           

15                   

17                       
18                       
19                       

20                       
21                         

22                       

23                   

27                       
28                       
29                       

30                       
31                         

32                       

33                   

35                       
36                       
37                       

38                       
39                         

40                       

41                   

46           

47           

48       

49   
50   

51   
52

3. 后台页面代码


 1public partial class _Default : System.Web.UI.Page
 2{
 3    protected void Page_Load(object sender, EventArgs e)
 4    {
 5
 6    }
 7    protected void btZipDictory_Click(object sender, EventArgs e)
 8    {
 9        string[] FilePathS = new string[2];
10        FilePathS[0] = TextBox1.Text.Trim();  //待压缩的文件目录
11        FilePathS[1] = TextBox2.Text.Trim();  //生成的目标文件       
12        ZipClass.ZipFileDictory(FilePathS);
13    }
14    protected void btUnZipDictory_Click(object sender, EventArgs e)
15    {
16        string[] FilePathS = new string[2];
17        FilePathS[0] = TextBox3.Text.Trim();  //待解压的文件
18        FilePathS[1] = TextBox4.Text.Trim();  //解压目标存放目录
19        UnZipClass.UnZip(FilePathS);
20    }
21    protected void btZipFile_Click(object sender, EventArgs e)
22    {
23        string[] FilePathS = new string[2];
24        FilePathS[0] = TextBox5.Text.Trim();  //待压缩的文件
25        FilePathS[1] = TextBox6.Text.Trim();  //生成的压缩文件名
26        ZipClass.ZipFile(FilePathS[0], FilePathS[1]);
27
28    }
29    protected void btUnZipFile_Click(object sender, EventArgs e)
30    {
31        string[] FilePathS = new string[2];
32        FilePathS[0] = TextBox7.Text.Trim();  //待解压的文件
33        FilePathS[1] = TextBox8.Text.Trim();  //解压目标存放目录
34        UnZipClass.UnZip(FilePathS);
35    }
36}

示例代码下载

     http://www.cnblogs.com/Files/ChengKing/ZIP.rar

你可能感兴趣的:(.net功能实现,C#功能实现)