C# 解壓縮文件


C# 解壓縮文件

 

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

/// <summary>
        /// 實例化FastZip
        /// </summary>
        public static FastZip fz = new FastZip();
實例化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;
        }
解壓ZIP

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM