ICSharpCode.SharpZipLib.Zip使用示例代码及下载地址
ICSharpCode.SharpZipLib.Zip使用示例代码及下载地址
ICSharpCode.SharpZipLib.Zip封装了ZIP文件在线压缩解压的一个dll,这里收集了ICSharpCode.SharpZipLib.Zip使用示例代码,方便有需要的人使用.
Code
1class ZIP
2 {/**//// <summary>压缩文件</summary>
3 /// <param name="filename">filename生成的文件的名称,如:C\123\123.zip</param>
4 /// <param name="directory">directory要压缩的文件夹路径</param>
5 /// <returns></returns>
6 public static bool PackFiles(string filename, string directory)
7 {
8 try
9 {
10
11 directory = directory.Replace("/", "\\");
12
13 if (!directory.EndsWith("\\"))
14 directory += "\\";
15 if (!Directory.Exists(directory))
16 {
17 Directory.CreateDirectory(directory);
18 }
19 if (File.Exists(filename))
20 {
21 File.Delete(filename);
22 }
23 //ICSharpCode.SharpZipLib.Zip.ZipFile pp = new ZipFile();
24 //FastZip fz = new FastZip();
25 //fz.CreateEmptyDirectories = true;
26 //fz.CreateZip(filename, directory, true, "");
27 return true;
28 }
29 catch (Exception)
30 {
31 return false;
32 }
33 }
34
35
36
37 /**//// <summary>解压文件</summary>
38 /// <param name="file">压缩文件的名称,如:C:\123\123.zip</param>
39 /// <param name="dir">dir要解压的文件夹路径</param>
40 /// <returns></returns>
41 public static bool UnpackFiles(string file, string dir)
42 {
43 try
44 {
45 if (!File.Exists(file))
46 return false;
47
48 dir = dir.Replace("/", "\\");
49 if (!dir.EndsWith("\\"))
50 dir += "\\";
51
52 if (!Directory.Exists(dir))
53 Directory.CreateDirectory(dir);
54
55 ZipInputStream s = new ZipInputStream(File.OpenRead(file));
56 ZipEntry theEntry;
57 while ((theEntry = s.GetNextEntry()) != null)
58 {
59
60 string directoryName = Path.GetDirectoryName(theEntry.Name);
61 string fileName = Path.GetFileName(theEntry.Name);
62
63 if (directoryName != String.Empty)
64 Directory.CreateDirectory(dir + directoryName);
65
66 if (fileName != String.Empty)
67 {
68 FileStream streamWriter = File.Create(dir + theEntry.Name);
69
70 int size = 2048;
71 byte[] data = new byte[2048];
72 while (true)
73 {
74 size = s.Read(data, 0, data.Length);
75 if (size > 0)
76 {
77 streamWriter.Write(data, 0, size);
78 }
79 else
80 {
81 break;
82 }
83 }
84
85 streamWriter.Close();
86 }
87 }
88 s.Close();
89 return true;
90 }
91 catch (Exception)
92 {
93 return false;
94 }
95 }
96
97 }
1class ZIP
2 {/**//// <summary>压缩文件</summary>
3 /// <param name="filename">filename生成的文件的名称,如:C\123\123.zip</param>
4 /// <param name="directory">directory要压缩的文件夹路径</param>
5 /// <returns></returns>
6 public static bool PackFiles(string filename, string directory)
7 {
8 try
9 {
10
11 directory = directory.Replace("/", "\\");
12
13 if (!directory.EndsWith("\\"))
14 directory += "\\";
15 if (!Directory.Exists(directory))
16 {
17 Directory.CreateDirectory(directory);
18 }
19 if (File.Exists(filename))
20 {
21 File.Delete(filename);
22 }
23 //ICSharpCode.SharpZipLib.Zip.ZipFile pp = new ZipFile();
24 //FastZip fz = new FastZip();
25 //fz.CreateEmptyDirectories = true;
26 //fz.CreateZip(filename, directory, true, "");
27 return true;
28 }
29 catch (Exception)
30 {
31 return false;
32 }
33 }
34
35
36
37 /**//// <summary>解压文件</summary>
38 /// <param name="file">压缩文件的名称,如:C:\123\123.zip</param>
39 /// <param name="dir">dir要解压的文件夹路径</param>
40 /// <returns></returns>
41 public static bool UnpackFiles(string file, string dir)
42 {
43 try
44 {
45 if (!File.Exists(file))
46 return false;
47
48 dir = dir.Replace("/", "\\");
49 if (!dir.EndsWith("\\"))
50 dir += "\\";
51
52 if (!Directory.Exists(dir))
53 Directory.CreateDirectory(dir);
54
55 ZipInputStream s = new ZipInputStream(File.OpenRead(file));
56 ZipEntry theEntry;
57 while ((theEntry = s.GetNextEntry()) != null)
58 {
59
60 string directoryName = Path.GetDirectoryName(theEntry.Name);
61 string fileName = Path.GetFileName(theEntry.Name);
62
63 if (directoryName != String.Empty)
64 Directory.CreateDirectory(dir + directoryName);
65
66 if (fileName != String.Empty)
67 {
68 FileStream streamWriter = File.Create(dir + theEntry.Name);
69
70 int size = 2048;
71 byte[] data = new byte[2048];
72 while (true)
73 {
74 size = s.Read(data, 0, data.Length);
75 if (size > 0)
76 {
77 streamWriter.Write(data, 0, size);
78 }
79 else
80 {
81 break;
82 }
83 }
84
85 streamWriter.Close();
86 }
87 }
88 s.Close();
89 return true;
90 }
91 catch (Exception)
92 {
93 return false;
94 }
95 }
96
97 }
ICSharpCode.SharpZipLib.Zip下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib/