利用 SharpZipLib方便地压缩和解压缩文件

利用 SharpZipLib方便地压缩和解压缩文件
最新版本的SharpZipLib(0.84)增加了很多新的功能,其中包括增加了FastZip类,这让我们可以非常方便地把一个目录压缩成一个压缩包,经测试可以很好地支持文件中包含中文以及空格的情况。

 1 ExpandedBlockStart.gif ContractedBlock.gif     /**/ /// <summary>
 2InBlock.gif    /// Create a zip archive.
 3InBlock.gif    /// </summary>
 4InBlock.gif    /// <param name="filename">The filename.</param>
 5ExpandedBlockEnd.gif    /// <param name="directory">The directory to zip.</param> 

 6 None.gif      public   static   void  PackFiles( string  filename,  string  directory)
 7 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 8InBlock.gif        try
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10InBlock.gif            FastZip fz = new FastZip();
11InBlock.gif            fz.CreateEmptyDirectories = true;
12InBlock.gif            fz.CreateZip(filename, directory, true"");
13InBlock.gif            fz = null;
14ExpandedSubBlockEnd.gif        }

15InBlock.gif        catch (Exception)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            throw;
18ExpandedSubBlockEnd.gif        }

19ExpandedBlockEnd.gif    }

20 None.gif
21 ExpandedBlockStart.gifContractedBlock.gif     /**/ /// <summary>
22InBlock.gif    /// Unpacks the files.
23InBlock.gif    /// </summary>
24InBlock.gif    /// <param name="file">The file.</param>
25ExpandedBlockEnd.gif    /// <returns>if succeed return true,otherwise false.</returns>

26 None.gif      public   static   bool  UnpackFiles( string  file,  string  dir)
27 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
28InBlock.gif        try
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            if (!Directory.Exists(dir))
31InBlock.gif                Directory.CreateDirectory(dir);
32InBlock.gif
33InBlock.gif            ZipInputStream s = new ZipInputStream(File.OpenRead(file));
34InBlock.gif
35InBlock.gif            ZipEntry theEntry;
36InBlock.gif            while ((theEntry = s.GetNextEntry()) != null)
37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
38InBlock.gif
39InBlock.gif                string directoryName = Path.GetDirectoryName(theEntry.Name);
40InBlock.gif                string fileName = Path.GetFileName(theEntry.Name);
41InBlock.gif
42InBlock.gif                if (directoryName != String.Empty)
43InBlock.gif                    Directory.CreateDirectory(dir + directoryName);
44InBlock.gif
45InBlock.gif                if (fileName != String.Empty)
46ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
47InBlock.gif                    FileStream streamWriter = File.Create(dir + theEntry.Name);
48InBlock.gif
49InBlock.gif                    int size = 2048;
50InBlock.gif                    byte[] data = new byte[2048];
51InBlock.gif                    while (true)
52ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
53InBlock.gif                        size = s.Read(data, 0, data.Length);
54InBlock.gif                        if (size > 0)
55ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
56InBlock.gif                            streamWriter.Write(data, 0, size);
57ExpandedSubBlockEnd.gif                        }

58InBlock.gif                        else
59ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
60InBlock.gif                            break;
61ExpandedSubBlockEnd.gif                        }

62ExpandedSubBlockEnd.gif                    }

63InBlock.gif
64InBlock.gif                    streamWriter.Close();
65ExpandedSubBlockEnd.gif                }

66ExpandedSubBlockEnd.gif            }

67InBlock.gif            s.Close();
68InBlock.gif            return true;
69ExpandedSubBlockEnd.gif        }

70InBlock.gif        catch (Exception)
71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
72InBlock.gif            throw;
73ExpandedSubBlockEnd.gif        }

上面代码需要引用:

None.gif using  ICSharpCode.SharpZipLib.Zip;
None.gif
using  System.IO;

使用方法就不介绍了。

你可以到其官方网站下载最新版本的SharpZipLib:

http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
或者直接使用该链接下载:

  • Compiled assembly with documentation and install scripts Download [488 KB]
  • Source code and samples Download [622 KB]
  • Help file only Download [485 KB]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值