ICSharpCode.SharpZipLib.Zip封装了ZIP文件在线压缩解压的一个dll,这里收集了ICSharpCode.SharpZipLib.Zip使用示例代码,方便有需要的人使用.
Code
1
class ZIP
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{/**//// <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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
try
9![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
directory = directory.Replace("/", "\\");
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
if (!directory.EndsWith("\\"))
14
directory += "\\";
15
if (!Directory.Exists(directory))
16![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
Directory.CreateDirectory(directory);
18
}
19
if (File.Exists(filename))
20![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
return false;
32
}
33
}
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
try
44![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
if (!File.Exists(file))
46
return false;
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
dir = dir.Replace("/", "\\");
49
if (!dir.EndsWith("\\"))
50
dir += "\\";
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
if (!Directory.Exists(dir))
53
Directory.CreateDirectory(dir);
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
ZipInputStream s = new ZipInputStream(File.OpenRead(file));
56
ZipEntry theEntry;
57
while ((theEntry = s.GetNextEntry()) != null)
58![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
string directoryName = Path.GetDirectoryName(theEntry.Name);
61
string fileName = Path.GetFileName(theEntry.Name);
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
if (directoryName != String.Empty)
64
Directory.CreateDirectory(dir + directoryName);
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
if (fileName != String.Empty)
67![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
FileStream streamWriter = File.Create(dir + theEntry.Name);
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
int size = 2048;
71
byte[] data = new byte[2048];
72
while (true)
73![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
size = s.Read(data, 0, data.Length);
75
if (size > 0)
76![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
streamWriter.Write(data, 0, size);
78
}
79
else
80![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
break;
82
}
83
}
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
streamWriter.Close();
86
}
87
}
88
s.Close();
89
return true;
90
}
91
catch (Exception)
92![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
return false;
94
}
95
}
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
}
ICSharpCode.SharpZipLib.Zip下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib/