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

4

5 Answers 5

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);
5
  • ZipFile.CreateFromdirectory is unrecognizable in my code. Why? May 28, 2014 at 8:22
  • Have you read the link that I provided? I don't think see, otherwise you would have known that you need to add a reference to System.IO.Compression
    – Complexity
    May 28, 2014 at 8:25
  • i've added System.IO.Compression yet the ZipFile is unrecognizable. May 28, 2014 at 8:53
  • 2
    ZipFile only exists in .NET 4.5. She is using VS2010 (ie: not .NET 4.5). This answer is not applicable.
    – J...
    May 28, 2014 at 9:03
  • 1
    And it requires that you add project references to these assemblies: System.IO.Compression and System.IO.Compression.FileSystem Apr 24, 2018 at 17:45
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.

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

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/

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.