<返回更多

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

2023-12-31  今日头条  架构师老卢
加入收藏


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

1. SharpZipLib

功能:

优点:

使用实例:

using System;
using ICSharpCode.SharpZipLib.Zip;

class Program
{
    static void MAIn()
    {
        string sourceFolder = @"C:PathToYourFolder";
        string zipFile = @"C:PathToYourArchive.zip";

        ZipDirectory(sourceFolder, zipFile);
        Console.WriteLine("Compression completed.");

        string extractFolder = @"C:PathToYourExtracted";
        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

功能:

优点:

使用实例:

using System;
using Ionic.Zip;

class Program
{
    static void Main()
    {
        string sourceFolder = @"C:PathToYourFolder";
        string zipFile = @"C:PathToYourArchive.zip";

        ZipDirectory(sourceFolder, zipFile);
        Console.WriteLine("Compression completed.");

        string extractFolder = @"C:PathToYourExtracted";
        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);
        }
    }
}

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

关键词:C#      点击(4)
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多C#相关>>>