SharpZipLib的FastZip不会解压缩Mac上的目录

时间:2013-04-03 00:23:06

标签: mono sharpziplib fastzip

我在Mac上使用以下代码,使用Mono解压缩zip文件。 zip文件包含目录下的条目(例如foo/bar.txt)。但是,在解压缩目录中,FastZip创建文件foo,而不是使用文件bar.txt创建目录foo\bar.txt。我该如何解决这个问题?

FastZip fz = new FastZip();
string filePath = @"path\to\myfile.zip";

fz.ExtractZip(filePath, @"path\to\unzip\to", null);

这会在foo\bar.txt中创建一个文件path\to\unzip\to

2 个答案:

答案 0 :(得分:1)

显然无法使用FastZip这个案例,所以我最终编写了自己的解压缩机制:

string filePath = @"path\to\myfile.zip";
string unzipDir = @"path\to\unzip\to";

using (var zipFile = new ZipFile(filePath))
{
    foreach (var zipEntry in zipFile.OfType<ZipEntry>())
    {
        var unzipPath = Path.Combine(unzipDir, zipEntry.Name);
        var directoryPath = Path.GetDirectoryName(unzipPath);

        // create directory if needed
        if (directoryPath.Length > 0)
        {
            Directory.CreateDirectory(directoryPath);
        }

        // unzip the file
        var zipStream = zipFile.GetInputStream(zipEntry);
        var buffer = new byte[4096];

        using (var unzippedFileStream = File.Create(unzipPath))
        {
            StreamUtils.Copy(zipStream, unzippedFileStream, buffer);
        }
    }
}

答案 1 :(得分:-1)

在创建zip时使用正斜杠来分隔文件夹