【问题标题】:How to extract a folder from zip file using SharpZipLib?如何使用 SharpZipLib 从 zip 文件中提取文件夹?
【发布时间】:2023-03-21 20:25:01
【问题描述】:

我有一个 test.zip 文件,其中包含一个文件夹,其中包含一堆其他文件和文件夹。

我发现 SharpZipLib 在发现 .gz / GzipStream 不是可行的方法,因为它仅适用于单个文件。更重要的是,这样做类似于使用GZipStream,这意味着它将创建一个文件。但我已经压缩了整个文件夹。怎么解压到

由于某种原因,这里的example unzipping 设置为忽略目录,所以我不完全确定这是如何完成的。

另外,我需要使用 .NET 2.0 来完成此操作。

【问题讨论】:

标签: c# .net zip directory compression


【解决方案1】:

我认为这是更简单的方法。 默认功能(请在此处查看更多信息https://github.com/icsharpcode/SharpZipLib/wiki/FastZip

用文件夹解压。

代码:

using System;
using ICSharpCode.SharpZipLib.Zip;

var zipFileName = @"T:\Temp\Libs\SharpZipLib_0860_Bin.zip";
var targetDir = @"T:\Temp\Libs\unpack";
FastZip fastZip = new FastZip();
string fileFilter = null;

// Will always overwrite if target filenames already exist
fastZip.ExtractZip(zipFileName, targetDir, fileFilter);

【讨论】:

    【解决方案2】:

    我就是这样做的:

    public void UnZipp(string srcDirPath, string destDirPath)
    {
            ZipInputStream zipIn = null;
            FileStream streamWriter = null;
    
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(destDirPath));
    
                zipIn = new ZipInputStream(File.OpenRead(srcDirPath));
                ZipEntry entry;
    
                while ((entry = zipIn.GetNextEntry()) != null)
                {
                    string dirPath = Path.GetDirectoryName(destDirPath + entry.Name);
    
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }
    
                    if (!entry.IsDirectory)
                    {
                        streamWriter = File.Create(destDirPath + entry.Name);
                        int size = 2048;
                        byte[] buffer = new byte[size];
    
                        while ((size = zipIn.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            streamWriter.Write(buffer, 0, size);
                        }
                    }
    
                    streamWriter.Close();
                }
            }
            catch (System.Threading.ThreadAbortException lException)
            {
                // do nothing
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                if (zipIn != null)
                {
                    zipIn.Close();
                }
    
                if (streamWriter != null)
                {
                    streamWriter.Close();
                }
            }
        }
    

    有点草率,但希望对你有帮助!

    【讨论】:

    • 如果 (!entry.IsDirectory) 不包括目录,则该问题要求从文件夹中提取的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    相关资源
    最近更新 更多