How to Unzip all .Zip file from Folder using C# 4.0 and without using any OpenSource Dll?
Solution 1
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.
Solution 2
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().
SHEKHAR SHETE
Master Of Computer Application[MCA] Microsoft Certified Professional[MCP] Microsoft Certified Technology Specialist[MCTS]
Updated on July 10, 2022Comments
-
SHEKHAR SHETE almost 2 years
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.