Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

zip

unzip

I have a folder containing .ZIP files. Now, I want to Extract the ZIP Files to specific folders using C#, but without using any external assembly or the .Net Framework 4.5.

I have searched, but not found any solution for unpacking *.zip files using Framework 4.0 or below.

I tried GZipStream, but it only supports .gz and not .zip files.

like image 984
SHEKHAR SHETE Avatar asked Apr 17 '13 06:04

SHEKHAR SHETE


People also ask

How do I unzip all files in a folder?

Right-click the file you want to zip, and then select Send to > Compressed (zipped) folder. Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions.

How do I unzip a zip file in C?

Right click on the file and select “extract all”. This will open the extraction wizard. Click “next” and then select the location to extract the files to. (This example uses Program Files on C drive.)


2 Answers

Here is example from msdn. System.IO.Compression.ZipFile is made just for that:

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";              ZipFile.CreateFromDirectory(startPath, zipPath);              ZipFile.ExtractToDirectory(zipPath, extractPath);         }     } } 

Edit: sorry, I missed that you're interests in .NET 4.0 and below. Required .NET framework 4.5 and above.

like image 141
Denys Denysenko Avatar answered Sep 19 '22 11:09

Denys Denysenko


I had the same question and found a great and very simple article that solves the problem. http://www.fluxbytes.com/csharp/unzipping-files-using-shell32-in-c/

you'll need to reference the COM library called Microsoft Shell Controls And Automation (Interop.Shell32.dll)

The code (taken untouched from the article just so you see how simple it is):

public static void UnZip(string zipFile, string folderPath)
{
    if (!File.Exists(zipFile))
        throw new FileNotFoundException();

    if (!Directory.Exists(folderPath))
        Directory.CreateDirectory(folderPath);

    Shell32.Shell objShell = new Shell32.Shell();
    Shell32.Folder destinationFolder = objShell.NameSpace(folderPath);
    Shell32.Folder sourceFile = objShell.NameSpace(zipFile);

    foreach (var file in sourceFile.Items())
    {
        destinationFolder.CopyHere(file, 4 | 16);
    }
}

highly recommend reading the article- he brings an explenation for the flags 4|16

EDIT: after several years that my app, which uses this, has been running, I got complaints from two users that all of the sudden the app stopped working. it appears that the CopyHere function creates temp files/folders and these were never deleted which caused problems. The location of these files can be found in System.IO.Path.GetTempPath().

like image 41
TBD Avatar answered Sep 18 '22 11:09

TBD