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


相关推荐

  • marquee用到的属性

    marquee用到的属性一、marquee标签的几个重要属性:1.direction:滚动方向(包括4个值:up、down、left、right)说明:up:从下向上滚动;down:从上向下滚动;left:从右向左滚动;

    2022年7月2日
    27
  • 微信小程序图片预览功能_匿名聊天室小程序

    微信小程序图片预览功能_匿名聊天室小程序第一次写小程序,老板就让我用websoket写个聊天对话,群聊这种。第一次写聊天功能,第一次用websoket,第一次用小程序,这是在考验我吗?不过我还是研究了一下,终于实现了。首先看一下界面,界面很简单,就是首页刚进来获取了用户信息头像,昵称等。点击进入聊天室就可以聊天了,下面我介绍的是前端代码实现,后台需要做的很简单,就是你给他发送什么数据,他就给你返回什么数据,就是在接收前台发送过来的图…

    2022年8月30日
    3
  • pytest的assert_assert断言语句

    pytest的assert_assert断言语句前言断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢?简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试failed

    2022年7月28日
    9
  • 详解 CAP 定理 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性)

    详解 CAP 定理 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性)详解CAP定理Consistency(一致性)、Availability(可用性)、Partitiontolerance(分区容错性)CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性)、Availability(可用性)、Partitiontolerance(分区容错性),三者不可得兼。分布式系统(distributedsystem)正变得越来越重要,大型网站几乎都是分布式的。分布式系统的最大难点,就是各个节点的状态如何同步。CAP定理是.

    2022年7月25日
    18
  • k8s pod Evicted状态问题解决

    k8s pod Evicted状态问题解决kubectlgetpo-nkube-system-owide#查看pod运行状态信息可以看到有两个状态为驱赶,一般是磁盘空间不足了,清理slave磁盘空间后,状态正常

    2022年5月16日
    60
  • UART串口通信软件推荐

    UART串口通信软件推荐UART 串口通信软件推荐在我们调试单片机的时候 经常用到 UART 串口通信 没有足够的资金购入 LCD 屏 OLED 屏等显示器件 市面上这么多的串口调试软件实在是让人无从下手 下面安利 3 款串口调试软件 提供大家参考选择吧 numberone VOFA VOFA 原名伏特加 于 2018 年 10 月启动 代码配酒 bug 没有 Volt 伏特 Ohm 欧姆 Fala 法拉 Ampere 安培 是电气领域的基础单位 与他们的发明者 4 位电子物理学领域的科学巨人 分别同名 他们的首字母共同构成了 VOFA

    2025年10月29日
    3

发表回复

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

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