[英]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.