Kuriyama Mirai
Kuriyama Mirai

Reputation: 937

Zip and Unzip file

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

Upvotes: 2

Views: 12842

Answers (5)

Dilip Jangid
Dilip Jangid

Reputation: 764

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

Upvotes: 1

Dilip Jangid
Dilip Jangid

Reputation: 764

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

Upvotes: 0

turgay
turgay

Reputation: 458

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/

Upvotes: 0

Kuriyama Mirai
Kuriyama Mirai

Reputation: 937

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.

Upvotes: 1

Complexity
Complexity

Reputation: 5820

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

Upvotes: 8

Related Questions