forked from lioncash/ExtractData
/
ExtractData.cpp
658 lines (544 loc) · 14.7 KB
/
ExtractData.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include "stdafx.h"
#include "Common.h"
#include "Dialog/ExistsDialog.h"
#include "Dialog/FileDialog.h"
#include "Dialog/FolderInputDialog.h"
#include "ArcFile.h"
//#include "DataBase/DataBase.h"
//#include "DragDrop/DataObject.h"
//#include "DragDrop/DropSource.h"
#include "ExtractData.h"
#define WM_INPUT_FILE (WM_USER+1)
#define WM_OUTPUT_FILE (WM_USER+2)
#define WM_THREAD_END (WM_USER+3)
BOOL g_bThreadEnd;
CExtractData::CExtractData()
{
m_hParentWnd = NULL;
m_hParentInst = NULL;
m_pOption = NULL;
m_pListView = NULL;
GetModuleFileName(NULL, m_szPathToTmpFileList, MAX_PATH);
PathRemoveFileSpec(m_szPathToTmpFileList);
PathAppend(m_szPathToTmpFileList, _T("TmpFileList.txt"));
}
void CExtractData::Init(HWND hWnd, SOption& option, CMainListView& listview)
{
m_hParentWnd = hWnd;
m_hParentInst = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
m_pOption = &option;
m_pListView = &listview;
}
void CExtractData::Open(LPTSTR pOpenDir)
{
std::vector<TCHAR> szFileNames(MAX_PATH * 1000, 0);
CFileDialog clFileDlg;
if (clFileDlg.DoModal(m_hParentWnd, &szFileNames[0], pOpenDir))
{
Mount(&szFileNames[0]);
}
}
void CExtractData::OpenHistory(YCString& clsFilePath)
{
std::vector<TCHAR> szFileNames(32000, 0);
lstrcpy(&szFileNames[0], clsFilePath);
Mount(&szFileNames[0]);
}
void CExtractData::OpenDrop(WPARAM wp)
{
HDROP hDrop = (HDROP)wp;
UINT uFileNo = DragQueryFile(hDrop, -1, NULL, 0);
std::vector<TCHAR> szFileNames(MAX_PATH * uFileNo, 0);
DragQueryFile(hDrop, 0, &szFileNames[0], MAX_PATH);
if (uFileNo >= 2)
{
// Gets the first file name
TCHAR filename[MAX_PATH];
lstrcpy(filename, PathFindFileName(&szFileNames[0]));
// Get the directory name
PathRemoveFileSpec(&szFileNames[0]);
// The following refers to the '\0'
TCHAR *pszFileName = &szFileNames[lstrlen(&szFileNames[0]) + 1];
// Attach filename following the '\0'
memcpy(pszFileName, filename, lstrlen(filename));
pszFileName += lstrlen(filename) + 1;
for (int i = 1; i < (int)uFileNo; i++)
{
DragQueryFile(hDrop, i, filename, MAX_PATH);
memcpy(pszFileName, PathFindFileName(filename), lstrlen(PathFindFileName(filename)));
pszFileName += lstrlen(PathFindFileName(filename)) + 1;
}
}
DragFinish(hDrop);
Mount(&szFileNames[0]);
}
void CExtractData::Close()
{
// Close all open archive files
std::vector<CArcFile*>& rArcList = m_ArcList;
if (!rArcList.empty())
{
for (size_t i = 0; i < rArcList.size(); i++)
delete rArcList[i];
rArcList.clear();
}
// Close the decoding class
CExtract::Close();
// Delete all temporary files
DeleteTmpFile();
// Clear the list view
m_pListView->Clear();
}
void CExtractData::Mount(LPCTSTR c_pclArcNames)
{
Close(); // Close the last opened
m_pclArcNames = c_pclArcNames;
m_bInput = TRUE;
DialogBoxParam(m_hParentInst, _T("PROGBAR"), m_hParentWnd, (DLGPROC)WndStaticProc, (LPARAM)this);
m_pListView->Show();
}
UINT WINAPI CExtractData::MountThread(LPVOID lpParam)
{
CExtractData* pObj = (CExtractData*)lpParam;
try
{
SOption* pOption = pObj->m_pOption;
LPCTSTR pclArcNames = pObj->m_pclArcNames;
std::vector<SFileInfo>& rEnt = pObj->m_pListView->GetFileInfo();
std::vector<YCString> sArcNameList;
YCString sArcNames(pclArcNames);
sArcNameList.push_back(sArcNames);
pclArcNames += lstrlen(pclArcNames) + 1; // Because of ZeroMemory, we have to add one for the null terminator char.
// If you select more than one
if (lstrcmp(pclArcNames, _T("")) != 0)
{
// Get directory name
TCHAR tmpDir[_MAX_DIR];
lstrcpy(tmpDir, sArcNameList[0]);
PathAddBackslash(tmpDir);
YCString Dir(tmpDir);
sArcNameList.clear();
// Directory Name + Filename in the list.
while (lstrcmp(pclArcNames, _T("")) != 0)
{
YCString sArcName(pclArcNames);
sArcNameList.push_back(Dir + sArcName);
pclArcNames += lstrlen(pclArcNames) + 1;
}
sort(sArcNameList.begin(), sArcNameList.end());
}
// Entire file size
QWORD AllArcSize = 0;
std::vector<CArcFile*>& rArcList = pObj->m_ArcList;
for (std::vector<YCString>::iterator itr = sArcNameList.begin(); itr != sArcNameList.end(); )
{
// Open the archive file
CArcFile* pclArc = new CArcFile();
if (!pclArc->Open(*itr))
{
itr = sArcNameList.erase(itr); // Remove archive files that could not be opened from the list
delete pclArc;
}
else
{
// Add archive filesize
AllArcSize += pclArc->GetArcSize();
rArcList.push_back(pclArc);
itr++;
}
}
// Initialize the progress bar
CProgBar prog;
prog.Init(pObj->m_hWnd, AllArcSize);
// Reading
DWORD dwArcID = 0;
//for (std::vector<CArcFile*>::iterator itrArc = rArcList.begin(); itrArc != rArcList.end(); ) {
for (size_t i = 0; i < rArcList.size(); i++)
{
CArcFile* pclArc = rArcList[i];
pclArc->SetArcID(dwArcID);
pclArc->SetEnt(rEnt);
pclArc->SetProg(prog);
pclArc->SetOpt(pOption);
// View the archive filename
prog.SetArcName(pclArc->GetArcName());
// Corresponding file
if (CExtract::Mount(pclArc) == TRUE)
{
dwArcID++;
pclArc->SetState(TRUE);
}
}
//MessageBox(pObj->m_hWnd, "", "", MB_OK);
// Progress towards 100%
prog.UpdatePercent();
}
catch (std::bad_alloc)
{
// Out of memory
CError error;
error.bad_alloc(pObj->m_hWnd);
}
catch (...)
{
// or if canceled
}
PostMessage(pObj->m_hWnd, WM_THREAD_END, 0, 0);
_endthreadex(0);
return 0;
}
void CExtractData::Save(DWORD ExtractMode, LPTSTR pSaveDir, BOOL bConvert)
{
SOption* pstOption = m_pOption;
if (pstOption->bSaveSel)
{
// Specifies output destination
CFolderInputDialog clFolderInputDlg;
if (clFolderInputDlg.DoModal(m_hParentWnd, pSaveDir ) == IDOK)
{
Decode(ExtractMode, pSaveDir, bConvert);
}
}
else if (pstOption->bSaveSrc)
{
// Output to input destination
Decode(ExtractMode, NULL, bConvert);
}
else if (pstOption->bSaveDir)
{
// Output to a fixed destination
Decode(ExtractMode, pstOption->SaveDir, bConvert);
}
}
void CExtractData::SaveSel(LPTSTR pSaveDir, BOOL bConvert)
{
Save(EXTRACT_SELECT, pSaveDir, bConvert);
}
void CExtractData::SaveAll(LPTSTR pSaveDir, BOOL bConvert)
{
Save(EXTRACT_ALL, pSaveDir, bConvert);
}
void CExtractData::SaveDrop()
{
/*
YCString sFiles;
YCString sSaveDir = m_pOption->TmpDir + _T('\\');
CMainListView* pListView = m_pListView;
int nItem = -1;
while ((nItem = pListView->GetNextItem(nItem)) != -1)
{
sFiles += sSaveDir + pListView->GetFileInfo()[nItem].name;
sFiles += _T('\0');
}
sFiles += _T('\0');
CDataObject* pObj = new CDataObject();
CDropSource* pSrc = new CDropSource();
// Set the structure of FORMATETC
FORMATETC fmtetc;
fmtetc.cfFormat = CF_HDROP;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
// Set the structure of STGMEDIUM
STGMEDIUM medium;
medium.tymed = TYMED_HGLOBAL;
medium.hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(DROPFILES) + sFiles.size());
medium.pUnkForRelease = NULL;
LPTSTR p = (LPTSTR)GlobalLock(medium.hGlobal);
((DROPFILES*)p)->pFiles = sizeof(DROPFILES);
((DROPFILES*)p)->fWide = FALSE;
CopyMemory(p + sizeof(DROPFILES), sFiles, sFiles.size());
GlobalUnlock(medium.hGlobal);
// Sets IDataObject
pObj->SetData(&fmtetc, &medium, FALSE);
// Starts OLE Drag & Drop
DWORD effect;
HRESULT hr = DoDragDrop(pObj, pSrc, DROPEFFECT_MOVE | DROPEFFECT_COPY | DROPEFFECT_LINK, &effect);
// Free resources
pObj->Release();
pSrc->Release();
*/
}
void CExtractData::Decode(DWORD ExtractMode, LPCTSTR pSaveDir, BOOL bConvert)
{
CExistsDialog ExistsDlg;
ExistsDlg.GetOverWrite() = 0x01; // Perform Overwrite Confirmation
m_dwExtractMode = ExtractMode;
m_pSaveDir = pSaveDir;
m_bConvert = bConvert;
m_bInput = FALSE;
DialogBoxParam(m_hParentInst, _T("PROGBAR"), m_hParentWnd, (DLGPROC)WndStaticProc, (LPARAM)this);
}
void CExtractData::DecodeTmp()
{
CExistsDialog ExistsDlg;
ExistsDlg.GetOverWrite() = 0x00; // Overwrite confirmation is not perform
m_dwExtractMode = EXTRACT_SELECT;
m_pSaveDir = m_pOption->TmpDir;
m_bConvert = TRUE;
m_bInput = FALSE;
DialogBoxParam(m_hParentInst, _T("PROGBAR"), m_hParentWnd, (DLGPROC)WndStaticProc, (LPARAM)this);
}
UINT WINAPI CExtractData::DecodeThread(LPVOID lpParam)
{
CExtractData* pObj = (CExtractData*)lpParam;
CArcFile* pclArc = NULL;
try
{
SOption* pOption = pObj->m_pOption;
LPCTSTR pSaveDir = pObj->m_pSaveDir;
BOOL bConvert = pObj->m_bConvert;
// Determine entire filesize
std::vector<int> nSelects;
QWORD AllFileSize = 0;
std::vector<CArcFile*>& rArcList = pObj->m_ArcList;
if (pObj->m_dwExtractMode == EXTRACT_SELECT)
{
int nItem = -1;
while ((nItem = pObj->m_pListView->GetNextItem(nItem)) != -1)
{
nSelects.push_back(nItem);
AllFileSize += rArcList[0]->GetFileInfo(nItem)->sizeOrg;
}
}
else
{
size_t nItemCount = rArcList[0]->GetFileInfo().size();
for (int nItem = 0; nItem < (int)nItemCount; nItem++)
{
nSelects.push_back(nItem);
AllFileSize += rArcList[0]->GetFileInfo(nItem)->sizeOrg;
}
}
// Initialize progressbar
CProgBar prog;
prog.Init(pObj->m_hWnd, AllFileSize);
for (size_t i = 0; i < nSelects.size(); i++)
{
SFileInfo* pInfFile = rArcList[0]->GetFileInfo(nSelects[i]);
pclArc = rArcList[pInfFile->arcID];
pclArc->SetProg(prog);
// Create destination folder name from the destination filename input
if (pSaveDir == NULL && pOption->bSaveSrc == TRUE)
{
TCHAR SaveDir[MAX_PATH];
// Get input
lstrcpy(SaveDir, pclArc->GetArcPath());
// Delete the filename
PathRemoveFileSpec(SaveDir);
if (pclArc->GetCtEnt() == 1)
{
// Archive folder creation stores files one by one
TCHAR ArcDirName[_MAX_DIR];
lstrcpy(ArcDirName, PathFindFileName(SaveDir));
PathAppend(SaveDir, _T("_"));
lstrcat(SaveDir, ArcDirName);
}
else
{
// Create a folder with the name of the archive files if there are more than two files present
PathAppend(SaveDir, _T("_"));
lstrcat(SaveDir, pclArc->GetArcName());
}
pclArc->SetSaveDir(SaveDir);
}
else
{
pclArc->SetSaveDir(pSaveDir);
}
pclArc->SetFileInfo(nSelects[i]);
// View filename
prog.SetFileName(pInfFile->name);
// Extraction
CExtract::Decode(pclArc, bConvert);
// Close the file (For when I forget to close it)
pclArc->CloseFile();
}
//MessageBox(pObj->m_hWnd, "", "", MB_OK);
}
catch (std::bad_alloc)
{
// Out of memory error
CError error;
error.bad_alloc(pObj->m_hWnd);
}
catch (...)
{
// Or if we cancel the operation
//CError error;
//error.Message(pObj->m_hWnd, "");
// Corresponds to the case where the file was still not closed, even with the exception thrown.
//if (pclArc != NULL)
// pclArc->CloseFile();
}
// Corresponds to the case where the file was still not closed, even with the exception thrown.
if (pclArc != NULL)
pclArc->CloseFile();
// Send message to terminate the thread
PostMessage(pObj->m_hWnd, WM_THREAD_END, 0, 0);
_endthreadex(0);
return 0;
}
void CExtractData::OpenRelate()
{
CMainListView* pListView = m_pListView;
int nItem = -1;
while ((nItem = pListView->GetNextItem(nItem)) != -1)
{
std::set<YCString>& sTmpFilePathList = pListView->GetFileInfo()[nItem].sTmpFilePath;
if (!sTmpFilePathList.empty())
{
const YCString& clsTmpFilePath = *sTmpFilePathList.begin();
// Open from file association
if (::ShellExecute( NULL, NULL, clsTmpFilePath, NULL, NULL, SW_SHOWNORMAL ) == (HINSTANCE) SE_ERR_NOASSOC)
{
// If there is no association with the file, issue a
// dialog to select an application which can open the file
YCString clsParam = _T("shell32.dll, OpenAs_RunDLL ") + clsTmpFilePath;
::ShellExecute( NULL, NULL, _T("rundll32.exe"), clsParam, NULL, TRUE );
}
// Added to the temporary file list
for (std::set<YCString>::iterator itr = sTmpFilePathList.begin() ; itr != sTmpFilePathList.end() ; itr++)
{
if (*itr != _T(""))
{
m_ssTmpFile.insert(*itr);
}
}
sTmpFilePathList.clear();
}
}
}
void CExtractData::DeleteTmpFile()
{
// Add the last remaining temporary files
LoadTmpFileList();
for (std::set<YCString>::iterator itr = m_ssTmpFile.begin(); itr != m_ssTmpFile.end(); )
{
TCHAR szTmp[MAX_PATH];
lstrcpy(szTmp, *itr);
// Delete temporary files
if (PathFileExists(szTmp))
{
// File exists
if (!DeleteFile(szTmp))
{
// Fails to remove it
itr++;
continue;
}
}
while (lstrcmp(szTmp, m_pOption->TmpDir) != 0)
{
// Delete folder
if (!PathRemoveFileSpec(szTmp))
{
break;
}
RemoveDirectory(szTmp);
}
itr = m_ssTmpFile.erase(itr);
}
// Save the list of remaining temp files
SaveTmpFileList();
}
void CExtractData::LoadTmpFileList()
{
CFile clfTmpFileList;
if (clfTmpFileList.Open(m_szPathToTmpFileList, FILE_READ) == INVALID_HANDLE_VALUE)
{
// Failed to open import file
return;
}
while (1)
{
char buf[MAX_PATH];
if (clfTmpFileList.ReadLine(buf, sizeof(buf), TRUE) == 0)
break;
m_ssTmpFile.insert(buf);
}
}
void CExtractData::SaveTmpFileList()
{
if (m_ssTmpFile.empty())
{
// Was able to remove all
DeleteFile(m_szPathToTmpFileList);
return;
}
CFile clfTmpFileList;
if (clfTmpFileList.Open(m_szPathToTmpFileList, FILE_WRITE) == INVALID_HANDLE_VALUE)
{
// Failed to open file for writing
return;
}
for (std::set<YCString>::iterator itr = m_ssTmpFile.begin(); itr != m_ssTmpFile.end(); itr++)
{
clfTmpFileList.WriteLine(*itr);
clfTmpFileList.Write("\r\n", 2);
}
m_ssTmpFile.clear();
}
LRESULT CExtractData::WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
static HANDLE hThread;
UINT thId;
switch (msg)
{
case WM_INITDIALOG:
{
CWindowBase::Init();
g_bThreadEnd = FALSE;
SetFocus(GetDlgItem(hWnd, IDC_CANCEL));
if (m_bInput)
{
// Reading
SetWindowText(hWnd, _T("Reading the file..."));
PostMessage(hWnd, WM_INPUT_FILE, 0, 0);
}
else
{
// Extraction
SetWindowText(hWnd, _T("Extracting..."));
PostMessage(hWnd, WM_OUTPUT_FILE, 0, 0);
}
}
return FALSE;
case WM_COMMAND:
{
switch (LOWORD(wp))
{
case IDCANCEL:
case IDC_CANCEL:
// Cancel is pressed
g_bThreadEnd = TRUE;
return FALSE;
}
}
return FALSE;
case WM_INPUT_FILE:
{
// Information Acquisition Start
hThread = (HANDLE) _beginthreadex( NULL, 0, MountThread, this, 0, &thId );
//hThread = CreateThread(NULL, 0, MountThread, this, 0, &thId);
}
return FALSE;
case WM_OUTPUT_FILE:
{
// Start extraction process
hThread = (HANDLE) _beginthreadex( NULL, 0, DecodeThread, this, 0, &thId );
//hThread = CreateThread(NULL, 0, DecodeThread, this, 0, &thId);
}
return FALSE;
case WM_THREAD_END:
{
// Finish the process
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
EndDialog(hWnd, IDOK);
}
return TRUE;
}
return FALSE;
}