/// <summary> /// 功能:解壓zip格式的文件。 /// </summary> /// <param name="zipFilePath">壓縮文件路徑</param> /// <param name="unZipDir">解壓文件存放路徑,為空時默認與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾</param> /// <param name="err">出錯信息</param> /// <returns>解壓是否成功</returns> public bool UnZipFile(string zipFilePath, string unZipDir)// , out string err { // err = ""; if (zipFilePath == string.Empty) { //err = "壓縮文件不能為空!"; return false; } if (!File.Exists(zipFilePath)) { //err = "壓縮文件不存在!"; return false; } //解壓文件夾為空時默認與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾 if (unZipDir == string.Empty) unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath)); if (!unZipDir.EndsWith("\\")) unZipDir += "\\"; if (!Directory.Exists(unZipDir)) Directory.CreateDirectory(unZipDir); try { using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath.ToLower()))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName.Length > 0) { Directory.CreateDirectory(unZipDir + directoryName); } if (!directoryName.EndsWith("\\")) directoryName += "\\"; if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(unZipDir + 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; } } } } }//while } } catch (Exception ex) { //err = ex.Message; return false; } return true; }//解壓結束