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)
上一篇 2022年7月26日 下午12:00
下一篇 2022年7月26日 下午12:00


相关推荐

  • TP框架的目录结构总结

    TP框架的目录结构总结用 thinkphp 开发了好些项目了 最近准备抽空写一些经验总结 希望能给刚开始接触 tp 的童鞋们提供一些开发的方案 少走一些弯路 少踩一些坑 这些绝对都是些精华干货 耐着性子阅读 相信肯定是会有收获的 先从 thinkphp 的目录架构开始吧 koudaigaoxia Application 项目逻辑目录 Common 公共模块

    2026年3月17日
    1
  • Android自己定义控件系列二:自己定义开关button(一)「建议收藏」

    Android自己定义控件系列二:自己定义开关button(一)

    2022年2月6日
    129
  • Inherits语句

    Inherits语句使当前类或接口继承另一个类或一组接口的属性 Attribute 变量 属性 Property 过程和事件 Inheritsbase 各部分说明 basetypename 必选 此类派生自的类的名称 或 此接口派生自的接口的名称 可使用逗号分隔多个名称 备注如果

    2026年3月19日
    1
  • ArrayList和LinkedList区别?看完秒懂~

    ArrayList和LinkedList区别?看完秒懂~工作中 大家是不是经常分不清楚 什么时候用 ArrayList 什么时候用 LinkedList

    2026年3月18日
    1
  • 因果图法用例设计

    因果图法用例设计等价类划分法和边界值法着重考虑输入条件 而不考虑输入条件的组合 决策表考虑了输入条件的组合情况 但没有考虑输入条件之间的相互制约的关系 在查看程序规格说明时 如果发现输入之间有关系 相应会产生多个动作 需要考虑条件组合的情况 又发现条件之间存在相互制约 可以考虑使用因果图法 因果图法概述 1 因果图法相关概念因果图法是一种适合于描述对于多种条件的组合 相应产生多个动作的形式的方法 对照规格说明书利用图解法分析输入条件的组合 约束关系和输出条件的因果关系 从而设计测试用例的方法 它适合于检查程

    2026年3月18日
    2
  • maxscript命令

    maxscript命令————————————————————-Black————————————————————-APEXSaveFBXActionCreateFlowActionEditFlowActionExtendFlowAc

    2025年6月29日
    5

发表回复

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

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