Search code examples
c#system.io.compression

C# Unable to unzip files zipped with CreateEntryFromFile


As per this post I'm zipping SQL backup files with the code below:

using (ZipArchive zip = ZipFile.Open("test.bak", ZipArchiveMode.Create))
{
    zip.CreateEntryFromFile(@"c:\something.txt", "data/path/something.txt");
}

The zip file are created successfully with the correct file size. But I can't unzip the file. I get the the error:

The compressed (zip) folder is invalid or corrupted

I've tried using 7-zip and the build-in Windows 'Extract All' options. I also tried reinstalling the software with no luck.

My version of the code:

var fileName = Path.GetFileName(e.FullPath);
var newFile = dir + "\\" + fileName + ".zip";
var backupFile = txtBackupFolder.Text == "" ? "" : txtBackupFolder.Text + "\\" + fileName + ".zip";

using (ZipArchive zip = ZipFile.Open(newFile, ZipArchiveMode.Create))
{
    zip.CreateEntryFromFile(@e.FullPath, newFile);
}

Solution

  • Nkosi's post above did help, but I think what added to the problem was the escape characters in the newfile variable.

    This does work:

    using (ZipArchive zip = ZipFile.Open(newFile, ZipArchiveMode.Create))
    {
        zip.CreateEntryFromFile(@e.FullPath, "mybackup.bak");
    }