Click here to Skip to main content
15,905,566 members
Articles / Programming Languages / C#
Article

Using Resource File for Adding and Extracting ZIP File

Rate me:
Please Sign up or sign in to vote.
3.71/5 (12 votes)
28 Aug 2007CPOL 39.2K   578   22   2
Using Resource file for adding and extracting ZIP file

Introduction

Recently, a requirement had come to me wherein I had to copy some files to a given destination at runtime. So the best thing that came to my mind was to use the Resource (RESX) file for storing the zipped file, and at runtime extracting it and putting it into the destination path.

I used SharpZipLib for extracting zip files.

Using the Code

Before beginning, you need to add a reference to SharpZipLib.dll.

Following were the steps followed to achieve the requirement:

Step 1: Right click on Project in Solution Explorer and add a Resource file.

Step 2: Add the ZIP file into the Resource file.

Next use the following code to extract the zip:

C#
using System.Text;
using System.IO; 
using System.Resources; 
using ICSharpCode.SharpZipLib.Zip; 
using ICSharpCode.SharpZipLib.GZip; 
using System.Resources; 
using System.Reflection;

/// <summary> 
/// 
/// </summary> 

private void ExtractFiles() 
{ 
    try 
    { 
        ResourceManager objResMgr = new ResourceManager 
            ("namespace.resource_filename", Assembly.GetExecutingAssembly()); 
        byte[] objData = (byte[])objResMgr.GetObject("zipfilename"); 
        MemoryStream objMS = new MemoryStream(objData); 
        ZipInputStream objZIP = new ZipInputStream(objMS); 
        ZipEntry theEntry; 
        while ((theEntry = objZIP.GetNextEntry()) != null) 
        { 
            FileStream streamWriter = 
                File.Create(Path.Combine("destination path", theEntry.Name)); 
            int size = objData.Length; 
            byte[] data = new byte[size]; 
            while (true) 
            { 
                size = objZIP.Read(data, 0, data.Length); 
                if (size > 0) 
                { 
                    streamWriter.Write(data, 0, size); 
                } 
                else 
                { 
                    break; 
                } 
            } 
            streamWriter.Close(); 
        } 
        objZIP.Close(); 
    } 
    catch (MissingManifestResourceException ex) 
    { 
    } 
    catch (Exception e1) 
    { 
    } 
}

Just be careful with the namespace.resourcefilename and zip file name that you add to the Resource file in the following:

C#
ResourceManager objResMgr = new ResourceManager
    ("namespace.resource_filename", Assembly.GetExecutingAssembly()); 

byte[] objData = (byte[])objResMgr.GetObject("zipfilename");

History

  • 29th August, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Software Developer

Comments and Discussions

 
QuestionCompilation error Pin
rajesh.arthimalla4-Jul-13 4:05
rajesh.arthimalla4-Jul-13 4:05 
GeneralOther Article Pin
metcarob28-Aug-07 21:48
metcarob28-Aug-07 21:48 
Have you seen the article titled "Store and Extract multiple compressed files in an archive & Extract from Resource"

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.