1. ICSharpCode.SharpZipLib.Zip使用示例代码及下载地址ICSharpCode.SharpZipLib.Zip封装了ZIP文件在线压缩解压的一个dll,这里收集了ICSharpCode.SharpZipLib.Zip使用示例代码,方便有需要的人使用.  
  2.  
  3. Code  
  4.  
  5.  class ZIP  
  6.  
  7.      {/**//// <summary>压缩文件</summary>  
  8.  
  9.         /// <param name="filename">filename生成的文件的名称,如:C\123\123.zip</param>  
  10.  
  11.          /// <param name="directory">directory要压缩的文件夹路径</param>  
  12.  
  13.          /// <returns></returns>  
  14.  
  15.          public static bool PackFiles(string filename, string directory)  
  16.  
  17.          {  
  18.  
  19.              try 
  20.  
  21.             {  
  22.  
  23.  
  24.  
  25.                directory = directory.Replace("/""\\");  
  26.  
  27.  
  28.                 if (!directory.EndsWith("\\"))  
  29.  
  30.                    directory += "\\";  
  31.  
  32.                if (!Directory.Exists(directory))  
  33.  
  34.                 {  
  35.  
  36.                    Directory.CreateDirectory(directory);  
  37.  
  38.                 }  
  39.  
  40.                 if (File.Exists(filename))  
  41.  
  42.                {  
  43.  
  44.                     File.Delete(filename);  
  45.  
  46.                }  
  47.  
  48.                //ICSharpCode.SharpZipLib.Zip.ZipFile pp = new ZipFile();  
  49.  
  50.                //FastZip fz = new FastZip();  
  51.  
  52.                 //fz.CreateEmptyDirectories = true;  
  53.  
  54.                 //fz.CreateZip(filename, directory, true, "");  
  55.  
  56.                return true;  
  57.  
  58.             }  
  59.  
  60.            catch (Exception)  
  61.  
  62.            {  
  63.                return false;  
  64.             }  
  65.         }  
  66.  
  67.  
  68.         /**//// <summary>解压文件</summary>  
  69.  
  70.        /// <param name="file">压缩文件的名称,如:C:\123\123.zip</param>  
  71.  
  72.        /// <param name="dir">dir要解压的文件夹路径</param>  
  73.  
  74.        /// <returns></returns>  
  75.  
  76.         public static bool UnpackFiles(string file, string dir)  
  77.        {  
  78.            try 
  79.            {  
  80.                if (!File.Exists(file))  
  81.                  return false;  
  82.                dir = dir.Replace("/""\\");  
  83.                 if (!dir.EndsWith("\\"))  
  84.                    dir += "\\";  
  85.                if (!Directory.Exists(dir))  
  86.                    Directory.CreateDirectory(dir);  
  87.                ZipInputStream s = new ZipInputStream(File.OpenRead(file));  
  88.                ZipEntry theEntry;  
  89.               while ((theEntry = s.GetNextEntry()) != null)  
  90.  
  91.                {  
  92.                     string directoryName = Path.GetDirectoryName(theEntry.Name);  
  93.                     string fileName = Path.GetFileName(theEntry.Name);  
  94.  
  95.                     if (directoryName != String.Empty)  
  96.                        Directory.CreateDirectory(dir + directoryName);  
  97.                    if (fileName != String.Empty)  
  98.                    {  
  99.                        FileStream streamWriter = File.Create(dir + theEntry.Name);  
  100.                         int size = 2048;  
  101.                         byte[] data = new byte[2048];  
  102.                         while (true)  
  103.                         {  
  104.                            size = s.Read(data, 0, data.Length);  
  105.                            if (size > 0)  
  106.                            {  
  107.                                streamWriter.Write(data, 0, size);  
  108.                             }  
  109.                            else 
  110.                            {  
  111.                                 break;  
  112.                            }  
  113.                        }  
  114.                        streamWriter.Close();  
  115.                     }  
  116.                }  
  117.                s.Close();  
  118.                 return true;  
  119.             }  
  120.            catch (Exception)  
  121.            {  
  122.                return false;  
  123.            }  
  124.        }  
  125.     }  
  126.  
  127.    
  128.  
  129. ICSharpCode.SharpZipLib.Zip下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib/