This tutorial is the second part of the tutorial Compressing files in C#.If you’ve not read that one,I suggest you read it.
ExtractToDirectory :
In this tutorial we’ll be focusing on Decompressing files in C#.To follow along create a ConsoleApplication project in Visual Studio or you preferred IDE,name it Decompression Test.Here is the code to decompress a compressed archive to a specified folder(PS : The code below can be immediately compiled just like the first part.) :
using
System; using System.IO; using System.IO.Compression;namespace Decompression_Test
{
class Program{
static void Main(string[] args){
// A Zip archive to decompress. string sourceArchive = null; // A folder to put the extracted files into. string destinationFolder = null;// To check if a valid file is selected. bool IsSelectedFileValid = false; while (IsSelectedFileValid != true)// Loop until a valid filename is found.
{
Console.WriteLine("Please enter path to a Zip archive..."); Console.WriteLine(); sourceArchive = Console.ReadLine();// Get path entered by user. if (sourceArchive.Contains("\""))// Strip off inverted commas. sourceArchive = sourceArchive.Replace("\"", ""); if (!File.Exists(sourceArchive))// If the entered path doesn't exist.{
Console.WriteLine("The path you entered is invalid.Please try again."); Console.WriteLine();}
else// If the entered path exists.{
IsSelectedFileValid = true;}
}
// To check if the destination folder is valid. bool IsSelectedPathValid = false; while (IsSelectedPathValid != true)// Loop until a valid path is entered.
{
Console.WriteLine("Please enter the destination folder..."); Console.WriteLine(); destinationFolder = Console.ReadLine();// Get the path entered by user. if (destinationFolder.Contains("\""))// Strip off all inverted commas. destinationFolder = destinationFolder.Replace("\"", ""); if (!Directory.Exists(destinationFolder))// If the entered path is invalid.{
Console.WriteLine("The destination folder is invalid.Try again with a valid one."); Console.WriteLine(); IsSelectedPathValid = false;}
else// The user entered a valid path.{
IsSelectedPathValid = true;// Break the loop.}
}
try
{
// We need to check out if the file is actually a Zip archive or it had been renamed to look like Zip file. // If an InvalidDataException is thrown,it flags the reality of file,this means whatever the file be,its most probably not // a Zip archive or is corrupted. ZipFile.ExtractToDirectory(sourceArchive, destinationFolder);// Extract all files in the archive to destination folder. Console.WriteLine("Successfully extracted the archive.Press Enter to exit."); Console.Read();}
catch (System.IO.InvalidDataException)
{
Console.WriteLine("The archive you selected is either damaged or its another filetype with .zip extension.Press Enter to exit."); Console.Read();}
catch (Exception exception)
{
Console.WriteLine(exception.Message + "Press Enter to exit."); Console.Read();}
}
}
}
Go on build and run the solution,enter path to a Zip file on disk,press enter,now enter a target folder’s path(use Desktop =>%SystemRoot/Users/UserName/Desktop) and press enter if the file you selected is valid one you’ll see contents on your desktop folder.Now find a mp3,exe,png or any other type of file rename its extension to .zip and try to open this one,when you do that you’ll see the error message written in catch block of InvalidDataException,this means any invalid operations throw this exception.Again here is the project if you need.
No comments :
Post a Comment