3

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.

sohum
  • 3,207
  • 2
  • 39
  • 63

2 Answers2

1

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);
        }
    }
}
sohum
  • 3,207
  • 2
  • 39
  • 63
  • you might also want to check if its an dir : `if(!zipEntry.IsDirectory) { //unzip stream `. Otherwise dir in zipfile will create an error. – D. Herzog Aug 30 '22 at 14:17
-1

use a forward slash to separate folders when creating the zip

Brian Rice
  • 3,107
  • 1
  • 35
  • 53