Reputation: 33
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.
Upvotes: 2
Views: 3673
Reputation: 1605
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
64
from the extension so it is just .zip
hope this helps
Upvotes: 0