1. 程式人生 > 實用技巧 >資料夾解壓縮

資料夾解壓縮

#endregion
        #region 解壓縮檔案
        /// <summary>
        /// 解壓縮檔案
        /// </summary>
        /// <param name="GzipFile">壓縮包檔名</param>
        /// <param name="targetPath">解壓縮目標路徑</param>
        public static void Decompress(string GzipFile, string targetPath)
        {
            
string directoryName = Path.GetDirectoryName(targetPath + "//") + "//"; //string directoryName = targetPath; if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解壓目錄 string CurrentDirectory = directoryName; byte[] data = new byte
[2048]; int size = 2048; ZipEntry theEntry = null; using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile))) { while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.IsDirectory) {
// 該結點是目錄 if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name); } else { if (theEntry.Name != String.Empty) { // 檢查多級目錄是否存在 if (theEntry.Name.Contains("//")) { string parentDirPath = theEntry.Name.Remove(theEntry.Name.LastIndexOf("//") + 1); if (!Directory.Exists(parentDirPath)) { Directory.CreateDirectory(CurrentDirectory + parentDirPath); } } //解壓檔案到指定的目錄 using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name)) { while (true) { size = s.Read(data, 0, data.Length); if (size <= 0) break; streamWriter.Write(data, 0, size); } streamWriter.Close(); } } } } s.Close(); } }