-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunzip.cpp
92 lines (79 loc) · 2.39 KB
/
unzip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//See http://www.codeproject.com/KB/cs/compresswithwinshellapics.aspx
#include <stdio.h>
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <shldisp.h>
#define FOF_NO_UI (FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR)
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "shlwapi.lib")
int main (int argc, char **argv) {
wchar_t fin[30000];
wchar_t odir[30000];
HRESULT hr;
IShellDispatch *pISD;
Folder *pToFolder = NULL;
VARIANT vDir, vFile, vOpt;
wchar_t** wargv = CommandLineToArgvW (GetCommandLineW(), &argc);
if (argc != 3) {
printf("Usage: %S infile emptyDir/non-existentDir\n", wargv[0]);
return 1;
}
if (PathFileExistsW(wargv[1])) {
if (PathIsDirectoryW(wargv[1]))
return 1;
else
if (GetFullPathNameW(wargv[1], 30000, fin, NULL) < 4) return 1;
} else return 1;
if (PathFileExistsW(wargv[2])) {
if (PathIsDirectoryW(wargv[2])) {
if (PathIsDirectoryEmpty (wargv[2])) {
if (GetFullPathNameW(wargv[2], 30000, odir, NULL) < 4) return 1;
} else
return 1;
}
} else {
if (GetFullPathNameW(wargv[2], 30000, odir, NULL) < 4) return 1;
if (SHCreateDirectoryExW(NULL, odir, NULL) != ERROR_SUCCESS) return 1; //deprecation or just die?
}
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&pISD);
if (SUCCEEDED(hr))
{
VariantInit(&vDir);
vDir.vt = VT_BSTR;
vDir.bstrVal = odir;
// Destination is our zip file
hr = pISD->NameSpace(vDir, &pToFolder);
if (SUCCEEDED(hr))
{
Folder *pFromFolder = NULL;
VariantInit(&vFile);
vFile.vt = VT_BSTR;
vFile.bstrVal = fin;
pISD->NameSpace(vFile, &pFromFolder);
FolderItems *fi = NULL;
pFromFolder->Items(&fi);
VariantInit(&vOpt);
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
VARIANT newV;
VariantInit(&newV);
newV.vt = VT_DISPATCH;
newV.pdispVal = fi;
hr = pToFolder->CopyHere(newV, vOpt);
Sleep(500);
pFromFolder->Release();
pToFolder->Release();
}
pISD->Release();
}
CoUninitialize();
if (SUCCEEDED(hr))
return 0;
return 1;
}