Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract ZIP file in C#

How can I extract a ZIP file using C#?

like image 763
sari k Avatar asked Nov 28 '10 13:11

sari k


People also ask

How do I extract a zip file to C drive?

Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

How do I unzip a zip file?

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.


2 Answers

in .NET Framework 4.5 and newer

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);
        }
    }
}
like image 67
Ali Eshghi Avatar answered Sep 24 '22 16:09

Ali Eshghi


DotNetZip:

class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files...

DotNetZip works on PCs with the full .NET Framework, and also runs on mobile devices that use the .NET Compact Framework. Create and read zip files in VB, C#, or any .NET language, or any scripting environment. DotNetZip supports these scenarios:

  • a Silverlight app that dynamically creates zip files.
  • an ASP.NET app that dynamically creates ZIP files and allows a browser to download them
  • a Windows Service that periodically zips up a directory for backup and archival purposes
  • a WPF program that modifies existing archives - renaming entries, removing entries from an archive, or adding new entries to an archive
  • a Windows Forms app that creates AES-encrypted zip archives for privacy of archived content.
  • a SSIS script that unzips or zips
  • An administrative script in PowerShell or VBScript that performs backup and archival.
  • a WCF service that receives a zip file as an attachment, and dynamically unpacks the zip to a stream for analysis
  • an old-school ASP (VBScript) application that produces a ZIP file via the COM interface for DotNetZIp
  • a Windows Forms app that reads or updates ODS files
  • creating zip files from stream content, saving to a stream, extracting to a stream, reading from a stream
  • creation of self-extracting archives.

If all you want is a better DeflateStream or GZipStream class to replace the one that is built-into the .NET BCL, DotNetZip has that, too. DotNetZip's DeflateStream and GZipStream are available in a standalone assembly, based on a .NET port of Zlib. These streams support compression levels and deliver much better performance than the built-in classes. There is also a ZlibStream to complete the set (RFC 1950, 1951, 1952)...

have fun

like image 45
BlindingDawn Avatar answered Sep 24 '22 16:09

BlindingDawn