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


相关推荐

  • 【转载】互联网架构,如何进行容量设计?

    【转载】互联网架构,如何进行容量设计?

    2021年11月18日
    49
  • Matlab 非线性有约束规划的粒子群算法「建议收藏」

    Matlab 非线性有约束规划的粒子群算法「建议收藏」粒子群算法的基本认识简单介绍:通过群体中个体之间的协作和信息共享来寻找最优解。适用于连续函数极值问题,对于非线性,多峰问题均有较强的全局搜索能力。主要掌握两点1.粒子的速度和位置速度代表移动的快慢,位置代表移动的方向。位置对应每个自变量,速度一般设置为变量范围的10%~20%。2.粒子的更新规则具体实例下面展示matlab代码。clear;close;clc%%约束条件和目标函数构建fun=@(x)x(1)^2+x(2)^2+x(3)^2+8;bind1

    2022年6月1日
    44
  • Python学习:关键字global和nonlocal的用法说明

    Python学习:关键字global和nonlocal的用法说明一、globalglobal关键字用来在函数或其他局部作用域中使用全局变量。1.1如果局部要对全局变量修改,而不使用global关键字。count=0defglobal_test():count+=1print(count)global_test()会出现如下错误:1.2如果局部要对全局变量修改,应在局部声明该全局变量。co…

    2025年9月22日
    4
  • js正则截取指定字符串_java正则表达式提取字符串

    js正则截取指定字符串_java正则表达式提取字符串一、javascript正则表达式的基本知识1javascript正则对象创建和用法声明javascript正则表达式varreCat=newRegExp(“cat”);你也可以varreCat=/cat/;//Perl风格(推荐)2学习最常用的testexecmatchsearchreplacesplit6个方法1)test检查指定的字符串是否存在vardata=“123123″;varreCat=/123/gi;alert(r

    2022年9月20日
    2
  • group by 和 order by 的区别 + 理解过程

    group by 和 order by 的区别 + 理解过程orderby和groupby的区别order by 和 group by 的区别:1,order by 从英文里理解就是行的排序方式,默认的为升序。 order by 后面必须列出排序的字段名,可以是多个字段名。2,group by 从英文里理解就是分组。必须有“聚合函数”来配合才能使用,使用时至少需要一个分组标志字段。注意:聚合函数是—sum()、count()、…

    2022年5月9日
    40
  • 迁移学习与代码举例

    迁移学习出现背景在有监督的机器学习和尤其是深度学习的场景应用中,需要大量的标注数据。标注数据是一项枯燥无味且花费巨大的任务,关键是现实场景中,往往无法标注足够的数据。而且模型的训练是极其耗时的。因此迁移学习营运而生。传统机器学习(主要指监督学习)基于同分布假设需要大量标注数据然而实际使用过程中不同数据集可能存在一些问题,比如数据分布差异标注数据过期训练数据过期,也就是好不容易标定…

    2022年4月15日
    115

发表回复

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

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