Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement zip compression in .Net ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 440
    Comment on it

    Implementing zip compression in .Net

     

    ZipFile class is provided by .Net framework for zip compression. This class provides static methods for creation, extraction, and opening of zip archives. The namespace required for zip compression is System.IO.Compression.

     

    Program to demonstrate zip compression in .Net :-

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO.Compression;
    
    namespace ConsoleApplication1
    {
    
        class Program
        {
            static void Main()
            {
                try
                {
                    string startingPath = @"d:\deepika\angular";
                    string zippingPath = @"d:\deepika\angular.zip";
                    string extractingPath = @"d:\deepika\extract";
    
                    ZipFile.CreateFromDirectory(startingPath, zippingPath);
    
                    ZipFile.ExtractToDirectory(zippingPath, extractingPath);
                }
                catch(Exception e)
                {
                    Console.WriteLine("An exception occurred {0}",e);
                }
    
                Console.ReadKey();
            }
        }
    }
    

     

    In above program two static methods CreateFromDirectory and ExtractToDirectory of ZipFile  class is used for zip compression. CreateFromDirectory method is used for creating zip archive that is the container of the files and directories from the specified directory, on the other hand ExtractToDirectory is used for extracting all the files from the specified zip archived to a specified directory on the file system.

     

    In above example startingPath is a variable initialized with the path of folder which needs to be zipped,zippingPath is a variable initialized with the path of resulted zipped folder and extractingPath is a variable initialized with the path where resulted zipped folder is to be extracted. The statement  ZipFile.CreateFromDirectory(startingPath, zippingPath) created a zipped folder of the folder specified by startingPath in th location of the name specified by zippingPath. The statement ZipFile.ExtractToDirectory(zippingPath, extractingPath) extracts all the of resulted zipped folder specified by zippingPath to a folder specified by extractingPath.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: