VS2010 C# 壓縮/解壓縮目錄的方法

VS2010 C# 壓縮/解壓縮目錄的方法

VS2010 C# 壓縮/解壓縮目錄的方法


 

資料來源: https://dotnetzip.codeplex.com/  (外部元件)

http://einboch.pixnet.net/blog/post/267937268-c%23%E4%BD%BF%E7%94%A8dotnetzip%E4%BE%86%E5%AE%8C%E6%88%90%E6%AA%94%E6%A1%88%E7%9A%84%E5%A3%93%E7%B8%AE-%E8%A7%A3%E5%A3%93%E7%B8%AE%E5%8A%9F%E8%83%BD
 (
以這一文件為基礎配合新版元件做修改)

 


01.    

引用參考元件Ionic.Zip.dll (1.9板之前是引用Ionic.Zlib.dll)


02.    

程式碼如下:


03.    


using System;


04.    


using System.Collections.Generic;


05.    


using System.Linq;


06.    


using System.Text;


07.    


 


08.    


using Ionic.Zip;//ZIP 1.9VS2010


09.    


using System.Collections;//ArrayList


10.    


using System.IO;//Directory


11.    


 


12.    


namespace Console_CS_Zlib


13.    


{


14.    


    class Program


15.    


    {


16.    


        //aLuDOyUO3E¡Ñ


17.    


        public static ArrayList GetFiles(string
path)


18.    


        {


19.    


            ArrayList files = new
ArrayList();


20.    


 


21.    


            if (Directory.Exists(path))


22.    


            {


23.    


                files.AddRange(Directory.GetFiles(path));


24.    


            }


25.    


 


26.    


            return files;


27.    


        }


28.    


        //¢GYE¡Ñ


29.    


        //path: ¢GYE¡ÑMo|


30.    


        //password: ÓKX


31.    


        //comment: guMN


32.    


        public static void ZipFiles(string
s_path, string zipPath, string password, string
comment)


33.    


        {


34.    


            //string zipPath = e_path + @”\” +
Path.GetFileName(e_path) + “.zip”;


35.    


            ZipFile zip = new
ZipFile();


36.    


            if (password != null
&& password != string.Empty)
zip.Password = password;


37.    


            if (comment != null
&& comment != “”)
zip.Comment = comment;


38.    


            ArrayList files = GetFiles(s_path);


39.    


            foreach (string f in files)


40.    


            {


41.    


                zip.AddFile(f, string.Empty);//AGOXNA]¢XAEiDU¢GYE¡ÑE¢GÓNE¡ÑMo|D[J


42.    


            }


43.    


            zip.Save(zipPath);//snbLOP?DOy?


44.    


        }


45.    


        //ODsDOy


46.    


        public static void CreateDirectory(string
path)


47.    


        {


48.    


            if (!Directory.Exists(path))


49.    


            {


50.    


                Directory.CreateDirectory(path);


51.    


            }


52.    


        }


53.    


        //MN¢GYE¡Ñ


54.    


        //path: MN¢GYE¡ÑDOyMo|


55.    


        //password: ÓKX


56.    


        public static void UnZipFiles(string
zipPath, string unZipPath, string password)


57.    


        {


58.    


            ZipFile unzip = ZipFile.Read(zipPath);


59.    


            if (password != null
&& password != string.Empty)
unzip.Password = password;


60.    


            //string unZipPath = f_path.Replace(“.zip”,
“”);


61.    


 


62.    


            foreach (ZipEntry
e in unzip)


63.    


            {


64.    


                e.Extract(unZipPath, ExtractExistingFileAction.OverwriteSilently);


65.    


            }


66.    


        }


67.    


        static void Pause()


68.    


        {


69.    


            Console.Write(“Press
any key to continue . . . ”
);


70.    


            Console.ReadKey(true);


71.    


        }


72.    


        static void Main(string[] args)


73.    


        {


74.    


            string s_path = @”C:\Users\RD\Desktop\Release”;//n¢GYE¡ÑoDOyMo|


75.    


            string zipPath = @”C:\Users\RD\Desktop\123.zip”;


76.    


            ZipFiles(s_path,zipPath,
string.Empty, string.Empty);


77.    


            Pause();


78.    


            string unZipPath=@”C:\Users\RD\Desktop”;


79.    


            UnZipFiles(zipPath, unZipPath, string.Empty);


80.    


            Pause();


81.    


        }


82.    


    }


83.    


}


84.    


 


85.    


 

 


 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *