ICSharpCode.SharpZipLib.Zip使用示例代码及下载地址

ICSharpCode.SharpZipLib.Zip封装了ZIP文件在线压缩解压的一个dll,这里收集了ICSharpCode.SharpZipLib.Zip使用示例代码,方便有需要的人使用.

ContractedBlock.gif ExpandedBlockStart.gif Code
 1class ZIP
 2ExpandedBlockStart.gifContractedBlock.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)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 8            try
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            {
10
11                directory = directory.Replace("/""\\");
12
13                if (!directory.EndsWith("\\"))
14                    directory += "\\";
15                if (!Directory.Exists(directory))
16ExpandedSubBlockStart.gifContractedSubBlock.gif                {
17                    Directory.CreateDirectory(directory);
18                }

19                if (File.Exists(filename))
20ExpandedSubBlockStart.gifContractedSubBlock.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)
30ExpandedSubBlockStart.gifContractedSubBlock.gif            {
31                return false;
32            }

33        }

34
35
36
37ExpandedSubBlockStart.gifContractedSubBlock.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)
42ExpandedSubBlockStart.gifContractedSubBlock.gif        {
43            try
44ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
58ExpandedSubBlockStart.gifContractedSubBlock.gif                {
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)
67ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
68                        FileStream streamWriter = File.Create(dir + theEntry.Name);
69
70                        int size = 2048;
71                        byte[] data = new byte[2048];
72                        while (true)
73ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
74                            size = s.Read(data, 0, data.Length);
75                            if (size > 0)
76ExpandedSubBlockStart.gifContractedSubBlock.gif                            {
77                                streamWriter.Write(data, 0, size);
78                            }

79                            else
80ExpandedSubBlockStart.gifContractedSubBlock.gif                            {
81                                break;
82                            }

83                        }

84
85                        streamWriter.Close();
86                    }

87                }

88                s.Close();
89                return true;
90            }

91            catch (Exception)
92ExpandedSubBlockStart.gifContractedSubBlock.gif            {
93                return false;
94            }

95        }

96
97    }

 

ICSharpCode.SharpZipLib.Zip下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib/

转载于:https://www.cnblogs.com/lgh/archive/2009/03/09/1407163.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值