C# 解压zip压缩文件

此方法需要在程序内引用ICSharpCode.SharpZipLib.dll 类库

 

[csharp][/csharp] 

  1. /// <summary>
  2.        /// 功能:解压zip格式的文件。
  3.        /// </summary>
  4.        /// <param name=”zipFilePath”>压缩文件路径</param>
  5.        /// <param name=”unZipDir”>解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
  6.        /// <param name=”err”>出错信息</param>
  7.        /// <returns>解压是否成功</returns>
  8.        public bool UnZipFile(string zipFilePath, string unZipDir)// , out string err
  9.        {
  10.            // err = “”;
  11.            if (zipFilePath == string.Empty)
  12.            {
  13.                //err = “压缩文件不能为空!”;
  14.                return false;
  15.            }
  16.            if (!File.Exists(zipFilePath))
  17.            {
  18.                //err = “压缩文件不存在!”;
  19.                return false;
  20.            }
  21.            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
  22.            if (unZipDir == string.Empty)
  23.                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
  24.            if (!unZipDir.EndsWith(“\\”))
  25.                unZipDir += “\\”;
  26.            if (!Directory.Exists(unZipDir))
  27.                Directory.CreateDirectory(unZipDir);
  28.            try
  29.            {
  30.                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath.ToLower())))
  31.                {
  32.                    ZipEntry theEntry;
  33.                    while ((theEntry = s.GetNextEntry()) != null)
  34.                    {
  35.                        string directoryName = Path.GetDirectoryName(theEntry.Name);
  36.                        string fileName = Path.GetFileName(theEntry.Name);
  37.                        if (directoryName.Length > 0)
  38.                        {
  39.                            Directory.CreateDirectory(unZipDir + directoryName);
  40.                        }
  41.                        if (!directoryName.EndsWith(“\\”))
  42.                            directoryName += “\\”;
  43.                        if (fileName != String.Empty)
  44.                        {
  45.                            using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
  46.                            {
  47.                                int size = 2048;
  48.                                byte[] data = new byte[2048];
  49.                                while (true)
  50.                                {
  51.                                    size = s.Read(data, 0, data.Length);
  52.                                    if (size > 0)
  53.                                    {
  54.                                        streamWriter.Write(data, 0, size);
  55.                                    }
  56.                                    else
  57.                                    {
  58.                                        break;
  59.                                    }
  60.                                }
  61.                            }
  62.                        }
  63.                    }//while
  64.                }
  65.            }
  66.            catch (Exception ex)
  67.            {
  68.                //err = ex.Message;
  69.                return false;
  70.            }
  71.            return true;
  72.        }//解压结束

标签