31

I'm kind of a newbie to Visual Studio programming.

I recently upgraded .Net 4.0 to 4.5 in order to use the ZipFile class under System.IO.Compression, but after the installation completed, Visual Studio (I'm using 2012) still cannot recognize ZipFile as a class name.

I've made sure that .Net 4.5 appears in Control Panel programs list and my C# solution sets .Net Framework 4 as the target framework.

Could someone help me figure this out?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
wuxilixi
  • 357
  • 1
  • 6
  • 11

7 Answers7

69

See ZipFile Class on MSDN. It shows the required framework version is 4.5. Once the framework version is fixed check you have added a reference to the System.IO.Compression.FileSystem.dll assembly and added a using System.IO.Compression directive to your class.

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
  • 1
    Thanks a million for this! I thought I was having an issue with being able to call .net 4.5 assemblies. But you nailed it! – SteckDEV Jul 02 '15 at 17:19
  • 2
    Yes, need to add both references: System.IO.Compression.FileSystem, and System.IO.Compression to your project. – rfolt Nov 12 '15 at 16:30
  • How to do this with a simple CreateZip.cs file compiled by csc.exe ? – Alex 75 Nov 17 '15 at 22:00
15

You also need to reference the System.IO.Compression.FileSystem.dll assembly.

platon
  • 5,310
  • 1
  • 22
  • 24
  • Ah, I got it. Thanks for your help! Just wondering, how is adding the assembly in reference different from simply typing "using System.IO.Compression" in the code? For example, I have "using System.Configuration" in the code but not reference it and the program still works OK. – wuxilixi Jan 08 '13 at 15:58
  • 1
    @user1958597, Adding assembly to the references list means that you make all public classes from this assembly available to your code. "using " allows you to make your code shorter. I mean if you add using System.IO.Compression; to your code, you will be able to write ZipFile zip = ... instead of System.IO.Compression.ZipFile zip = ... – platon Jan 08 '13 at 16:01
10

Just to further clarify the previous answers, here's how to add the references manually to a Web.config:

<configuration>
  <system.web>
    <compilation targetFramework="4.5">
      <assemblies>
        <add assembly="System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

Or to a *.csproj:

<Project ...>
  <ItemGroup>
    <Reference Include="System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089, processorArchitecture=MSIL" />
    <Reference Include="System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089, processorArchitecture=MSIL" />
  </ItemGroup>
</Project>

The files can be found in C:\Windows\Microsoft.NET\assembly\GAC_MSIL\ and the subfolders contain the necessary info on version, culture and PublicKeyToken as well.

Michael
  • 8,362
  • 6
  • 61
  • 88
The Conspiracy
  • 3,495
  • 1
  • 17
  • 18
5

You need to change the target framework of the current project from .Net 4 to .Net 4.5.

daryal
  • 14,643
  • 4
  • 38
  • 54
2

New features in .NET 4.5

Zip compression improvements to reduce the size of a compressed file. See the System.IO.Compression namespace.

Add System.IO.Compression assembly as reference to your project. You may also want to reference System.IO.Compression.FileSystem assembly to access three extension methods (from the ZipFileExtensions class) for the ZipArchive class: CreateEntryFromFile, CreateEntryFromFile, and ExtractToDirectory. These extension methods enable you to compress and decompress the contents of the entry to a file.

Sample

const string zipFilePath = @"C:\apps\Sample Pictures.zip";

using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read)) {

        foreach (var zipArchiveEntry in archive.Entries)
            Console.WriteLine(
                "FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName
            );
}

References:
http://msdn.microsoft.com/en-us/library/ms171868.aspx
http://www.tugberkugurlu.com/archive/net-4-5-to-support-zip-file-manipulation-out-of-the-box

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
1

For Windows Phone 8.1, use NuGet to add the Microsoft Compression package to your project and reference it.

If you had an older WP8 project you may have been using a different package that would create conflicts with the System.IO.Compression dll that is part of the .NET 4.5 that comes with WP8.1. You need to get rid of that and use Microsoft Compression which works harmoniously with .NET 4.5. That's how I got here!

Andy Weinstein
  • 2,639
  • 3
  • 21
  • 32
1

In my case I needed to manually delete all dll references that started with System.IO.Compression and then add one by one the needed ones (System.IO.Compression.FileSystem, etc.) even though I only wrote a single using statement

using System.IO.Compression;

I hope this helps someone

Hugo Nava Kopp
  • 2,906
  • 2
  • 23
  • 41