4

I am using ZipFile class.

A reference/dll must be added in order to use this class. It seems that you can use both the System.IO.Compression and the Ionic.Zip dll (one at a time ofcourse).

In my case, only the latter works. The problem though, is that the Ionic.Zip doesn't seem to have the method: ExtracToFile(...) which overwrites an existing file. I need this method badly. Instead, I can only do:

zip.Extract(extractDirectory); (Then I get an error saying the file already exist)

If I do:

zip.ExtractToFile(extractDirectory);

I get an error saying that the method ExtractToFile does not exist.

Another thing to note, is that doing Using System.IO.Compression, I get an error when I do:

var zip = ZipFile.Read(finalFileToUnzip)

which says that it can't find ZipFile (although I did add the dll as a reference, doing "Add Reference -> and selecting the .dll file")

If I do Using Ionic.Zip, it will accept: var zip = ZipFile.Read(finalFileToUnzip)

I am not able to find any info or previous posts about this issue, I hope anyone can help

Any idea about what might be the problem?

Here is more code:

for (int i = 0; i < listWithZipToUnpack.Count; ++i)
{
    extractDirectory = Path.Combine(projectPath.ToString(), pathDir + listWithZipToUnpack[i]);

    var finalFileToUnzip = Path.Combine(projectPath.ToString(), pathDir, "Lely", listWithZipToUnpack[i]);

    if (finalFileToUnzip.Equals("--Vælg fil--")) { continue; }

    using (var zip = ZipFile.OpenRead(finalFileToUnzip))
    {
        if (!Directory.Exists(extractDirectory))
        {
            Directory.CreateDirectory(extractDirectory+"-"+listWithZipToUnpack[i]);
        }

        foreach (var zipArchiveEntry in zip.Entries)
        {
            zipArchiveEntry.ExtractToFile(extractDirectory);
        }
    }
}

I am using .NET framework 4.5.2

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
user1960836
  • 1,732
  • 7
  • 27
  • 47
  • 2
    `ZipFile` was added in .NET 4.5... which version of the framework are you using? – Jon Skeet Oct 30 '14 at 16:00
  • sounds like you have a `Path` Issue here can you show how you are assigning the file / file path of the contents that you need to have zipped.. – MethodMan Oct 30 '14 at 16:03
  • I am using version 4.5.2 of .NET. I edited the code as added more code 2 it – user1960836 Oct 30 '14 at 16:07
  • What specific problem are you asking about? A compile error? An error on execution? Can you tell us *exactly* what error you're getting, and where? – Jim Mischel Oct 30 '14 at 16:11
  • I cannot use the method: ExtractToFile. It says that it doesn't exist. That is my problem – user1960836 Oct 30 '14 at 17:00
  • 1
    Without a complete code example it's impossible to say what's wrong. The code you posted _should_ compile fine, so if it's not then it's because of some detail you omitted from your question. That said, I will point out that even if you got this to compile, it still wouldn't work, because you are passing the directory name as the file name in the call to `ExtractToFile()`. For each archive entry, you need to combine the directory name with the entry name to get the file name to use to save the file. – Peter Duniho Oct 30 '14 at 17:32
  • What says that what doesn't exist? Does the compiler say that the method `ExtractToFile` does't exist, or do you get an error at runtime saying that the path you specified (the `extractDirectory`) doesn't exist? – Jim Mischel Oct 30 '14 at 18:08
  • The error is on compile time. It is VS12 that says the method doesn't exist – user1960836 Oct 30 '14 at 18:46

1 Answers1

11

I solved it. The problem is that it is not enough to only add System.IO.Compression to references, you must also add System.IO.Compression.Filesystem

user1960836
  • 1,732
  • 7
  • 27
  • 47