7

How to zip a folder using vb.net and in .net framework 4.0. I am able to do this by using ZipFile class which is available in framework 4.5 but I need to use only framework 4.0 and also no third party library. Please help

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
charu
  • 83
  • 1
  • 1
  • 3

1 Answers1

8

Actually you can use the ZipFile class in .NET 4.0 as well. You just gotta add a reference to the System.IO.Compression.FileSystem.dll file, located in:

%SystemRoot%\Microsoft.NET\assembly\GAC_MSIL\System.IO.Compr‌​‌​‌​ession.FileSyste‌​m\‌​v4‌​.0_4.0.0.0__‌​b77a‌​5c56‌​1934e089‌​\Syste‌​m.IO.C‌​ompr‌​ession.F‌​ileSyste‌​‌​m.dll

As you see it is located in the "v4.0" folder, so it exists in .NET 4.0, but for some reason isn't referenced automatically.


To add a reference to your project:

  1. Right-click the project node in the Solution Explorer.

    Solution Explorer

    (Image credit: another Stack Overflow question)

  2. Press Add Reference....

  3. Go to the Browse tab and locate the file mentioned above.

How to: Add or Remove References By Using the Add Reference Dialog Box

Community
  • 1
  • 1
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Hi can you please elaborate it where to add the reference – charu Apr 20 '17 at 06:42
  • @charu : Glad I could help! Welcome to Stack Overflow, and good luck! – Visual Vincent Apr 20 '17 at 09:53
  • 3
    Following this procedure will make the program dependent on .Net 4.5 as that is the minor version that introduced the `System.IO.Compr‌​‌​‌​ession.FileSyste‌​m.dll`. In an up-to-date system, this should not be a problem, but the program is not strictly developed against a minimum of .Net 4.0. For a good discussion see: [.NET Versioning and Multi-Targeting - .NET 4.5 is an in-place upgrade to .NET 4.0](https://www.hanselman.com/blog/NETVersioningAndMultiTargetingNET45IsAnInplaceUpgradeToNET40.aspx). @charu you may want to access how this works with your criteria. – TnTinMn Apr 20 '17 at 16:05
  • @TnTinMn : Why is it then located in a folder that appears to be for .NET 4.0? Anyhow, he can always choose to set it to `CopyLocal` and it'll be loaded from his application folder instead. – Visual Vincent Apr 20 '17 at 18:24
  • 3
    VisualVincent, there is no separate folder for 4.5 - that is what they mean by it being an in-place replacement for 4.0. For more on that see: [Where is the .NET Framework 4.5 directory?](http://stackoverflow.com/questions/12070518/where-is-the-net-framework-4-5-directory#12070551). Also the copy-local option/solution is what I was hoping for you to realize and add to your answer without me stating it. :) Not that it matters, but the folder reference you point to is the GAC copy; the install directory is: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\`. – TnTinMn Apr 20 '17 at 19:29
  • @TnTinMn : Oh. Well, I'll add `Copy Local` to the answer in a bit, just gotta do it from my computer so I can add screenshots. -- Because I use VS2010 and therefore don't use .NET 4.5 (I know 4.0 is deprecated) I am not as much aware of how it works as I am with 4.0. Thanks for sharing the info! – Visual Vincent Apr 20 '17 at 19:31
  • VisualVincent, after a quick peek with Reflector, it looks like a 4.5 version of `System.IO.Compression.dll` will be needed (defines the ZipArchive Class). I don't know what other references would be needed. There it would be advised to thoroughly test this on a vanilla 4.0 system. – TnTinMn Apr 20 '17 at 19:49
  • @TnTinMn : Hmm, thanks for the info... I have a virtual machine with Windows XP that I can test it on, I'll also check the Reference Source to see if there's anything else. – Visual Vincent Apr 20 '17 at 20:06
  • 3
    @charu : TnTinMn is right, unfortunately you need a working installation of .NET 4.5 or higher for this to work. Running this on an XP machine with .NET 4.0 throws a `TypeLoadException` because it cannot load **one** of `System.IO.Compression`'s enumerations. Sadly I have not been able to find a workaround yet, thus without .NET 4.5 or higher installed there seem to be no solution other than to use a third-party library. – Visual Vincent Apr 20 '17 at 21:52
  • @TnTinMn : Update, see comment above this. – Visual Vincent Apr 20 '17 at 21:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142229/discussion-between-tntinmn-and-visual-vincent). – TnTinMn Apr 21 '17 at 02:53