Extract ZIP
ZIP is a file compression and archiving format used to reduce file sizes for efficient storage and transmission. It combines multiple files and directories into a single archive, often with a '.zip' extension. ZIP archives are commonly used for data backup, software distribution, and file sharing.
To extract files from a ZIP archive, you can utilize the ExtractArchiveToDirectory
method. Specify the location of the ZIP file and the directory to which you want to extract the files.
using System;
using System.IO.Compression;
class Program
{
static void Main()
{
// Specify the path to your ZIP file
string zipPath = @"C:\example\myArchive.zip";
// Specify the directory to extract files to
string extractPath = @"C:\example\extractedFiles";
try
{
// Extracts all the files from the specified ZIP archive to the specified directory
ZipFile.ExtractToDirectory(zipPath, extractPath);
Console.WriteLine("Files have been successfully extracted.");
}
catch (Exception ex)
{
// Catch any exceptions that occur during the extraction process
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
using System;
using System.IO.Compression;
class Program
{
static void Main()
{
// Specify the path to your ZIP file
string zipPath = @"C:\example\myArchive.zip";
// Specify the directory to extract files to
string extractPath = @"C:\example\extractedFiles";
try
{
// Extracts all the files from the specified ZIP archive to the specified directory
ZipFile.ExtractToDirectory(zipPath, extractPath);
Console.WriteLine("Files have been successfully extracted.");
}
catch (Exception ex)
{
// Catch any exceptions that occur during the extraction process
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
Imports System
Imports System.IO.Compression
Friend Class Program
Shared Sub Main()
' Specify the path to your ZIP file
Dim zipPath As String = "C:\example\myArchive.zip"
' Specify the directory to extract files to
Dim extractPath As String = "C:\example\extractedFiles"
Try
' Extracts all the files from the specified ZIP archive to the specified directory
ZipFile.ExtractToDirectory(zipPath, extractPath)
Console.WriteLine("Files have been successfully extracted.")
Catch ex As Exception
' Catch any exceptions that occur during the extraction process
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
End Sub
End Class
- Namespace Usage: The
System.IO.Compression
namespace is used to access theZipFile
class, which provides static methods for creating, extracting, and manipulating ZIP archives. - Error Handling: The extraction is wrapped in a try-catch block to handle any potential exceptions, such as issues with file paths or access permissions.
This script will extract files from a ZIP archive located at C:\example\myArchive.zip
and place them into the C:\example\extractedFiles
directory. Adjust the zipPath
and extractPath
strings as necessary for your use case.