.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
{
 /// <summary>
 /// ZipClass の概要の説明です。
 /// </summary>
 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();
  }

  /// <summary>
  /// ??文件
  /// </summary>
  /// <param name="FileToZip">要?行??的文件名</param>
  /// <param name="ZipedFile">??后生成的??文件名</param>
  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
{
 /// <summary>
 /// UnZipClass の概要の説明です。
 /// </summary>

 public class UnZipClass: System.Web.UI.Page
 {
  public UnZipClass()
  {
   //
   // TODO: コンストラクタ ロジックをここに追加してください。
   //
  }
  /// <summary>
  /// 解?功能(解???文件到指定目?)
  /// </summary>
  /// <param name="args">待解?的文件</param>
  /// <param name="args">指定解?目?目?</param>
  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      <body>
 2    <form id="form1" runat="server">
 3    <div>
 4        &nbsp;<asp:Label ID="Label1" runat="server" BackColor="#C0C0FF" Font-Size="XX-Large"
 5            Height="44px" Text="压缩文件/文件夹示例" Width="366px"></asp:Label>
 6        <asp:Panel ID="Panel1" runat="server" Height="1px" Width="369px" BackColor="#FFFFC0">
 7            <table width="100%" height="100%">
 8                <tr>
 9                    <td style="width: 3px" valign="top">
10                        <asp:Label ID="lbDisplay" runat="server" Text="压缩目录(from/to):" Width="153px"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
11            <br />
12            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
13            &nbsp;<br />
14            <asp:Button ID="btZipDictory" runat="server" OnClick="btZipDictory_Click" Text="压缩目录" /><br />
15                    </td>
16                    <td style="width: 4px" valign="middle">
17                        <asp:Label ID="Label2" runat="server" Text="解压目录(from/to):" Width="154px"></asp:Label>
18                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
19                        <br />
20                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
21                        &nbsp;<br />
22                        <asp:Button ID="btUnZipDictory" runat="server" Text="解压目录" OnClick="btUnZipDictory_Click" /><br />
23                    </td>             
24                </tr>
25                <tr>
26                    <td style="width: 3px; height: 150px" valign="top">
27                        <asp:Label ID="Label3" runat="server" Text="压缩文件(from/to):" Width="153px"></asp:Label>
28                        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
29                        <br />
30                        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
31                        &nbsp;<br />
32                        <asp:Button ID="btZipFile" runat="server" Text="压缩文件" OnClick="btZipFile_Click" /><br />
33                    </td>
34                    <td style="width: 4px; height: 150px" valign="top">
35                        <asp:Label ID="Label4" runat="server" Text="解压文件(from/to):" Width="154px"></asp:Label>
36                        <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
37                        <br />
38                        <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
39                        &nbsp;<br />
40                        <asp:Button ID="btUnZipFile" runat="server" Text="解压文件" OnClick="btUnZipFile_Click" /><br />
41                    </td>
42            
43                </tr>
44    
45            </table>
46            <asp:Label ID="lbMessage" runat="server" Width="368px"></asp:Label><br />
47            <br />
48        </asp:Panel>
49   
50    </div>
51    </form>
52</body>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值