我在做项目的时候需要将文件进行压缩和解压缩,于是就从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进行文件的压缩和解压缩收藏的更多相关文章

  1. C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件

    我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...

  2. C#利用SharpZipLib进行文件的压缩和解压缩

    我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...

  3. iOS中使用ZipArchive压缩和解压缩文件-备

    为什么我需要解压缩文件 有许多原因能解释为什么我要在工程中使用压缩和解压缩功能,下面是几个常见的原因: 苹果App Store的50M下载限制 苹 果公司出于流量的考虑,规定在非WIFI环境下,限制用 ...

  4. C# - WinFrm应用程序调用SharpZipLib实现文件的压缩和解压缩

    前言 本篇主要记录:VS2019 WinFrm桌面应用程序调用SharpZipLib,实现文件的简单压缩和解压缩功能. SharpZipLib 开源地址戳这里. 准备工作 搭建WinFrm前台界面 添 ...

  5. 利用SharpZipLib进行字符串的压缩和解压缩

    http://www.izhangheng.com/sharpziplib-string-compression-decompression/ 今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用 ...

  6. .net中压缩和解压缩的处理

    最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip 参考代码如下: //======================= ...

  7. .net 利用 GZipStream 压缩和解压缩

    1.GZipStream 类 此类在 .NET Framework 2.0 版中是新增的. 提供用于压缩和解压缩流的方法和属性 2.压缩byte[] /// <summary> /// 压 ...

  8. 利用ICSharpCode进行压缩和解压缩

    说说我利用ICSharpCode进行压缩和解压缩的一些自己的一下实践过程 1:组件下载地址 参考文章:C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件 2: 文件类 // ...

  9. ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩

    一:MiMEType:一般可以再百度上搜索到相应文件的MiMEType,或是利用c语言的api去获取文件的MiMEType : //对该文件发送一个异步请求,拿到文件的MIMEType - (void ...

随机推荐

  1. Office 365 plans, SharePoint Online, and SharePoint 2013 on-premises 功能对比列表

    Andrew ConnellSharePoint大牛整理了一个各个版本SharePoint功能对比列表,是SharePoint相关人员必备资料.赶紧收藏起来. SharePoint 2013 Feat ...

  2. cocos2d-x 3.2 例子文件工程的位置

    更新到3.2后突然想要看看官方的例子,忽然发现在test中的cpp工程下面没有了工程的启动配置.奇怪,难道只有代码吗?重新查找后原来启动的工程文件都移动到了build文件夹下面,具体的路径就是 coc ...

  3. 【MySql】性能优化之分析命令

    一 当发现程序运行比较慢的时候,首先排除物力资源问题之后,就将注意力转向mysq数据库: 1.首先确定运行慢的sql语句: mysql> show full processlist; 2.确认低 ...

  4. 使用XML与远程服务器进行交互

    最近在做的一个项目其中的一部分是与远程服务器进行交互,确定身份验证的合法性,于是编写了SendRequest方法 此方法发送给远程服务器XML请求,服务器经过处理后,返回XML回应,由此方法接收到后进 ...

  5. [hihoCoder]#1039 : 字符消除

    Description 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1)如果s包含长度超过1的由相同字母组成的子串,那么这些 ...

  6. 第二章TP-Link 703N OpenWrt刷回原厂固件

    (TP官网)首先原厂固件下载 用终端,使用cat /proc/mtd查看路由支持的升级命令,我的是root@OpenWrt:~# cat /proc/mtd dev:    size   erases ...

  7. (剑指Offer)面试题32:从1到n整数中1出现的次数

    题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,一共出现了5次. 思路: 1.累加法 累加1到n中每个整数 ...

  8. CSS(04) 定位

    布局常用的三种:标准流.定位.浮动: 1.文档流-标准流 窗体自上而下分成一行行(元素在 (X)HTML 中的位置),并在一行行中从左到右排放元素: 2.CSS 定位 Position 属性(绝对定位 ...

  9. C++的优秀特性2:inline 函数

    (转载请注明原创于潘多拉盒子) Inline函数是C++的一个很小的特性,在不计较效率的情况下,这个特性似乎可有可无.然而,C++天生是为最为广泛的应用场景设计的,因此,总会有关于效率的问题.其实,除 ...

  10. Linux Kconfig及Makefile学习

    内核源码树的目录下都有两个文档Kconfig (2.4版本是Config.in)和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源 ...