编辑 | blame | 历史 | 原始文档
ICSharpCode.SharpZipLib.Zip使用示例代码及下载地址


ICSharpCode.SharpZipLib.Zip封装了ZIP文件在线压缩解压的一个dll,这里收集了ICSharpCode.SharpZipLib.Zip使用示例代码,方便有需要的人使用.下载地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/

Code
class ZIP
    {
      /**//// 压缩文件
        /// filename生成的文件的名称,如:C\123\123.zip
       /// directory要压缩的文件夹路径
        /// 
       public static bool PackFiles(string filename, string directory)
        {
          try
            {

                directory = directory.Replace("/", "\\");

                if (!directory.EndsWith("\\"))
                    directory += "\\";
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
               //ICSharpCode.SharpZipLib.Zip.ZipFile pp = new ZipFile();
                //FastZip fz = new FastZip();
                //fz.CreateEmptyDirectories = true;
                //fz.CreateZip(filename, directory, true, "");
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }



        /**//// 解压文件
        /// 压缩文件的名称,如:C:\123\123.zip
       /// dir要解压的文件夹路径
        /// 
        public static bool UnpackFiles(string file, string dir)
        {
           try
            {
                if (!File.Exists(file))
                    return false;

                dir = dir.Replace("/", "\\");
               if (!dir.EndsWith("\\"))
                   dir += "\\";

                if (!Directory.Exists(dir))
                   Directory.CreateDirectory(dir);

                ZipInputStream s = new ZipInputStream(File.OpenRead(file));
                ZipEntry theEntry;
               while ((theEntry = s.GetNextEntry()) != null)
               {

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

                    if (directoryName != String.Empty)
                        Directory.CreateDirectory(dir + directoryName);

                  if (fileName != String.Empty)
                    {
                       FileStream streamWriter = File.Create(dir + 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();
               return true;
           }
            catch (Exception)
            {
               return false;
           }
        }

    }