LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】SharpZipLib,压缩解压专家.NET库!

admin
2025年1月23日 22:53 本文热度 366

SharpZipLib,压缩解压专家.NET库!

大家好,今天我要和小伙伴们分享一个特别好用的.NET压缩解压库 - SharpZipLib!在开发中,我们经常需要处理文件的压缩和解压操作,比如制作安装包、备份文件等。SharpZipLib就是一个完全由C#编写的开源压缩库,支持ZIP、GZIP、BZip2等多种压缩格式。

1. 快速入门

首先通过NuGet安装SharpZipLib:

powershell复制

Install-Package SharpZipLib

2. 文件压缩实战

2.1 创建ZIP文件

来看看如何将一个文件夹压缩成ZIP文件:

csharp复制

using ICSharpCode.SharpZipLib.Zip;

public void CreateZipFile(string folderPath, string zipFilePath)

    FastZip fastZip = new FastZip();
    // 是否包含子文件夹
    fastZip.CreateEmptyDirectories = true
    // 密码保护,不需要则设为null
    fastZip.Password = "123456";
    // 开始压缩
    fastZip.CreateZip(zipFilePath, folderPath, true, "");


2.2 解压ZIP文件

解压也很简单,看代码:

csharp复制

using ICSharpCode.SharpZipLib.Zip;

public void ExtractZipFile(string zipFilePath, string extractPath)

    FastZip fastZip = new FastZip();
    // 设置密码(如果有的话)
    fastZip.Password = "123456";
    // 开始解压
    fastZip.ExtractZip(zipFilePath, extractPath, null);


3. GZIP压缩

有时我们需要压缩单个文件或数据流,GZIP是个不错的选择:

csharp复制

using ICSharpCode.SharpZipLib.GZip;

public void CompressWithGZip(string sourceFile, string compressedFile)

    using (FileStream sourceStream = File.OpenRead(sourceFile))
    using (FileStream targetStream = File.Create(compressedFile))
    using (GZipOutputStream gzipStream = new GZipOutputStream(targetStream))
    {
        byte[] buffer = new byte[4096];
        int readCount;
        while ((readCount = sourceStream.Read(buffer, 0, buffer.Length)) >; 0)
        {
            gzipStream.Write(buffer, 0, readCount);
        }
    }


4. 实用小技巧

4.1 进度监控

想知道压缩进度吗?试试这个:

csharp复制

public void ZipWithProgress(string folderPath, string zipFilePath, Action<;int>; progressCallback)

    FastZip fastZip = new FastZip();
    fastZip.CreateZip(zipFilePath, folderPath, true, "");
    
    long totalSize = new DirectoryInfo(folderPath)
        .GetFiles("*.*", SearchOption.AllDirectories)
        .Sum(file =>; file.Length);
    
    using (FileStream fs = File.OpenRead(zipFilePath))
    {
        int progress = (int)((fs.Length * 100) / totalSize);
        progressCallback(progress);
    }


4.2 内存压缩

有时我们需要在内存中进行压缩:

csharp复制

public byte[] CompressInMemory(byte[] data)

    using (MemoryStream msCompressed = new MemoryStream())
    {
        using (GZipOutputStream gzipStream = new GZipOutputStream(msCompressed))
        {
            gzipStream.Write(data, 0, data.Length);
        }
        return msCompressed.ToArray();
    }


小贴士

  1. 记得处理压缩文件时要使用 using 语句,确保资源正确释放
  2. 压缩大文件时,建议使用缓冲区读写,避免内存溢出
  3. 设置密码时,推荐使用强密码,并妥善保管
  4. 解压前最好先检查目标路径是否有足够空间


阅读原文:原文链接


该文章在 2025/1/24 9:06:18 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved