Reputation: 421
I'm using c# fw4.5. I have a simple code extracting a zip file.
foreach(ZipArchiveEntry entry in z.entries) //z is a zip file open in ZipArchiveMode.Read
{
entry.ExtractToFile(entry.FullName);
}
The zip file have a directory inside it and all files are inside that directory. When I look at the z.Entries I see its an array which place [0] is only the directory and [1],[2],[3] are files. But when its try to do:
entry.ExtractToFile(entry.FullName);
On the first entry, I get an error: "The filename, directory name or volume label syntax is incorrect".
I can't seems to find out whats wrong. Do I need to anything also for it to open the directory? Maybe because the entry is a directory only the "ExtractToFile(entry.FullName)" can't work?
Thanks in advanced.
Upvotes: 1
Views: 4560
Reputation: 33
In addition to Tonkleton's answer I would suggest that you use a third-party compression library since ZipArchive is not supported for framework versions before the .Net 4.5 framework, might I suggest DotNetZip as mentioned in other questions regarding compression in earlier frameworks on StackOverflow.
Upvotes: 0
Reputation: 787
Replace your paths:
void Main()
{
var zipPath = @"\\ai-vmdc1\RedirectedFolders\jlambert\Downloads\cscie33chap1and2.zip";
var extractPath = @"c:\Temp\extract";
using (ZipArchive z = ZipFile.OpenRead(zipPath))
{
foreach(ZipArchiveEntry entry in z.Entries) //z is a zip file open in ZipArchiveMode.Read
{
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName), true);
}
}
}
Upvotes: -1
Reputation: 547
According to this MSDN article, the ExtractToFile
method expects a path to a file (with an extension) and will throw an ArgumentException
if a directory is specified.
Since the first entry in the archive is a directory and you are using its name as the argument, that is why you are having this issue.
Look into the related ExtractToDirectory
method, which is used like so:
ZipFile.ExtractToDirectory(@"c:\zip\archive.zip", @"c:\extract\");
Upvotes: 2