Click here to Skip to main content
15,884,237 members
Articles / Desktop Programming / ATL
Tip/Trick

Quickly unzipping a file

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
22 Jan 2014CPOL 12.6K   7   2
How to use the shell to unzip a file to a folder location

Introduction

This is a small code snippet that uses the shell to unzip a zip-file. It is meant to to be used from your application if you need a simple, uncomplicated way to quickly unzip something, without the need to include other libraries or write lots of code. It is not meant to be a replacement for a complete, flexible solution that is able to deal with zip files in general. 

Honsestly, the code is not my idea. I found it on MSDN and I just adapted it to use ATL's save pointers and made it a bit more compact.

Using the code

Add the function to your application and just call

C++
HRESULT hr = Unzip2Folder(aZIPFile, aDestinationFolder);
// error checking here

And here is the function:

C++
HRESULT Unzip2Folder(LPCTSTR zipFileName, LPCTSTR outFolderName)
{
  // the shell object
  CComPtr<ishelldispatch> shell;
  HRESULT hr = shell.CoCreateInstance(CLSID_Shell);
  if (FAILED(hr)) {
    return hr;
  }

  // the zip file
  CComPtr<folder> zipFile;
  hr = shell->NameSpace(CComVariant(zipFileName), &zipFile);
  if (FAILED(hr)) {
    return hr;
  }

  // destination folder
  CComPtr<folder> destination;
  hr = shell->NameSpace(CComVariant(outFolderName), &destination);
  if (FAILED(hr)) {
    return hr;
  }

  // folder items inside the zip file
  CComPtr<folderitems> folderItems;
  hr = zipFile->Items(&folderItems);
  if (FAILED(hr)) {
    return hr;
  }

  // copy operation
  hr = destination->CopyHere(CComVariant(folderItems),
      CComVariant(2048 | 1024 | 512 | 16 | 4, VT_I4));

  return hr;
}
</folderitems></folder></folder></ishelldispatch>

If you need more information you should probably read about IShellDispatch, Folder and FolderItems.

For the flags used in CopyHere please look at Folder::CopyHere. Basically they supress all UIs that may appear. Depending on your application you might want to change this.

And that's all, folkes!

License

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


Written By
Software Developer
Germany Germany
Born in 1968 I do programming since over 25 years now. I started with Basic on a ZX81 and with hacking hexcodes in a Microprofessor before I switched to C++ and other languages.

Since more than 10 years I work as a professional software developer, currently for Salsitasoft in Prague.

Comments and Discussions

 
QuestionI found the solution to my own problem Pin
FredWah2-Jun-23 11:00
FredWah2-Jun-23 11:00 
QuestionHow to get folder name from zip file Pin
FredWah2-Jun-23 10:30
FredWah2-Jun-23 10:30 

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.