0

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);
}
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
  • I would try unzipping it through code and see if that works... Then at least you could find out if it's something to do with 7-zip/Windows or the way the archive is created. – Heretic Monkey Jul 26 '18 at 20:23
  • 2
    The code works fine in my environment, I guess you did not show all your code. probably your zip is downloaded from somewhere or reading big file that's not easy to open and read. could you show more details. – Dongdong Jul 26 '18 at 20:26
  • 1
    Quote from referenced link `Just in case it helps anybody, the second argument is the file entry. This is the path to which the file will be extracted relative to the unzip folder. In Windows 7, I found that if the file entry is a full path, e.g., @"D:\Temp\file1.pdf", the native Windows extractor fails. You may run into this issue if you simply use the filenames resulting from Directory.GetFiles(). Best to extract the file name using Path.GetFileName() for the file entry argument.` – Nkosi Jul 26 '18 at 20:41
  • 1
    SQL backups... how big is the file? Is it over 4 GB? I suspect we are hitting into file size issues. – vcsjones Jul 30 '18 at 19:39
  • Yes, it is aobut 6 GB – Cameron Castillo Jul 30 '18 at 20:27

2 Answers2

0

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");
}
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
0

I faced this very issue, and mine was related to special characters in the file names I was generating (specifically, a : character). So my code looked like

using (ZipArchive zip = ZipFile.Open(newFile, ZipArchiveMode.Create))
{
    var title = GetTitleOfHtmlFile(); //code to get the file name to use
    zip.CreateEntryFromFile(@e.FullPath, title); // title = "Chapter 1: My Chapter.html"
}

As the title being generated contained path-unfriendly characters, while I could add the entry to a zip file, when I tried to unzip the file, the OS would freak out because it couldn't deal with the path-unfriendly characters in the entry's name.

I simply changed the title to include an extension method to strip out such things, as so

title.ReplaceScreenUnfriendlyChars("-");

With an extension method like this:

public static string ReplaceScreenUnfriendlyChars(
this string curString, 
string replacementString = "")
{
   return string.Join(replacementString, curString.Split(Path.GetInvalidFileNameChars()));
}