首先要引用一下類庫:using ionic.zip;這個類庫可以到網上下載。
下面對類庫使用的封裝方法:
得到指定的輸入流的zip壓縮流對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/// < summary > /// 得到指定的輸入流的zip壓縮流對象【原有流對象不會改變】 /// </ summary > /// < param name = "sourcestream" ></ param > /// < returns ></ returns > public static stream zipcompress(stream sourcestream, string entryname = "zip") { memorystream compressedstream = new memorystream(); if (sourcestream != null) { long sourceoldposition = 0; try { sourceoldposition = sourcestream.position; sourcestream.position = 0; using (zipfile zip = new zipfile()) { zip.addentry(entryname, sourcestream); zip.save(compressedstream); compressedstream.position = 0; } } catch { } finally { try { sourcestream.position = sourceoldposition; } catch { } } } return compressedstream; } |
得到指定的字節數組的zip解壓流對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/// < summary > /// 得到指定的字節數組的zip解壓流對象 /// 當前方法僅適用于只有一個壓縮文件的壓縮包,即方法內只取壓縮包中的第一個壓縮文件 /// </ summary > /// < param name = "sourcestream" ></ param > /// < returns ></ returns > public static stream zipdecompress(byte[] data) { stream decompressedstream = new memorystream(); if (data != null) { try { memorystream datastream = new memorystream(data); using (zipfile zip = zipfile.read(datastream)) { if (zip.entries.count > 0) { zip.entries.first().extract(decompressedstream); // extract方法中會操作ms,后續使用時必須先將stream位置歸零,否則會導致后續讀取不到任何數據 // 返回該stream對象之前進行一次位置歸零動作 decompressedstream.position = 0; } } } catch { } } return decompressedstream; } |
壓縮zip文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
/// < summary > /// 壓縮zip文件 /// 支持多文件和多目錄,或是多文件和多目錄一起壓縮 /// </ summary > /// < param name = "list" >待壓縮的文件或目錄集合</ param > /// < param name = "strzipname" >壓縮后的文件名</ param > /// < param name = "isdirstruct" >是否按目錄結構壓縮</ param > /// < returns >成功:true/失敗:false</ returns > public static bool compressmulti(list< string > list, string strzipname, bool isdirstruct) { try { using (zipfile zip = new zipfile(encoding.default))//設置編碼,解決壓縮文件時中文亂碼 { foreach (string path in list) { string filename = path.getfilename(path);//取目錄名稱 //如果是目錄 if (directory.exists(path)) { if (isdirstruct)//按目錄結構壓縮 { zip.adddirectory(path, filename); } else//目錄下的文件都壓縮到zip的根目錄 { zip.adddirectory(path); } } if (file.exists(path))//如果是文件 { zip.addfile(path,"imges"); } } zip.save(strzipname);//壓縮 return true; } } catch (exception) { return false; } } |
解壓zip文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/// < summary > /// 解壓zip文件 /// </ summary > /// < param name = "strzippath" >待解壓的zip文件</ param > /// < param name = "strunzippath" >解壓的目錄</ param > /// < param name = "overwrite" >是否覆蓋</ param > /// < returns >成功:true/失敗:false</ returns > public static bool decompression(string strzippath, string strunzippath, bool overwrite) { try { readoptions options = new readoptions(); options.encoding = encoding.default;//設置編碼,解決解壓文件時中文亂碼 using (zipfile zip = zipfile.read(strzippath, options)) { foreach (zipentry entry in zip) { if (string.isnullorempty(strunzippath)) { strunzippath = strzippath.split('.').first(); } if (overwrite) { entry.extract(strunzippath, extractexistingfileaction.overwritesilently);//解壓文件,如果已存在就覆蓋 } else { entry.extract(strunzippath, extractexistingfileaction.donotoverwrite);//解壓文件,如果已存在不覆蓋 } } return true; } } catch (exception) { return false; } } |
以上動圖由“圖斗羅”提供
這篇c#打包文件解壓縮的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/ldyblogs/archive/2017/11/21/package.html