• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

UsingZipLibtocreateaZipFileinC#

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

System.IO.Compression.Net 2.0里与压缩有关的命名空间,但是使用起来并不是很方便。使用第3方库ziplib可以很方便地进行压缩类的操作。

      从[1] 下载动态库,然后在工程里Add Reference,把ICSharpCode.SharpZipLib.dll加进去。


在代码来创建一个zip包的例子如下(摘自ziplib sample code


using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;

using ICSharpCode.SharpZipLib.GZip;


把指定目录下的所有文件压缩到一个zip包里

        private void ZipTheReports()
        {
            string year = System.DateTime.Today.Year.ToString();
            string month = System.DateTime.Today.Month.ToString();
            string days = System.DateTime.Today.Day.ToString();    


            string[] filenames = Directory.GetFiles(curDir);


            string zipFileName = "Rplan Report " + year + month + days + ".zip";
            string zipAbsPath = this.curDir + zipFileName;


            zipRelativePath = zipFileName;


            Crc32 crc = new Crc32();
            ZipOutputStream s = new ZipOutputStream(File.Create(zipAbsPath)); //
指定zip文件的绝对路径,包括文件名

           

            s.SetLevel(6); // 0 - store only to 9 - means best compression


            foreach (string file in filenames)
            {
                FileStream fs = File.OpenRead(file); //
文件的绝对路径,包括文件名


                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                ZipEntry entry = new ZipEntry(extractFileName(file)); //这里ZipEntry的参数必须是相对路径名,表示文件在zip文档里的相对路径


                entry.DateTime = DateTime.Now;

 

               // set Size and the crc, because the information

                // about the size and crc should be stored in the header

                // if it is not set it is automatically written in the footer.

                // (in this case size == crc == -1 in the header)

                // Some ZIP programs have problems with zip files that don't store

                // the size and crc in the header.

                entry.Size = fs.Length;
                fs.Close();


                crc.Reset();

                crc.Update(buffer);


                entry.Crc = crc.Value;


                s.PutNextEntry(entry);

 

               s.Write(buffer, 0, buffer.Length);


            }


            s.Finish();
            s.Close();

        }

 

  // e.g. convert  c:\\temp\test.xls to test.xls

       private string extractFileName(string filePath)
        {

  

            int index1 = filePath.LastIndexOf("\\");


            string fileName =

                filePath.Substring(index1+1);

 

            return fileName;
        }


 

Reference

1ZipLib
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
A port of zlib library to C#. Its license allows developers to include this library in commercial, closed-source applications.


2. System.IO.Compression Namespace
http://msdn2.microsoft.com/en-us/library/system.io.compression.aspx


DeflateStream Class
http://msdn2.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx


3Compression Application Sample
http://msdn2.microsoft.com/en-us/library/ywf6dxhx.aspx

This sample demonstrates compression capabilities available in the .NET Framework. It builds a Windows Forms application that employs the GZipStream and DeflateStream types to compress and decompress files. The sample also introduces several types that are new in the .NET Framework version 2.0.

 

4. Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C#

http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/default.aspx


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#调用oracle存储过程发布时间:2022-07-14
下一篇:
c#Request.Params与Request.QueryString有什么区别发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap