2

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);
    }
}

}

enter image description here

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Kuriyama Mirai
  • 867
  • 5
  • 17
  • 37
  • 1
    possible duplicate of [Unzip files programmatically in .net](http://stackoverflow.com/questions/836736/unzip-files-programmatically-in-net) – aevitas May 28 '14 at 08:17
  • possible duplicate of [I didn't find "ZipFile" class in the "System.IO.Compression" namespace](http://stackoverflow.com/questions/15241889/i-didnt-find-zipfile-class-in-the-system-io-compression-namespace) – J... May 28 '14 at 09:02
  • 1
    For .NET versions prior to 4.5, if you don't mind using GZip, see : http://msdn.microsoft.com/en-us/library/ms404280%28v=vs.100%29.aspx Otherwise, Zip needs third party libraries. – J... May 28 '14 at 09:05
  • Use the following link. You will get what you are looking for. [ZIP to Unzip and vice versa](http://msdn.microsoft.com/en-us/library/ms404280%28v=vs.110%29.aspx) – Maverick May 28 '14 at 08:28

5 Answers5

8

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);
Complexity
  • 5,682
  • 6
  • 41
  • 84
1
I've use this code to solve my problem...

I also do this one in my reference

enter image description here

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.

Kuriyama Mirai
  • 867
  • 5
  • 17
  • 37
1

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. :-)

Dilip Jangid
  • 754
  • 1
  • 10
  • 20
0

The following link shows two example for zip and unzip to the files in C#. You can use this sample.

Sample(using 7-zip):

var tmp = new SevenZipCompressor();
tmp.ScanOnlyWritable = true;
tmp.CompressFilesEncrypted(outputFilePath, password, filePaths);

Sample(using ZipArchive):

ZipArchive zip = ZipFile.Open(filePath, ZipArchiveMode.Create);
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
zip.Dispose();

For more information:

http://csharpexamples.com/zip-and-unzip-files-programmatically-in-c/

turgay
  • 438
  • 4
  • 7
0

using System.IO.Compression;

include the above assembly in your code. It contains definition for the below 2 methods

ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath);

Dilip Jangid
  • 754
  • 1
  • 10
  • 20