当前位置:首页 > 科技  > 软件

搞懂C#文件压缩:SharpZipLib vs. DotNetZip,实用代码一网打尽!

来源: 责编: 时间:2024-03-20 17:27:43 328观看
导读1. SharpZipLib功能:支持ZIP和GZip格式的压缩和解压缩。提供了对Tar和BZip2格式的支持。轻量级,易于使用。优点:开源,广泛使用。灵活性较高,适用于多种压缩需求。使用实例:using System;using ICSharpCode.SharpZipLib.Zip;

UC528资讯网——每日最新资讯28at.com

1. SharpZipLib

功能:

  • 支持ZIP和GZip格式的压缩和解压缩。
  • 提供了对Tar和BZip2格式的支持。
  • 轻量级,易于使用。

优点:

  • 开源,广泛使用。
  • 灵活性较高,适用于多种压缩需求。

使用实例:

using System;using ICSharpCode.SharpZipLib.Zip;class Program{    static void Main()    {        string sourceFolder = @"C:/Path/To/Your/Folder";        string zipFile = @"C:/Path/To/Your/Archive.zip";        ZipDirectory(sourceFolder, zipFile);        Console.WriteLine("Compression completed.");        string extractFolder = @"C:/Path/To/Your/Extracted";        UnzipFile(zipFile, extractFolder);        Console.WriteLine("Extraction completed.");    }    static void ZipDirectory(string sourceFolder, string zipFile)    {        using (ZipOutputStream zipStream = new ZipOutputStream(System.IO.File.Create(zipFile)))        {            zipStream.SetLevel(9); // 0 - store only to 9 - means best compression            ZipFolder(sourceFolder, sourceFolder, zipStream);            zipStream.Finish();            zipStream.Close();        }    }    static void ZipFolder(string rootFolder, string currentFolder, ZipOutputStream zipStream)    {        string[] files = System.IO.Directory.GetFiles(currentFolder);        foreach (string file in files)        {            ZipFile(zipStream, currentFolder, file);        }        string[] subFolders = System.IO.Directory.GetDirectories(currentFolder);        foreach (string folder in subFolders)        {            ZipFolder(rootFolder, folder, zipStream);        }    }    static void ZipFile(ZipOutputStream zipStream, string rootFolder, string filePath)    {        byte[] buffer = new byte[4096];        string relativePath = filePath.Substring(rootFolder.Length + 1);        ZipEntry entry = new ZipEntry(relativePath);        zipStream.PutNextEntry(entry);        using (System.IO.FileStream fs = System.IO.File.OpenRead(filePath))        {            int sourceBytes;            do            {                sourceBytes = fs.Read(buffer, 0, buffer.Length);                zipStream.Write(buffer, 0, sourceBytes);            } while (sourceBytes > 0);        }    }    static void UnzipFile(string zipFile, string extractFolder)    {        using (ZipInputStream zipStream = new ZipInputStream(System.IO.File.OpenRead(zipFile)))        {            ZipEntry entry;            while ((entry = zipStream.GetNextEntry()) != null)            {                string entryName = entry.Name;                string fullZipToPath = System.IO.Path.Combine(extractFolder, entryName);                string directoryName = System.IO.Path.GetDirectoryName(fullZipToPath);                if (directoryName.Length > 0)                {                    System.IO.Directory.CreateDirectory(directoryName);                }                if (entry.IsFile)                {                    byte[] buffer = new byte[4096];                    using (System.IO.FileStream streamWriter = System.IO.File.Create(fullZipToPath))                    {                        int sourceBytes;                        do                        {                            sourceBytes = zipStream.Read(buffer, 0, buffer.Length);                            streamWriter.Write(buffer, 0, sourceBytes);                        } while (sourceBytes > 0);                    }                }            }        }    }}

2. DotNetZip

功能:

  • 支持ZIP格式的压缩和解压缩。
  • 提供了对多卷和自解压缩ZIP文件的支持。
  • 具有更简单的API,易于使用。

优点:

  • 使用方便,简洁明了。
  • 集成度高,适合快速实现文件压缩解压缩功能。

使用实例:

using System;using Ionic.Zip;class Program{    static void Main()    {        string sourceFolder = @"C:/Path/To/Your/Folder";        string zipFile = @"C:/Path/To/Your/Archive.zip";        ZipDirectory(sourceFolder, zipFile);        Console.WriteLine("Compression completed.");        string extractFolder = @"C:/Path/To/Your/Extracted";        UnzipFile(zipFile, extractFolder);        Console.WriteLine("Extraction completed.");    }    static void ZipDirectory(string sourceFolder, string zipFile)    {        using (ZipFile zip = new ZipFile())        {            zip.AddDirectory(sourceFolder);            zip.Save(zipFile);        }    }    static void UnzipFile(string zipFile, string extractFolder)    {        using (ZipFile zip = ZipFile.Read(zipFile))        {            zip.ExtractAll(extractFolder, ExtractExistingFileAction.OverwriteSilently);        }    }}

以上两个例子都提供了基本的目录压缩和解压缩功能,你可以根据具体需求进行进一步定制。确保在实际项目中进行充分的测试和适当的错误处理。UC528资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-78147-0.html搞懂C#文件压缩:SharpZipLib vs. DotNetZip,实用代码一网打尽!

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: Go 团队近两年在做什么,AI 方面如何发力?

下一篇: 行业机构 SEMI:全球 12 英寸晶圆厂设备投资 2025 年将破千亿美元大关

标签:
  • 热门焦点
  • K60 Pro官方停产 第三方瞬间涨价

    虽然没有官方宣布,但Redmi的一些高管也已经透露了,Redmi K60 Pro已经停产且不会补货,这一切都是为了即将到来的K60 Ultra铺路,属于厂家的正常操作。但有意思的是该机在停产之后
  • 量化指标是与非:挽救被量化指标扼杀的技术团队

    作者 | 刘新翠整理 | 徐杰承本文整理自快狗打车技术总监刘新翠在WOT2023大会上的主题分享,更多精彩内容及现场PPT,请关注51CTO技术栈公众号,发消息【WOT2023PPT】即可直接领取
  • 多线程开发带来的问题与解决方法

    使用多线程主要会带来以下几个问题:(一)线程安全问题  线程安全问题指的是在某一线程从开始访问到结束访问某一数据期间,该数据被其他的线程所修改,那么对于当前线程而言,该线程
  • 19个 JavaScript 单行代码技巧,让你看起来像个专业人士

    今天这篇文章跟大家分享18个JS单行代码,你只需花几分钟时间,即可帮助您了解一些您可能不知道的 JS 知识,如果您已经知道了,就当作复习一下,古人云,温故而知新嘛。现在,我们就开始今
  • 每天一道面试题-CPU伪共享

    前言:了不起:又到了每天一到面试题的时候了!学弟,最近学习的怎么样啊 了不起学弟:最近学习的还不错,每天都在学习,每天都在进步! 了不起:那你最近学习的什么呢? 了不起学弟:最近在学习C
  • 2天涨粉255万,又一赛道在抖音爆火

    来源:运营研究社作者 | 张知白编辑 | 杨佩汶设计 | 晏谈梦洁这个暑期,旅游赛道彻底火了:有的「地方」火了——贵州村超旅游收入 1 个月超过 12 亿;有的「博主」火了&m
  • 联想YOGA 16s 2022笔记本将要推出,屏幕支持触控功能

    联想此前宣布,将于11月2日19:30召开联想秋季轻薄新品发布会,推出联想 YOGA 16s 2022 笔记本等新品。官方称,YOGA 16s 2022 笔记本将搭载 16 英寸屏幕,并且是一
  • 荣耀Magicbook V 14 2021曙光蓝版本正式开售,拥有触摸屏

    荣耀 Magicbook V 14 2021 曙光蓝版本正式开售,搭载 i7-11390H 处理器与 MX450 显卡,配备 16GB 内存与 512GB SSD,重 1.48kg,厚 14.5mm,具有 1.5mm 键盘键程、
  • 中关村论坛11月25日开幕,15位诺奖级大咖将发表演讲

    11月18日,记者从2022中关村论坛新闻发布会上获悉,中关村论坛将于11月25至30日在京举行。本届中关村论坛由科学技术部、国家发展改革委、工业和信息化部、国务
Top