Zip and Unzip file
Solution 1
I've use this code to solve my problem...
I also do this one in my reference
Then add this code.
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);
}
}
and in button event
private void btnUnzip_Click(object sender, RoutedEventArgs e)
{
UnZip(@"E:\Libraries\Pictures\EWB FileDownloader.zip", @"E:\Libraries\Pictures\sample");
}
I'm telling you, it do work.
Solution 2
Archiving has now been included by Microsoft in .NET framework by using the ZipFile namespace.
To make it very short, to zip a directory, you can use the following code:
ZipFile.CreateFromDirectory(sourceFolder, outputFile);
Solution 3
System.IO.Compression.FileSystem.dll
download the above dll file. Go to solution explorer -> Right click on References, Click on Add References. Select "Windows" in window Select "Extension" Select "Browse" and go to the folder where you unzip the System.IO.Compression.FileSystem.dll file.
click on "Ok" and run the app again. :-)
Comments
-
Kuriyama Mirai over 1 year
I'm working on a project that needed to unzip file and store in a specific folder. But the problem is, I don't know how to do it. It is my first time to work on this kind of project.
I'm using visual studio 2010. And I'm not gonna use any third party applications.
Can anyone suggest how could I do this?
i've tried this code but the ZIPFILE is not recognizable. It has a red line on it.
using System; using System.IO; using System.IO.Compression; namespace UnzipFile { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnZip_Click(object sender, RoutedEventArgs e) { string startPath = @"c:\example\start"; string zipPath = @"c:\example\result.zip"; string extractPath = @"c:\example\extract"; ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath); } }
}