壓縮包製做也是不少項目中須要用到的功能。好比有大量的文件(假設有10000個)須要上傳,1個1個的上傳彷佛不太靠譜(靠,那得傳到何時啊?),這時咱們能夠製做一個壓縮包zip,直接傳這個文件到服務器端,而後在服務器目錄解壓,釋放裏面的文件。算法
這裏咱們選用ICSharpCode.SharpZipLib這個類庫來實現咱們的需求。服務器
ICSharpCode.SharpZipLib提供了兩個壓縮算法的實現,分別是BZIP2(壓縮時間長,壓縮率高)和GZIP(壓縮效率高,壓縮率低)。測試
先定義一個枚舉,用於程序標示是哪一個壓縮算法。this
01 |
/// <summary> |
02 |
/// 壓縮枚舉 |
03 |
/// </summary> |
04 |
public enum ZipEnum |
05 |
{ |
06 |
//壓縮時間長,壓縮率高 |
07 |
BZIP2, |
08 |
09 |
//壓縮效率高,壓縮率低 |
10 |
GZIP |
11 |
} |
單個文件的壓縮:加密
01 |
#region 製做壓縮包(單個文件壓縮) |
02 |
/// <summary> |
03 |
/// 製做壓縮包(單個文件壓縮) |
04 |
/// </summary> |
05 |
/// <param name="sourceFileName">原文件</param> |
06 |
/// <param name="zipFileName">壓縮文件</param> |
07 |
/// <param name="zipEnum">壓縮算法枚舉</param> |
08 |
/// <returns>壓縮成功標誌</returns> |
09 |
public static bool ZipFile( string srcFileName, string zipFileName, ZipEnum zipEnum) |
10 |
{ |
11 |
bool flag = true ; |
12 |
try |
13 |
{ |
14 |
switch (zipEnum) |
15 |
{ |
16 |
case ZipEnum.BZIP2: |
17 |
18 |
FileStream inStream = File.OpenRead(srcFileName); |
19 |
FileStream outStream = File.Open(zipFileName, FileMode.Create); |
20 |
21 |
//參數true表示壓縮完成後,inStream和outStream鏈接都釋放 |
22 |
BZip2.Compress(inStream, outStream, true , 4096); |
23 |
24 |
inStream.Close(); |
25 |
outStream.Close(); |
26 |
27 |
28 |
break ; |
29 |
case ZipEnum.GZIP: |
30 |
31 |
FileStream srcFile = File.OpenRead(srcFileName); |
32 |
33 |
GZipOutputStream zipFile = new GZipOutputStream(File.Open(zipFileName, FileMode.Create)); |
34 |
35 |
byte [] fileData = new byte [srcFile.Length]; |
36 |
srcFile.Read(fileData, 0, ( int )srcFile.Length); |
37 |
zipFile.Write(fileData, 0, fileData.Length); |
38 |
39 |
srcFile.Close(); |
40 |
zipFile.Close(); |
41 |
42 |
break ; |
43 |
default : break ; |
44 |
} |
45 |
} |
46 |
catch |
47 |
{ |
48 |
flag = false ; |
49 |
} |
50 |
return flag; |
51 |
} |
52 |
#endregion |
單個文件的解壓縮:spa
01 |
#region 解壓縮包(單個文件解壓縮) |
02 |
/// <summary> |
03 |
/// 解壓縮包(單個文件解壓縮) |
04 |
/// </summary> |
05 |
/// <param name="zipFileName">壓縮文件</param> |
06 |
/// <param name="unzipFileName">解壓縮文件</param> |
07 |
/// <param name="zipEnum">壓縮算法枚舉</param> |
08 |
/// <returns>壓縮成功標誌</returns> |
09 |
public static bool UnZipFile( string zipFileName, string unzipFileName, ZipEnum zipEnum) |
10 |
{ |
11 |
bool flag = true ; |
12 |
try |
13 |
{ |
14 |
switch (zipEnum) |
15 |
{ |
16 |
case ZipEnum.BZIP2: |
17 |
FileStream inStream = File.OpenRead(zipFileName); |
18 |
FileStream outStream = File.Open(unzipFileName, FileMode.Create); |
19 |
BZip2.Decompress(inStream, outStream, true ); |
20 |
break ; |
21 |
case ZipEnum.GZIP: |
22 |
GZipInputStream zipFile = new GZipInputStream(File.OpenRead(zipFileName)); |
23 |
FileStream destFile = File.Open(unzipFileName, FileMode.Create); |
24 |
25 |
int bufferSize = 2048 * 2; |
26 |
byte [] fileData = new byte [bufferSize]; |
27 |
28 |
while (bufferSize > 0) |
29 |
{ |
30 |
bufferSize = zipFile.Read(fileData, 0, bufferSize); |
31 |
zipFile.Write(fileData, 0, bufferSize); |
32 |
} |
33 |
destFile.Close(); |
34 |
zipFile.Close(); |
35 |
break ; |
36 |
default : break ; |
37 |
} |
38 |
} |
39 |
catch |
40 |
{ |
41 |
flag = false ; |
42 |
} |
43 |
return flag; |
44 |
} |
45 |
#endregion |
上面的兩個方法在引用了dll後,能夠直接使用。.net
看到這裏,相信讀者有疑問了,若是我想讓多個文件壓縮到1個zip包裏呢?甚至能夠給文件加密?給zip包加註釋?code
好吧,我這裏繼續貼兩個方法,都通過測試,可用。blog
製做壓縮包:ip
01 |
#region 製做壓縮包(多個文件壓縮到一個壓縮包,支持加密、註釋) |
02 |
/// <summary> |
03 |
/// 製做壓縮包(多個文件壓縮到一個壓縮包,支持加密、註釋) |
04 |
/// </summary> |
05 |
/// <param name="topDirectoryName">壓縮文件目錄</param> |
06 |
/// <param name="zipedFileName">壓縮包文件名</param> |
07 |
/// <param name="compresssionLevel">壓縮級別 1-9</param> |
08 |
/// <param name="password">密碼</param> |
09 |
/// <param name="comment">註釋</param> |
10 |
public static void ZipFiles( string topDirectoryName, string zipedFileName, int compresssionLevel, string password, string comment) |
11 |
{ |
12 |
using (ZipOutputStream zos = new ZipOutputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) |
13 |
{ |
14 |
if (compresssionLevel != 0) |
15 |
{ |
16 |
zos.SetLevel(compresssionLevel); //設置壓縮級別 |
17 |
} |
18 |
19 |
if (! string .IsNullOrEmpty(password)) |
20 |
{ |
21 |
zos.Password = password; //設置zip包加密密碼 |
22 |
} |
23 |
24 |
if (! string .IsNullOrEmpty(comment)) |
25 |
{ |
26 |
zos.SetComment(comment); //設置zip包的註釋 |
27 |
} |
28 |
29 |
//循環設置目錄下全部的*.jpg文件(支持子目錄搜索) |
30 |
foreach ( string file in Directory.GetFiles(topDirectoryName, "*.jpg" , SearchOption.AllDirectories)) |
31 |
{ |
32 |
if (File.Exists(file)) |
33 |
{ |
34 |
FileInfo item = new FileInfo(file); |
35 |
FileStream fs = File.OpenRead(item.FullName); |
36 |
byte [] buffer = new byte [fs.Length]; |
37 |
fs.Read(buffer, 0, buffer.Length); |
38 |
39 |
ZipEntry entry = new ZipEntry(item.Name); |
40 |
zos.PutNextEntry(entry); |
41 |
zos.Write(buffer, 0, buffer.Length); |
42 |
} |
43 |
} |
44 |
} |
45 |
} |
46 |
#endregion |
解壓縮包:
01 |
#region 解壓縮包(將壓縮包解壓到指定目錄) |
02 |
/// <summary> |
03 |
/// 解壓縮包(將壓縮包解壓到指定目錄) |
04 |
/// </summary> |
05 |
/// <param name="zipedFileName">壓縮包名稱</param> |
06 |
/// <param name="unZipDirectory">解壓縮目錄</param> |
07 |
/// <param name="password">密碼</param> |
08 |
public static void UnZipFiles( string zipedFileName, string unZipDirectory, string password) |
09 |
{ |
10 |
using (ZipInputStream zis = new ZipInputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) |
11 |
{ |
12 |
if (! string .IsNullOrEmpty(password)) |
13 |
{ |
14 |
zis.Password = password; //有加密文件的,能夠設置密碼解壓 |
15 |
} |
16 |
17 |
ZipEntry zipEntry; |
18 |
while ((zipEntry = zis.GetNextEntry()) != null ) |
19 |
{ |
20 |
string directoryName = Path.GetDirectoryName(unZipDirectory); |
21 |
string pathName = Path.GetDirectoryName(zipEntry.Name); |
22 |
string fileName = Path.GetFileName(zipEntry.Name); |
23 |
24 |
pathName = pathName.Replace( "." , "$" ); |
25 |
directoryName += "\\" + pathName; |
26 |
27 |
if (!Directory.Exists(directoryName)) |
28 |
{ |
29 |
Directory.CreateDirectory(directoryName); |
30 |
} |
31 |
32 |
if (! string .IsNullOrEmpty(fileName)) |
33 |
{ |
34 |
FileStream fs = File.Create(Path.Combine(directoryName, fileName)); |
35 |
int size = 2048; |
36 |
byte [] bytes = new byte [2048]; |
37 |
while ( true ) |
38 |
{ |
39 |
size = zis.Read(bytes, 0, bytes.Length); |
40 |
if (size > 0) |
41 |
{ |
42 |
fs.Write(bytes, 0, size); |
43 |
} |
44 |
else |
45 |
{ |
46 |
break ; |
47 |
} |
48 |
} |
49 |
fs.Close(); |
50 |
} |
51 |
} |
52 |
} |
53 |
} |
54 |
#endregion |
調用時咱們能夠這麼寫:
ZipFileUtil.ZipFiles(@"E:\\test\\", "E:\\test.zip", 1, "admin", "this is admin's comment.");//製做壓縮包
ZipFileUtil.UnZipFiles("E:\\test.zip", "E:\\guwei4037\\", "admin");//解壓縮包
來自:http://blog.csdn.net/chinacsharper/article/details/16807095