Reputation: 31
I'm working on a downloader tool for a project at work, and I'm trying to create a function to unzip a zip file and extract the contents to a temporary directory. (I'm using Visual Studio Professional, version 4.5.1.)
However, even after adding System.IO.Compression and System.IO.Compression.FileSystem.dll to my references, visual studio still doesn't recognized the ExtractToDirectory method. The error message tells me "Ionic.Zip.ZipFile does not contain a definition for 'ExtractToDirectory'".
Anyone know how to fix this so that I can extract my files? Here is my code (the file path for the zip is passed in):
private void UnzipStation(string zipPath)
{
try
{
string newFp = Path.Combine(Path.GetTempPath(), "CommissioningFiles");
Directory.CreateDirectory(newFp);
ZipFile.ExtractToDirectory(zipPath, newFp);
}
catch (Exception ex)
{
MessageBox.Show("Error unzipping station files. Contact a Novar representative. \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
Upvotes: 0
Views: 3405
Reputation: 4652
to use Ionic.Zip you have to do that :
using (ZipFile zipFile = new ZipFile(filePath))
{
zipFile.ExtractAll(newFp);
}
Upvotes: 0
Reputation: 141638
The error message tells me "Ionic.Zip.ZipFile does not contain a definition for 'ExtractToDirectory'".
You have two classes named ZipFile
that are "available" to the compiler. The C# compiler is confused about which one you are trying to use. It thinks you want to use Ionic.Zip.ZipFile
, when you really want to use System.IO.Compression.ZipFile
.
You can solve this in two ways. The first, is fully specify the namespace of the ZipFile
class you want to use:
System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, newFp);
Or, you can add a using
directive at the top of your C# file:
using ZipFile = System.IO.Compression.ZipFile;
Upvotes: 2
Reputation: 1321
You have a reference and using statement for DotNetZip in your project (Ionic.Zip.ZipFile). If you decided to stop using it and start using System.IO.Compression, delete this using statement and the reference. If you still use DotNetZip elsewhere, use System.IO.Compression.Zipfile.ExtractToDirectory(zipPath,newFp);
to explicitly use the ZipFile class from System.IO.Compression.
Upvotes: 2