Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

Unzip with zlib without using SetCurrentDirectory

I'm using zlib for unzip list of zip files. Here is the simple way to do it. But SetCurrentDirectory function calling affect my other threads. is there any way to unzip to specific directory using zlib.

SetCurrentDirectory("c:\\docs\\stuff");
HZIP hz = OpenZip("c:\\stuff.zip",0);
ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
for (int i=0; i<numitems; i++)
{ GetZipItem(hz,i,&ze);
   UnzipItem(hz,i,ze.name);
 }
 CloseZip(hz);

Upvotes: 0

Views: 970

Answers (2)

Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

Found the solution.

void unZipPackage(std::wstring zip_file,std::wstring dest_dir){ 

    HZIP hz = OpenZip(zip_file.c_str(),0);  
    ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
    for (int i=0; i<numitems; i++)
    { 
        GetZipItem(hz,i,&ze);               
        wchar_t dest[MAX_PATH];
        swprintf(dest,MAX_PATH,L"%s\\%s",dest_dir.c_str(),ze.name);     
        UnzipItem(hz,i,dest);       
    }   
    CloseZip(hz);
}

Upvotes: 1

cha
cha

Reputation: 10411

What you have there is a wrapper allowing easy usage of zlib library. You have tagged the question as a C++, you are using a wrapper for c++, and at the same time you are using the global helper APIs utilising this wrapper.

I recommend you use the TUnzip wrapper directly (see how UnzipItemInternal is implemented as an example). The TUnzip class has a nice method allowing to set the base directory ZRESULT TUnzip::SetUnzipBaseDir(const TCHAR *dir). Call it to set the directory

Upvotes: 1

Related Questions