Using ZipLib to create a Zip File in C#

Using ZipLib to create a Zip File in C#System.IO.Compression是.Net2.0里与压缩有关的命名空间,但是使用起来并不是很方便。使用第3方库ziplib可以很方便地进行压缩类的操作。从[1]下载动态库,然后在工程里AddReference,把ICSharpCode.SharpZipLib.dll加进去。在代码来创建一个zip包的例子如下(摘自ziplibsamplecode)…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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

转载于:https://www.cnblogs.com/yuquanlaobo/archive/2007/01/17/622851.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159448.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 三十名网友共同自主研发粤语打字软件「建议收藏」

    三十名网友共同自主研发粤语打字软件「建议收藏」来源:羊城晚报 日期:2007-7-23  王许乐是厚街镇前进小学的语文教师。2005年底,他和网上其他29人一起用半年时间研发了一套粤语打字软件,在网友中大受欢迎,下载量过万。王许乐等人研发的这套轻松粤拼输入法目前已经推出了两个版本,他们正打算推出进一步改良版。 王许乐一直致力于粤语研究。2005年底,一个偶然的机会,他在网络上认识了一大批热爱粤语的人,大家一起交流从简单的粤语方言到省

    2022年7月16日
    14
  • Android 4.4 Kitkat 使能 USB adb 功能

    Android 4.4 Kitkat 使能 USB adb 功能

    2021年12月2日
    59
  • TCP报文段格式[通俗易懂]

    三次握手就是一次TCP建立链接的过程四次挥手就是一次TCP断开的过程所以在学习三次握手四次挥手之前先了解一下TCP报文段的格式源端口(2字节):发送端应用程序的端口号,与源IP地址确定一个唯一地址目的端口(2字节):接收端计算机应用程序的端口号,与目的IP地址确定唯一的地址序号(4字节):TCP是面向字节流传输的,他为每一个字节编了一个序号,该报文段中序号为传输数据第一个字节的序号,例如:一个报文…

    2022年4月17日
    89
  • Rewritecond介绍「建议收藏」

    Rewritecond介绍「建议收藏」RewriteCondSyntax:RewriteCondTestStringCondPattern[flags]  RewriteCond指令定义一条规则条件。在一条RewriteRule指令前面可能会有一条或多条RewriteCond指令,只有当自身的模板(pattern)匹配成功且这些条件也满足时规则才被应用于当前URL处理。  TestString是一个字符串,除了包含普通的

    2022年6月13日
    17
  • prepareStatement使用「建议收藏」

    prepareStatement使用「建议收藏」publicbooleanlogin2(Stringusername,Stringpassword)throwsException{ Stringurl=”jdbc:mysql://localhost:3306/mydb1″; Class.forName(“com.mysql.jdbc.Driver”); StringsqlUsername=”root”;

    2022年6月2日
    46
  • sql面试题大全[通俗易懂]

    sql面试题大全[通俗易懂]Sql常见面试题(总结)1.用一条SQL语句查询出每门课都大于80分的学生姓名 name  kecheng  fenshu张三    语文      81张三    数学      75李四    语文      76李四    数学      90王五    语文      81王五    数学      100王五    英语      90A:selectdist…

    2022年8月26日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号