简体   繁体   English

如何使用SharpZipLib从zip文件中提取文件夹?

[英]How to extract a folder from zip file using SharpZipLib?

I have a test.zip file which contains inside a Folder with a bunch of other files and folders in it. 我有一个test.zip文件,其中包含一个文件夹,里面有一堆其他文件和文件夹。

I found SharpZipLib after figuring out that .gz / GzipStream was not the way to go since its only for individual files. 我找到了SharpZipLib后发现.gz / GzipStream不是那种方法,因为它只针对单个文件。 More importantly, doing this is similar to using GZipStream meaning it will create a FILE. 更重要的是,这样做与使用GZipStream类似, 意味着它将创建一个FILE。 But I have whole folder zipped. 但我有整个文件夹压缩。 How do I unzip to a 我如何解压缩到

For some reason the example unzipping here is set to ignore directories, so I'm not totally sure how that is done. 出于某种原因,这里解压缩示例设置为忽略目录,所以我不完全确定如何完成。

Also, I need to use .NET 2.0 for accomplish this. 此外,我需要使用.NET 2.0来实现这一目标。

I think it is the easier way. 我认为这是更简单的方法。 Default functionality (please look here for more info https://github.com/icsharpcode/SharpZipLib/wiki/FastZip ) 默认功能(请在此处查看更多信息https://github.com/icsharpcode/SharpZipLib/wiki/FastZip

it extract with folders. 它用文件夹提取。

code: 码:

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

This link explains exactly what you need to achieve: 此链接准确说明了您需要实现的目标:

http://community.sharpdevelop.net/forums/p/6873/23543.aspx http://community.sharpdevelop.net/forums/p/6873/23543.aspx

This is how I did it: 我就是这样做的:

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

It's sloppy but I hope it helps! 它很草率但我希望它有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 中的 SharpZipLib 提取多卷 zip 文件? - How can I extract a multi-volume zip file using SharpZipLib in C#? 如何在C#中使用Sharpziplib从http下载和提取单个文件? - how can i download and extract single file from http using sharpziplib in c#? 如何使用SharpZipLib提取或访问gzip中特定文件的流? - How to extract or access stream to specific file in gzip using SharpZipLib? 使用SharpZipLib更新zip-如果条目名称包含文件夹,那会很糟糕 - Using SharpZipLib to update a zip - something bad if the entry name includes folder 如何使用ICSharpCode.SharpZipLib将文件夹添加到zip存档 - How do you add a folder to a zip archive with ICSharpCode.SharpZipLib 通过从SQL File流中获取文件,使用ICSharpCode.SharpZipLib.Zip压缩文件 - Compress the Files using ICSharpCode.SharpZipLib.Zip by fetching files from SQL File stream 如何使用 ICSharpCode.SharpZipLib.Zip 在 zip 文件中创建多个子文件夹 - 请忽略.. 此问题已解决 - How can i create multiple subfolder inside the zip file using ICSharpCode.SharpZipLib.Zip -— please ignore .. this issue has been resolved SharpZipLib ZipEntry如何更改zip中的文件名? - SharpZipLib ZipEntry how can you change the name of a file in the zip? sharpziplib如何以内存流或字节数组格式获取zip文件并解压缩 - How with sharpziplib get a zip file in memorystream or byte array format and unpack it 使用SharpZipLib解压缩另一个zip内的zip文件 - Unzip a zip file that is inside another zip with SharpZipLib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM