Click here to Skip to main content
Licence CPOL
First Posted 28 Aug 2007
Views 13,423
Downloads 175
Bookmarked 17 times

Using Resource File for Adding and Extracting ZIP File

By | 28 Aug 2007 | Article
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:

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:

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)

About the Author

Anshuman Roy

Web Developer

India India

Member

Software Developer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOther Article Pinmembermetcarob21:48 28 Aug '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 29 Aug 2007
Article Copyright 2007 by Anshuman Roy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid