C# 解壓縮文件
需要通過引用ICSharpCode.SharpZipLib.Zip(程序包里面下載安裝),來實現文件的壓縮與解壓

/// <summary> /// 實例化FastZip /// </summary> public static FastZip fz = new FastZip();

/// <summary> /// 壓縮文件 /// </summary> /// <param name="zipFilePath">壓縮文件的路徑與名稱</param> /// <param name="FilePath">被壓縮的文件路徑</param> /// <param name="ZipPWD">解壓密碼(null代表無密碼)</param> /// <returns></returns> public static string FileToZip(string zipFilePath, string FilePath, string ZipPWD) { string state = "Fail..."; try { FileInfo fi = new FileInfo(FilePath); string filename = fi.Name; string dirname = fi.DirectoryName; fz.Password = ZipPWD; fz.CreateZip(zipFilePath, dirname, false, filename); state = "Success !"; } catch(Exception ex) { state += "," + ex.Message; } return state; }

/// <summary> /// 壓縮文件夾 /// </summary> /// <param name="DirPath">被壓縮的文件夾路徑</param> /// <param name="ZipPath">壓縮文件夾的路徑與名稱</param> /// <param name="ZipPWD">解壓密碼(null代表無密碼)</param> /// <returns></returns> public static string DirToZip(string DirPath, string ZipPath, string ZipPWD) { string state = "Fail..."; try { fz.Password = ZipPWD; fz.CreateZip(ZipPath, DirPath, false, null); state = "Success !"; } catch (Exception ex) { state += "," + ex.Message; } return state; }

/// <summary> /// 解壓Zip /// </summary> /// <param name="DirPath">解壓后存放路徑</param> /// <param name="ZipPath">Zip的存放路徑</param> /// <param name="ZipPWD">解壓密碼(null代表無密碼)</param> /// <returns></returns> public static string Compress(string DirPath, string ZipPath, string ZipPWD) { string state = "Fail..."; try { fz.Password = ZipPWD; fz.ExtractZip(ZipPath, DirPath, null); state = "Success !"; } catch (Exception ex) { state += "," + ex.Message; } return state; }