Вопрос

I'm using the following code on Mac using Mono to unzip a zip file. The zip file contains entries under directories (for example foo/bar.txt). However, in the unzipped directory, instead of creating a directory foo with a file bar.txt, FastZip creates a file foo\bar.txt. How do I get around this?

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

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

This creates a file foo\bar.txt in path\to\unzip\to.

Это было полезно?

Решение

Apparently cannot use FastZip for this case so I ended up writing my own unzipping mechanism:

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

Другие советы

use a forward slash to separate folders when creating the zip

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top