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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • idea2022.01.13激活码永久[最新免费获取]2022.02.05

    (idea2022.01.13激活码永久)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html76VF8VOVJM-eyJsaWNlbnNlSW…

    2022年4月1日
    100
  • java游戏开发实例,吐血整理「建议收藏」

    java游戏开发实例,吐血整理「建议收藏」专题1:JavaOOP1、什么是B/S架构?什么是C/S架构2、Java都有哪些开发平台?3、什么是JDK?什么是JRE?4、Java语言有哪些特点5、面向对象和面向过程的区别6、什么是数据结构?7、Java的数据结构有哪些?8、什么是OOP?9、类与对象的关系?10、Java中有几种数据类型11、标识符的命名规则。12、instanceof关键字的作用13、什么是隐式转换,什么是显式转换14、Char类型能不能转成int类

    2022年7月7日
    40
  • 【实战】工作中常用的PHP 操作 Redis 的基本方法「建议收藏」

    【实战】工作中常用的PHP 操作 Redis 的基本方法

    2022年2月11日
    46
  • python中dtype object_python的dtype有几种

    python中dtype object_python的dtype有几种NumPy中定义的不同标量数据类型。云海天教程网,大量的免费python教程,欢迎在线学习!NumPy数字类型是dtype(数据类型)对象的实例,每个对象具有唯一的特征。这些类型可以是np.bool_,np.float32等。数据类型对象(dtype)数据类型对象描述了对应于数组的固定内存块的解释,取决于以下方面:数据类型(整数、浮点或者Python对象)数据大小字节序(小端或大端)在结…

    2022年6月2日
    73
  • C#ThreadPool.QueueUserWorkItem实例「建议收藏」

    C#ThreadPool.QueueUserWorkItem实例「建议收藏」今天学习线程池的时候发现,网上能搜到的都是很久以前的文档了,大家都是照搬过去,有没有考证都是问题。经过测试结果已经和他们说的不一样了,比如 Listactions=newList(){()=>{Console.WriteLine(“A-1”);},()=>{Conso

    2022年9月24日
    0
  • 阿里核心系统团队博客在哪_阿里云是谁创始人

    阿里核心系统团队博客在哪_阿里云是谁创始人http://csrd.aliapp.com/阿里核心系统团队博客基础极致分享Home招聘信息阿里核心系统团队介绍TFS运维平台改造1604天bylinqinginTFSTFS负责运维的同学在工作过程中,积累了各种运维脚本

    2022年8月13日
    2

发表回复

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

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