1

The following question's answer seems to outline how to extract files using the System.IO.Commpression.ZipFile.ExtractToDirectory method invocation. "ZipFile" doesn't seem to exist in .NET 4.5, when adding a reference to System.IO.Compression. How can I extract files from a *.zip file in .NET 4.5?

How to Unzip all .Zip file from Folder using C# 4.0 and without using any OpenSource Dll?

This seems to show how to compress files. But I'm looking for the reverse.

Zipping files in .NET 4.5

Even this question references "ZipFile" in the source code. But I can't seem to find this class.

How to extract just the specific directory from a zip archive in C# .NET 4.5?

enter image description here

EDIT:

Notice how 7z.exe (from 7zip package) didn't work. There must be a conflict with .NET and 7zip. ZipFile now seems to work fine.

private void extract_Click(object sender, EventArgs e)
{
    string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
    exePath = @"C:\test";  // path during troubleshooting

    ////var cmd1 = "cd \"" + exePath + "\"";
    ////ExecuteCommand(cmd1, 100, exePath);

    //var cmd2 = "\"" + exePath + "\\7z.exe\" x \"" + exePath + "\\source.zip\"";
    //ExecuteCommand(cmd2, 100, exePath);

    string zipPath = exePath + "\\source.zip";
    string extractPath = exePath;

    // needed explicit reference to System.IO.Compression.FileSystem
    ZipFile.ExtractToDirectory(zipPath, extractPath);


}

private static int ExecuteCommand(string command, int timeout, string dir)
{
    var processInfo = new ProcessStartInfo("cmd.exe", " /C " + command)
    {
        CreateNoWindow = true,
        UseShellExecute = false,
        WorkingDirectory = dir,
    };

    var process = Process.Start(processInfo);
    process.WaitForExit(timeout);
    var exitCode = process.ExitCode;
    process.Close();
    return exitCode;
}
Community
  • 1
  • 1
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
  • [ZipeFile.ExtractToDirectory MSDN applies to .net 4.5](https://msdn.microsoft.com/en-us/library/hh485723(v=vs.110).aspx) what's the issue and or problem..? – MethodMan Jan 13 '16 at 17:43
  • I think you need a reference to `System.IO.Compression.FileSystem` – petelids Jan 13 '16 at 17:44
  • `using System; using System.IO; using System.IO.Compression;` – MethodMan Jan 13 '16 at 17:45
  • petelids - FileSystem gets red squiggly marks. My error is "The name 'ZipFile' does not exist in the current context". I'm using Visual Studio 2013 Community Edition, and my project type is Windows Forms application using .NET 4.5. MethodMan - duly noted; still receiving error – JustBeingHelpful Jan 13 '16 at 17:48
  • 1
    @MacGyver - yep, the `ZipFile` class is in the _namespace_ System.IO.Commpression but it's in the _assembly_ System.IO.Compression.FileSystem so you need a reference to System.IO.Compression.FileSystem.dll in order to use that class – petelids Jan 13 '16 at 17:51

1 Answers1

4

You will need to add a reference to the System.IO.Compression.FileSystem assembly.

Every library class has an MSDN page. This is the one for ZipFile.

Notice the specification of the namespace and the assembly in the top section.

H H
  • 263,252
  • 30
  • 330
  • 514
  • What confuses me sometimes is how to tell if a library needs to be explicitly added (as a .NET reference) and when it's implicit, meaning "out of the box" and just requires a simple using declaration...? – JustBeingHelpful Jan 13 '16 at 17:55
  • That depends on the template you started from. They make an educated guess about what you most likely need. For the rest there is nothing special about any of those 'standard' assemblies. – H H Jan 13 '16 at 17:57
  • See the edit in my question. 7zip originally didn't work. .NET must conflict with something when launching a process from .NET vs. from the command line. – JustBeingHelpful Jan 13 '16 at 18:13
  • That's an entirely different question. Go through the steps of looking for an existing one and post a new one if needed. – H H Jan 13 '16 at 21:17
  • Henk, I discovered that if your C# library calls a command from the command line by launching a process in source code, and that command (exe) is a compiled C# program that references a C++ managed DLL (using the extern keyword), .NET does not allow this. But if the source from the 2nd C# application source code is inside your actual source code for the 1st C# application, this is allowed. I have two examples of this now. I found it interesting. – JustBeingHelpful Jan 21 '16 at 16:13
  • did anyone figure out how to do this for .net 4.5.2 ? the current ZipFile nuget 4.3 seem to support only 4.5 – Nandun Jan 15 '21 at 18:48
  • @Nandun - ? You don't need a package, it's part of the framework. Just add-reference the assembly. See above. – H H Jan 15 '21 at 21:49