Question

I have a very large zip file which contains other zip files inside of it. I want my c# program to be able to recognize that the file is a zip file and if it is a zip file, then to extract it to a folder in the same location as the zip file. My code is here:

private void Unzip(OpenFileDialog tvZipOpen)
{
    string zipFile = tvZipOpen.FileName; // file to unzip
    int i = zipFile.LastIndexOf(".zip");
    string targetDirectory = zipFile.Substring(0, i); // location to extract to
    using (ZipArchive zip = ZipFile.OpenRead(zipFile))
    {
        zip.ExtractToDirectory(targetDirectory);
    }

    tvZipOpen.InitialDirectory = targetDirectory;
    tvZipOpen.ShowDialog();
}

I am using the ZipFile class from .NET 4.5 and i call on this method here:

if (tvOpen.ShowDialog() == DialogResult.OK)
{
    while (tvOpen.FileName.ToLower().EndsWith(".zip"))
    {
        Unzip(tvOpen);
    }

    return tvOpen.FileNames;
}

The code works fine for extracting the first zip file but when I try to extract the second zip file, I get an InvalidDataException that says local file header is corrupt. However, I don't think it is corrupt because I am able to open and extract the zip files perfectly in windows explorer. I'm not sure if the fact that it is a large zip file with a zip64 extension has anything to do with it but whatever the problem is, how come I don't get the problem when I open and extract in windows explorer and how do I fix this? Any help would be greatly appreciated.

Était-ce utile?

La solution

c# does not support the .zip64 extension.

how large is your zip file because if it under 4GiB rename it to .zip and it should work fine if it is larger than that see this

http://dotnetzip.codeplex.com/

To change the file extension

  1. Open windows explorer and press Alt + V
  2. Then go to tools and then folder options and make sure that the hide extensions for known file types box is unchecked and click apply and ok.
  3. then simply rename the file to remove the 64 from the extension so it is just .zip
  4. Then Click yes on the prompt
  5. And then you should be able to open the file in your program

hope this helps

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top