c#的文件压缩

来源:互联网 发布:snmp出口流量监控软件 编辑:程序博客网 时间:2024/04/28 01:24

使用
ICSharpCode.SharpZipLib.dll
下载地址:
http://www.icsharpcode.net/OpenSource/SharpZipLib/

using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;


代码:
压缩到文件

  private void btnZip_Click(object sender, System.EventArgs e)
  {
   string filename;
   ZipOutputStream zos;
   FileStream fs;

   Crc32 crc = new Crc32();

   OpenFileDialog openfileDialog = new OpenFileDialog();

   //openfileDialog.InitialDirectory = "c://" ;
   openfileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
   openfileDialog.FilterIndex = 2 ;
   openfileDialog.RestoreDirectory = false ;

   if(openfileDialog.ShowDialog() == DialogResult.OK)
   {
    filename = Path.GetFileName(openfileDialog.FileName);

    zos = new ZipOutputStream(File.Create(filename + ".zip"));

    zos.SetLevel(6);

    fs = File.OpenRead(filename);

    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    this.richTextBox1.Text=Encoding.UTF8.GetString(buffer);
    ZipEntry entry = new ZipEntry(filename);
   
    entry.DateTime = DateTime.Now;
    entry.Size = fs.Length;

    fs.Close();

    crc.Reset();
    crc.Update(buffer);
   
    entry.Crc  = crc.Value;
   
    zos.PutNextEntry(entry);
   
    zos.Write(buffer, 0, buffer.Length);

    zos.Finish();
    zos.Close();
   }
  }
压缩到内存

  private void btnZipOneText_Click(object sender, System.EventArgs e)
  {
   msFileIn = new MemoryStream();

   //this.statusBarPanel1.Text="正在压缩编码第一个文本框里的数据 ... ...";
   byte[] getchr = Encoding.UTF8.GetBytes(this.richTextBox1.Text.ToString());

   ZipOutputStream zos = new ZipOutputStream(msFileIn);
   zos.SetLevel(6);
   ZipEntry entry = new ZipEntry("Stream");
   Crc32 crc = new Crc32();
   crc.Reset();
   crc.Update(getchr);
   entry.Crc  = crc.Value;
   entry.DateTime = DateTime.Now;
   entry.Size = getchr.Length;
   zos.PutNextEntry(entry);

   zos.Write(getchr,0,getchr.Length);

   zos.Finish();
   zos.Close();

   MessageBox.Show("压缩完成,结果存入内存!");
   //this.statusBarPanel1.Text = "第一个文本框里的数据压缩完成。";
  }
解压文件
  private void btnUnZip_Click(object sender, System.EventArgs e)
  {
   string filename;

   OpenFileDialog openfileDialog = new OpenFileDialog();

   //openfileDialog.InitialDirectory = "c://" ;
   openfileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
   openfileDialog.FilterIndex = 2 ;
   openfileDialog.RestoreDirectory = false ;

   if(openfileDialog.ShowDialog() == DialogResult.OK)
   {
    filename = Path.GetFileName(openfileDialog.FileName);

    ZipInputStream zis = new ZipInputStream(File.OpenRead(filename));
  
    ZipEntry theEntry;
    while ((theEntry = zis.GetNextEntry()) != null)
    {
     //string directoryName = Path.GetDirectoryName(theEntry.Name);
     //Directory.CreateDirectory(directoryName);

     string fileName = Path.GetFileName(theEntry.Name);

     if (fileName != String.Empty)
     {
      FileStream streamWriter = File.Create(theEntry.Name);
    
      int size = 2048;
      byte[] data = new byte[2048];
      while (true)
      {
       size = zis.Read(data, 0, data.Length);
       if (size > 0)
       {
        streamWriter.Write(data, 0, size);
       }
       else
       {
        break;
       }
      }
    
      streamWriter.Close();
     }
    }
    zis.Close();
   }
   
  }