程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#實現壓縮/解壓功能實現代碼

C#實現壓縮/解壓功能實現代碼

編輯:關於C#
 

(一). 實現功能


對文件及目錄的壓縮及解壓功能


(二). 運行圖片示例

 


(三).代碼

1. 壓縮類

 

1/// <summary>
2/// 壓縮類
3/// </summary>
4public class ZipClass
5{
6 public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
7 {
8 //如果文件沒有找到,則報錯
9 if (!System.IO.File.Exists(FileToZip))
10 {
11 throw new System.IO.FileNotFoundException("指定要壓縮的文件: " + FileToZip + " 不存在!");
12 }
13
14 System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
15 System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
16 ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
17 ZipEntry ZipEntry = new ZipEntry("ZippedFile");
18 ZipStream.PutNextEntry(ZipEntry);
19 ZipStream.SetLevel(CompressionLevel);
20 byte[] buffer = new byte[BlockSize];
21 System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
22 ZipStream.Write(buffer, 0, size);
23 try
24 {
25 while (size < StreamToZip.Length)
26 {
27 int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
28 ZipStream.Write(buffer, 0, sizeRead);
29 size += sizeRead;
30 }
31 }
32 catch (System.Exception ex)
33 {
34 throw ex;
35 }
36 ZipStream.Finish();
37 ZipStream.Close();
38 StreamToZip.Close();
39 }
40
41 /// <summary>
42 /// 壓縮目錄
43 /// </summary>
44 /// <param name="args">數組(數組[0]: 要壓縮的目錄; 數組[1]: 壓縮的文件名)</param>
45 public static void ZipFileDictory(string[] args)
46 {
47 string[] filenames = Directory.GetFiles(args[0]);
48
49 Crc32 crc = new Crc32();
50 ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
51 s.SetLevel(6);
52 foreach (string file in filenames)
53 {
54 //打開壓縮文件
55 FileStream fs = File.OpenRead(file);
56
57 byte[] buffer = new byte[fs.Length];
58 fs.Read(buffer, 0, buffer.Length);
59 ZipEntry entry = new ZipEntry(file);
60
61 entry.DateTime = DateTime.Now;
62
63 entry.Size = fs.Length;
64 fs.Close();
65
66 crc.Reset();
67 crc.Update(buffer);
68
69 entry.Crc = crc.Value;
70
71 s.PutNextEntry(entry);
72
73 s.Write(buffer, 0, buffer.Length);
74
75 }
76
77 s.Finish();
78 s.Close();
79 }
80
81 /// <summary>
82 /// 壓縮文件
83 /// </summary>
84 /// <param name="FileToZip">要進行壓縮的文件名</param>
85 /// <param name="ZipedFile">壓縮後生成的壓縮文件名</param>
86 public static void ZipFile(string FileToZip, string ZipedFile)
87 {
88 //如果文件沒有找到,則報錯
89 if (!File.Exists(FileToZip))
90 {
91 throw new System.IO.FileNotFoundException("指定要壓縮的文件: " + FileToZip + " 不存在!");
92 }
93 FileStream fs = File.OpenRead(FileToZip);
94 byte[] buffer = new byte[fs.Length];
95 fs.Read(buffer, 0, buffer.Length);
96 fs.Close();
97
98 FileStream ZipFile = File.Create(ZipedFile);
99 ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
100 ZipEntry ZipEntry = new ZipEntry("ZippedFile");
101 ZipStream.PutNextEntry(ZipEntry);
102 ZipStream.SetLevel(6);
103
104 ZipStream.Write(buffer, 0, buffer.Length);
105 ZipStream.Finish();
106 ZipStream.Close();
107 }
108}
109
110/// <summary>
111/// 解壓類
112/// </summary>
113public class UnZipClass
114{
115 /// <summary>
116 /// 解壓功能(解壓壓縮文件到指定目錄)
117 /// </summary>
118 /// <param name="args">待解壓的文件</param>
119 /// <param name="args">指定解壓目標目錄</param>
120 public static void UnZip(string[] args)
121 {
122 ZipInputStream s = new ZipInputStream(File.OpenRead(@args[0].Trim()));
123 ZipEntry theEntry;
124 string directoryName = Path.GetDirectoryName(@args[1].Trim());
125
126 if (!Directory.Exists(@args[1].Trim()))
127 {
128 Directory.CreateDirectory(directoryName);
129 }
130 while ((theEntry = s.GetNextEntry()) != null)
131 {
132 ;
133 string fileName = Path.GetFileName(theEntry.Name);
134
135 if (fileName != String.Empty)
136 {
137 FileStream streamWriter = File.Create(@args[1].Trim() + fileName);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved