码迷,mamicode.com
首页 > Windows程序 > 详细

在C#中利用SharpZipLib进行文件的压缩和解压缩收藏

时间:2015-06-23 17:25:52      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net(http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx)下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手。只好耐下心来,慢慢的研究,总算找到了门路。针对自己的需要改写了文件压缩和解压缩的两个类,分别为ZipClass和UnZipClass。其中碰到了不少困难,就决定写出来压缩和解压的程序后,一定把源码贴出来共享,让首次接触压缩和解压缩的朋友可以少走些弯路。下面就来解释如何在C#里用http://www.icsharpcode.net下载的SharpZipLib进行文件的压缩和解压缩。

    首先需要在项目里引用SharpZipLib.dll。然后修改其中的关于压缩和解压缩的类。实现源码如下:

 

 

技术分享
技术分享 /**//// <summary> 
技术分享 /// 压缩文件 
技术分享 /// </summary> 
技术分享 
技术分享using System; 
技术分享using System.IO; 
技术分享 
技术分享using ICSharpCode.SharpZipLib.Checksums; 
技术分享using ICSharpCode.SharpZipLib.Zip; 
技术分享using ICSharpCode.SharpZipLib.GZip; 
技术分享 
技术分享namespace Compression 
技术分享
技术分享 public class ZipClass 
技术分享 
技术分享  
技术分享  public void ZipFile(string FileToZip, string ZipedFile ,int CompressionLevel, int BlockSize) 
技术分享  
技术分享   //如果文件没有找到,则报错 
技术分享   if (! System.IO.File.Exists(FileToZip))  
技术分享   
技术分享    throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd"); 
技术分享   } 
技术分享   
技术分享   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 void ZipFileMain(string[] args) 
技术分享  
技术分享   string[] filenames = Directory.GetFiles(args[0]); 
技术分享   
技术分享   Crc32 crc = new Crc32(); 
技术分享   ZipOutputStream s = new ZipOutputStream(File.Create(args[1])); 
技术分享   
技术分享   s.SetLevel(6); // 0 - store only to 9 - means best compression 
技术分享   
技术分享   foreach (string file in filenames)  
技术分享   
技术分享    //打开压缩文件 
技术分享    FileStream fs = File.OpenRead(file); 
技术分享    
技术分享    byte[] buffer = new byte[fs.Length]; 
技术分享    fs.Read(buffer, 0, buffer.Length); 
技术分享    FileInfo   info   =   new   FileInfo(file);   
技术分享    ZipEntry   entry   =   new   ZipEntry(info.Name);   //压缩后显示的路径为当前压缩的目录
技术分享
技术分享    //ZipEntry   entry   =   new   ZipEntry(file); //压缩后显示全路径
技术分享    entry.DateTime = DateTime.Now; 
技术分享    
技术分享    // set Size and the crc, because the information 
技术分享    // about the size and crc should be stored in the header 
技术分享    // if it is not set it is automatically written in the footer. 
技术分享    // (in this case size == crc == -1 in the header) 
技术分享    // Some ZIP programs have problems with zip files that don‘t store 
技术分享    // the size and crc in the header. 
技术分享    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> 
技术分享 
技术分享using System; 
技术分享using System.Text; 
技术分享using System.Collections; 
技术分享using System.IO; 
技术分享using System.Diagnostics; 
技术分享using System.Runtime.Serialization.Formatters.Binary; 
技术分享using System.Data; 
技术分享 
技术分享using ICSharpCode.SharpZipLib.BZip2; 
技术分享using ICSharpCode.SharpZipLib.Zip; 
技术分享using ICSharpCode.SharpZipLib.Zip.Compression; 
技术分享using ICSharpCode.SharpZipLib.Zip.Compression.Streams; 
技术分享using ICSharpCode.SharpZipLib.GZip; 
技术分享 
技术分享namespace DeCompression 
技术分享
技术分享 public class UnZipClass 
技术分享 {    
技术分享  public void UnZip(string[] args) 
技术分享  
技术分享   ZipInputStream s = new ZipInputStream(File.OpenRead(args[0])); 
技术分享   
技术分享   ZipEntry theEntry; 
技术分享   while ((theEntry = s.GetNextEntry()) != null)  
技术分享   
技术分享    
技术分享          string directoryName = Path.GetDirectoryName(args[1]); 
技术分享    string fileName      = Path.GetFileName(theEntry.Name); 
技术分享    
技术分享    //生成解压目录 
技术分享    Directory.CreateDirectory(directoryName); 
技术分享    
技术分享    if (fileName != String.Empty)  
技术分享    {    
技术分享     //解压文件到指定的目录 
技术分享     FileStream streamWriter = File.Create(args[1]+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; 
技术分享      } 
技术分享     } 
技术分享     
技术分享     streamWriter.Close(); 
技术分享    } 
技术分享   } 
技术分享   s.Close(); 
技术分享  } 
技术分享 } 
技术分享

 

    有了压缩和解压缩的类以后,就要在窗体里调用了。怎么?是新手,不会调用?Ok,接着往下看如何在窗体里调用。

    首先在窗体里放置两个命令按钮(不要告诉我你不会放啊~),然后编写以下源码

 

技术分享
技术分享/**//// <summary> 
技术分享 /// 调用源码 
技术分享 /// </summary> 
技术分享 
技术分享      private void button2_Click_1(object sender, System.EventArgs e) 
技术分享  
技术分享   string []FileProperties=new string[2]; 
技术分享   FileProperties[0]="C:/unzipped/";//待压缩文件目录 
技术分享   FileProperties[1]="C:/zip/a.zip";  //压缩后的目标文件 
技术分享   ZipClass Zc=new ZipClass(); 
技术分享   Zc.ZipFileMain(FileProperties); 
技术分享  } 
技术分享 
技术分享     private void button2_Click(object sender, System.EventArgs e) 
技术分享  
技术分享   string []FileProperties=new string[2]; 
技术分享   FileProperties[0]="C:/zip/test.zip";//待解压的文件 
技术分享   FileProperties[1]="C:/unzipped/";//解压后放置的目标目录 
技术分享   UnZipClass UnZc=new UnZipClass(); 
技术分享   UnZc.UnZip(FileProperties); 
技术分享  } 

在C#中利用SharpZipLib进行文件的压缩和解压缩收藏

标签:

原文地址:http://www.cnblogs.com/lenther2002/p/4595566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!