03.
using System;
04.
using System.Collections.Generic;
05.
using System.Linq;
06.
using System.Text;
07.
08.
using Ionic.Zip;//ZIP 1.9版 VS2010
09.
using System.Collections;//ArrayList
10.
using System.IO;//Directory
11.
12.
namespace Console_CS_Zlib
13.
{
14.
class Program
15.
{
16.
//讀a取Lu目DO錄y下U所O有3檔E案¡Ñ
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.
//壓¢G縮Y檔E案¡Ñ
29.
//path: 壓¢G縮Y檔E案¡Ñ路Mo徑|
30.
//password: 密ÓK碼X
31.
//comment: 註gu解MN
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);//第A二G個O參XN數A設]為¢X空A值E表i示DU壓¢G縮Y檔E案¡Ñ時E不¢G將ÓN檔E案¡Ñ路Mo徑|加D[入J
42.
}
43.
zip.Save(zipPath);//存s放n在b來LO源P?目DO錄y中?
44.
}
45.
//建O立Ds目DO錄y
46.
public static void CreateDirectory(string
path)
47.
{
48.
if (!Directory.Exists(path))
49.
{
50.
Directory.CreateDirectory(path);
51.
}
52.
}
53.
//解MN壓¢G縮Y檔E案¡Ñ
54.
//path: 解MN壓¢G縮Y檔E案¡Ñ目DO錄y路Mo徑|
55.
//password: 密ÓK碼X
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壓¢G縮Y檔E案¡Ñ的o目DO錄y路Mo徑|
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.
|