Click here to Skip to main content
13,400,497 members (31,665 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 9 Nov 2011

Zip/Unzip using Windows Shell

, 8 Jan 2012
Rate this:
Please Sign up or sign in to vote.
A module for compressing / decompressing files in windows using Windows shell


Okay, so here I was supposed to write a module for compressing / decompressing files in Windows. I went through a lot of stuff over the net and found some open source libraries to do it. Out of those, I narrowed down to the one provided by Info-Zip. Although no longer actively maintained, this has been in existence for a long time and is known to be stable. This is used in various forms in a number of programs including Mac OS X.

However, on Windows, there is another way to zip files free of cost. I found this article - Easily zip / unzip files using Windows Shell32 on which provides a neat way to use Windows routines to zip and unzip files programmatically. The code provided is in VB but since my requirement was in C++, I wrote a small C++ module which is shared here. I found the discussion and code snippets provided here to be very useful.


To compress and zip files, first we need to create the required zip file. The code below creates “” in "C:"

Code Snippet

// Create Zip file
BYTE startBuffer[] = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
FILE *f = fopen("C:\\", "wb");

Now that our zip file is created, let's add the files to be compressed inside this zip. For this, we use the CopyHere function wherein we treat our newly created zip file as a folder.

Code Snippet

BSTR source = L"C:\\test.txt\0\0";
BSTR dest = L"C:\\\\\0\0";

HRESULT hResult;
IShellDispatch *pISD;
Folder *pToFolder = NULL;
VARIANT vDir, vFile, vOpt;


hResult = CoCreateInstance
(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&pISD);

if (SUCCEEDED(hResult))
  vDir.vt = VT_BSTR;
  vDir.bstrVal = dest;
  // Destination is our zip file
  hResult = pISD->NameSpace(vDir, &pToFolder);
  if (SUCCEEDED(hResult))
    // Now copy source file(s) to the zip
    vFile.vt = VT_BSTR;
    vFile.bstrVal = source;

    /* ****NOTE**** To copy multiple files into the zip, need to create a FolderItems
     * object(see unzip implementation below for more details)  */
    vOpt.vt = VT_I4;
    vOpt.lVal = FOF_NO_UI;  //Do not display a progress dialog box, not useful in compression

    // Copying and compressing the source files to our zip
    hResult = pToFolder->CopyHere(vFile, vOpt);

    // CopyHere() creates a separate thread to copy files and 
    // it may happen that the main thread exits before the 
    // copy thread is initialized. So we put the main thread to sleep 
    // for a second to give time for the copy thread to start.

Unzip is similar to zip except that we now have zip (treated as source folder) and destination which anyways is a folder. The code below currently assumes that there already exists the destination folder, however adding a code to check for a folder or create a new folder on the fly should not be difficult.

The minor change (compared to the above code) in unzip comes only in identifying the source files. Here, we use the folderItems, which is essentially a list of all files/folders inside the given folder, instead of a single source file.

Code Snippet

if (SUCCEEDED(hResult))
  Folder *pFromFolder = NULL;
  vFile.vt = VT_BSTR;
  vFile.bstrVal = source;

  pISD->NameSpace(vFile, &pFromFolder);
  FolderItems *fi = NULL;

  vOpt.vt = VT_I4;
  vOpt.lVal = FOF_NO_UI; // Do not display a progress dialog box

  // Creating a new Variant with pointer to FolderItems to be copied
  newV.vt = VT_DISPATCH;
  newV.pdispVal = fi;

  hResult = pToFolder->CopyHere(newV, vOpt);


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


About the Author

Aniruddha Loya
Student EPFL
Switzerland Switzerland
After almost 4 years of experience varying from finance to building & running a startup... I decided to take a break, go back to college and be a (academic) student again.
However, I continue to do freelance work and am currently working on HTML5 games for Code-Heads, a UK based studio.

You may also be interested in...


Comments and Discussions

QuestionWhy people do not use ATL? Pin
constm17-Jun-13 3:54
memberconstm17-Jun-13 3:54 
QuestionHow to add path to zip. Pin
vvidov6-Nov-12 21:25
membervvidov6-Nov-12 21:25 
AnswerRe: How to add path to zip. Pin
Aniruddha Loya7-Nov-12 2:15
memberAniruddha Loya7-Nov-12 2:15 
QuestionUnzip to memory Pin
JarmoP28-Mar-12 7:20
memberJarmoP28-Mar-12 7:20 
AnswerRe: Unzip to memory Pin
Aniruddha Loya28-Mar-12 8:33
memberAniruddha Loya28-Mar-12 8:33 
QuestionCannot control the UI Pin
Ranojay6-Feb-12 1:18
memberRanojay6-Feb-12 1:18 
AnswerRe: Cannot control the UI Pin
Aniruddha Loya6-Feb-12 1:37
memberAniruddha Loya6-Feb-12 1:37 
GeneralRe: Cannot control the UI Pin
Ranojay6-Feb-12 2:19
memberRanojay6-Feb-12 2:19 
GeneralRe: Cannot control the UI Pin
Aniruddha Loya6-Feb-12 3:27
memberAniruddha Loya6-Feb-12 3:27 
GeneralMy vote of 5 Pin
Mihai MOGA18-Dec-11 4:43
memberMihai MOGA18-Dec-11 4:43 
GeneralRe: My vote of 5 Pin
Aniruddha Loya19-Dec-11 2:51
memberAniruddha Loya19-Dec-11 2:51 
GeneralMy vote of 5 Pin
Pablo Aliskevicius14-Nov-11 21:29
memberPablo Aliskevicius14-Nov-11 21:29 
GeneralRe: My vote of 5 Pin
Aniruddha Loya14-Nov-11 21:56
memberAniruddha Loya14-Nov-11 21:56 
GeneralReally Good One Pin
MANISH RASTOGI14-Nov-11 19:55
memberMANISH RASTOGI14-Nov-11 19:55 
GeneralRe: Really Good One Pin
Aniruddha Loya14-Nov-11 21:54
memberAniruddha Loya14-Nov-11 21:54 
QuestionDotNetZip Pin
DaveRRR14-Nov-11 8:59
memberDaveRRR14-Nov-11 8:59 
AnswerRe: DotNetZip Pin
Aniruddha Loya14-Nov-11 22:16
memberAniruddha Loya14-Nov-11 22:16 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180218.2 | Last Updated 8 Jan 2012
Article Copyright 2011 by Aniruddha Loya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid