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

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

admin
2023年12月3日 22:51 本文热度 490


在C#中,有两个热门的文件压缩解析类库分别是SharpZipLib和DotNetZip。以下是它们的简要介绍以及使用实例代码。

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);

        }

    }

}

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


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