OfficeAddinDev
OfficeAddinDev

Reputation: 1125

How to zip files/directories in VB.NET?

I would like to zip a folder containing files and subfolders in VB.NET. My solution targets .NET 4.0 Client Profile.

I see that there is a ZipFile class for .NET 4.5, and System.IO.Packing for .NET 4.0 (but not Client Profile). So, those won't help. I note that there is also a GZipStream class, but I never see .gz files floating around, so not sure if that's a good approach. I would prefer a basic .zip file that I know my users can work with.

There are a handful of third-party solutions, such as http://icsharpcode.github.io/SharpZipLib/, but I assume they are far more bloated than the 10-20 lines of code I am looking for. Maybe that's the right answer...I don't know.

Just hoping for a few lines of VB.NET code to zip some files in a solution targeting .NET 4.0 CP.

Upvotes: 4

Views: 30255

Answers (5)

Fnk
Fnk

Reputation: 50

Same challenges as "an odder guest". That is can't commment due to low rep as new user and have to add a new answer. (Frequent reader though). Running on vb.Net 4.6 and got no errors on the suggestions above, but no output zip file was generated. Then after some googling added references System.IO.Compression.Filesystem and System.IO.Compression and ended up with the following one-liner:

System.IO.Compression.ZipFile.CreateFromDirectory(FilePath1, FilePath3_ZippedFile)

Where FilePath1 is the path to the source folder (c:\whateverfolder) and FilePath3_ZippedFile is the path to the resulting output zip-file(c:\anotherfolder\MyZipFile.zip).

Sorry if you think my input is redundant, but I know for newcommers like my self we need the answers feed with tee spoons.

Word of caution: If you try to create the zip-file in the source folder you might have issues. My code then only included one of the files from the source folder in the zip-file. (No error messages). Changing the target location to another folder solved this without other edits to the code.

Upvotes: 0

an odder guest
an odder guest

Reputation: 194

Unable to comment in the comments section due to lack of reputation (due to being new here).

On the two answers indicating that using System.IO.Compression can be used that were commented as not working in NET 4.0 CP, this should be noted, as the original answers did not include an often overlooked fact.

A reference needs to be added to the project in order to activate "System.IO.Compression". This is not obvious to many new VS users, and unless the refrence is added, it indeed seems like "System.IO.Compression" does not work in NET 4.0.

In this case, the reference that needs to be added is System.IO.Compression.Filesystem (v4.0.0.0). One may also wish to add the reference: System.IO.Compression.

The "Add Reference..." dialog is under the "Project" menu in VS.

Upvotes: 4

BinaryEvolved
BinaryEvolved

Reputation: 920

The MSDN Website has a class that can help you out here called ZipFile

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication {
  class Program {
    static void Main(string[] args) {
      string startPath = @"c:\example\start";
      string zipPath = @"c:\example\result.zip";
      string extractPath = @"c:\example\extract";
      'Creates Zip File to directory as specified (startPath). And puts it in a specifed folder (zipPath)
            ZipFile.CreateFromDirectory(startPath, zipPath);
              '
      Extracts Zip File to directory as specified(extractpath)
      ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
  }
}

Hope this helps!

Upvotes: 1

Naitik Patel
Naitik Patel

Reputation: 89

Create ZIP from "source" folder.

Imports System.IO.Compression    
ZipFile.CreateFromDirectory("source","destination.zip",CompressionLevel.Optimal,False)

Extract ZIP to "destination" folder.

ZipFile.ExtractToDirectory("destination.zip","destination")

Upvotes: 7

user4376581
user4376581

Reputation:

Simply you can do it using Ionic.Zip.Dll

Here is the function for this,accepting two variables source path and destination path

public static void ExecuteBackupAsZip(string SourcePath, string DestinationPath)
        {
            using (var zip = new Ionic.Zip.ZipFile())
            {
                try
                {
                    zip.AddDirectory(SourcePath);
                    zip.Save(DestinationPath);
                }
                catch { Console.WriteLine("Failed to execute backup"); }
            }
        }

Upvotes: 0

Related Questions