-1

I"m currently running .net version 4.5 and am trying to use it's "new" zip functions. I'm including System.IO.Compression and am trying to run the following code:

using System.IO.Compression;
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";

ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);

The issue I'm getting is that The name 'ZipFile does not exist in the current context. I don't know why it wouldn't exist if I'm already using what requires it.

Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
  • @Tinwor Not a duplicate, I'm already referencing the namespace but am still getting errors. – Joe Scotto Feb 24 '17 at 21:25
  • @JoeScotto you need to reference the *assembly* - most likely. Right click on "references" in your project, click "Add Reference" then file the `System.IO.Compression.FileSystem` assembly. Then you can use the namespace. – vcsjones Feb 24 '17 at 21:26
  • You are referecing the wrong library. It's `System.IO.Compression.FileSystem` – Tinwor Feb 24 '17 at 21:26
  • @vcsjones I'm not using a solution or a project, I'm using just a standalone handler.ashx file. – Joe Scotto Feb 24 '17 at 21:27
  • The dupe has the same namespace/assembly problem. The specifics of adding assemblies to an ashx are a separate issue, may deserve their own question. – H H Feb 24 '17 at 21:50
  • Please don't ask the same question 3 times. – Paul-Sebastian Manole Sep 29 '20 at 16:04

2 Answers2

1

You need to include a reference to the System.IO.Compression.FileSystem assembly. However the namespace is still System.IO.Compression.

See the MSDN documentation for details.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
1

Have you tried adding the namespace to your current file? To do this add this to the top of the file.

 using System.IO.Compression;
Licht
  • 1,079
  • 1
  • 12
  • 27
  • 1
    That's what I currently have. – Joe Scotto Feb 24 '17 at 21:24
  • Then see the other answer. To add the reference to your project right click References under your project and click Add Reference. Then select Assemblies on the left and find System.IO.Compression.FileSystem. Check that box and click OK. – Licht Feb 24 '17 at 21:25
  • It's just a standalone handler.ashx file though, no solution or project. – Joe Scotto Feb 24 '17 at 21:26
  • In Visual Studio click Solution Explorer on the right. This should expand a menu. Do you not see anything in there? – Licht Feb 24 '17 at 21:27
  • Solution 'Solution1' (0 projects) – Joe Scotto Feb 24 '17 at 21:28
  • 1
    So I'm going to say it will be significantly easier for you going forward (not just for this one thing) to make a project. It will give you much more control over what's going on. A new project will have a bunch of stuff you don't need in it but once you get it all set up this will be a simple matter. That said you can use this page to learn about adding assembly references using a web.config file. https://msdn.microsoft.com/en-us/library/37e2zyhb(v=vs.85).aspx Web.config is similar to PHP.ini. – Licht Feb 24 '17 at 21:38
  • What would be a good project type that I can add my existing html and css files to? – Joe Scotto Feb 24 '17 at 21:46
  • Templates > Visual C# > Web > ASP.NET Web Application (.NET Framework) should do. I believe there's some check boxes for various technologies you'll not want. Once you have that all set then add your assembly reference and you're off to the races. To compile your project right click it and click Publish. A file system publish is the easiest one. – Licht Feb 24 '17 at 21:50
  • What about adding my current files to that new project? – Joe Scotto Feb 24 '17 at 21:51
  • You can drag and drop the files into the project. Add Existing Files is another option. If you drag a folder in it tends to add the folder but not the contents, Show All Files gets around this (see the tiny buttons at the top of the Solution Explorer.) – Licht Feb 24 '17 at 21:52
  • I have everything setup now and added the reference to System.IO.Compression.Filesystem but still get that error.... – Joe Scotto Feb 24 '17 at 21:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136582/discussion-between-licht-and-joe-scotto). – Licht Feb 24 '17 at 21:55