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
|
// PanelSplitFile.cpp
#include "StdAfx.h"
#include "../../../Common/IntToString.h"
#include "../../../Windows/ErrorMsg.h"
#include "../../../Windows/FileName.h"
#include "../GUI/ExtractRes.h"
#include "resource.h"
#include "App.h"
#include "CopyDialog.h"
#include "FormatUtils.h"
#include "LangUtils.h"
#include "SplitDialog.h"
#include "SplitUtils.h"
#include "PropertyNameRes.h"
using namespace NWindows;
using namespace NFile;
using namespace NDir;
static const char * const g_Message_FileWriteError = "File write error";
struct CVolSeqName
{
UString UnchangedPart;
UString ChangedPart;
CVolSeqName(): ChangedPart("000") {}
void SetNumDigits(UInt64 numVolumes)
{
ChangedPart = "000";
while (numVolumes > 999)
{
numVolumes /= 10;
ChangedPart.Add_Char('0');
}
}
bool ParseName(const UString &name)
{
if (name.Len() < 2)
return false;
if (name.Back() != L'1' || name[name.Len() - 2] != L'0')
return false;
unsigned pos = name.Len() - 2;
for (; pos > 0 && name[pos - 1] == '0'; pos--);
UnchangedPart.SetFrom(name, pos);
ChangedPart = name.Ptr(pos);
return true;
}
UString GetNextName();
};
UString CVolSeqName::GetNextName()
{
for (int i = (int)ChangedPart.Len() - 1; i >= 0; i--)
{
const wchar_t c = ChangedPart[i];
if (c != L'9')
{
ChangedPart.ReplaceOneCharAtPos((unsigned)i, (wchar_t)(c + 1));
break;
}
ChangedPart.ReplaceOneCharAtPos((unsigned)i, L'0');
if (i == 0)
ChangedPart.InsertAtFront(L'1');
}
return UnchangedPart + ChangedPart;
}
class CThreadSplit: public CProgressThreadVirt
{
HRESULT ProcessVirt() Z7_override;
public:
FString FilePath;
FString VolBasePath;
UInt64 NumVolumes;
CRecordVector<UInt64> VolumeSizes;
};
class CPreAllocOutFile
{
UInt64 _preAllocSize;
public:
NIO::COutFile File;
UInt64 Written;
CPreAllocOutFile(): _preAllocSize(0), Written(0) {}
~CPreAllocOutFile()
{
SetCorrectFileLength();
}
void PreAlloc(UInt64 preAllocSize)
{
_preAllocSize = 0;
if (File.SetLength(preAllocSize))
_preAllocSize = preAllocSize;
File.SeekToBegin();
}
bool Write(const void *data, UInt32 size, UInt32 &processedSize) throw()
{
bool res = File.Write(data, size, processedSize);
Written += processedSize;
return res;
}
void Close()
{
SetCorrectFileLength();
Written = 0;
_preAllocSize = 0;
File.Close();
}
void SetCorrectFileLength()
{
if (Written < _preAllocSize)
{
File.SetLength(Written);
_preAllocSize = 0;
}
}
};
static const UInt32 kBufSize = (1 << 20);
HRESULT CThreadSplit::ProcessVirt()
{
NIO::CInFile inFile;
if (!inFile.Open(FilePath))
return GetLastError_noZero_HRESULT();
CPreAllocOutFile outFile;
CMyBuffer buffer;
if (!buffer.Allocate(kBufSize))
return E_OUTOFMEMORY;
CVolSeqName seqName;
seqName.SetNumDigits(NumVolumes);
UInt64 length;
if (!inFile.GetLength(length))
return GetLastError_noZero_HRESULT();
CProgressSync &sync = Sync;
sync.Set_NumBytesTotal(length);
UInt64 pos = 0;
UInt64 prev = 0;
UInt64 numFiles = 0;
unsigned volIndex = 0;
for (;;)
{
UInt64 volSize;
if (volIndex < VolumeSizes.Size())
volSize = VolumeSizes[volIndex];
else
volSize = VolumeSizes.Back();
UInt32 needSize = kBufSize;
{
const UInt64 rem = volSize - outFile.Written;
if (needSize > rem)
needSize = (UInt32)rem;
}
UInt32 processedSize;
if (!inFile.Read(buffer, needSize, processedSize))
return GetLastError_noZero_HRESULT();
if (processedSize == 0)
return S_OK;
needSize = processedSize;
if (outFile.Written == 0)
{
FString name = VolBasePath;
name.Add_Dot();
name += us2fs(seqName.GetNextName());
sync.Set_FilePath(fs2us(name));
if (!outFile.File.Create_NEW(name))
{
const HRESULT res = GetLastError_noZero_HRESULT();
AddErrorPath(name);
return res;
}
UInt64 expectSize = volSize;
if (pos < length)
{
const UInt64 rem = length - pos;
if (expectSize > rem)
expectSize = rem;
}
outFile.PreAlloc(expectSize);
}
if (!outFile.Write(buffer, needSize, processedSize))
return GetLastError_noZero_HRESULT();
if (needSize != processedSize)
throw g_Message_FileWriteError;
pos += processedSize;
if (outFile.Written == volSize)
{
outFile.Close();
sync.Set_NumFilesCur(++numFiles);
if (volIndex < VolumeSizes.Size())
volIndex++;
}
if (pos - prev >= ((UInt32)1 << 22) || outFile.Written == 0)
{
RINOK(sync.Set_NumBytesCur(pos))
prev = pos;
}
}
}
void CApp::Split()
{
const unsigned srcPanelIndex = GetFocusedPanelIndex();
CPanel &srcPanel = Panels[srcPanelIndex];
if (!srcPanel.Is_IO_FS_Folder())
{
srcPanel.MessageBox_Error_UnsupportOperation();
return;
}
CRecordVector<UInt32> indices;
srcPanel.Get_ItemIndices_Operated(indices);
if (indices.IsEmpty())
return;
if (indices.Size() != 1)
{
srcPanel.MessageBox_Error_LangID(IDS_SELECT_ONE_FILE);
return;
}
const unsigned index = indices[0];
if (srcPanel.IsItem_Folder(index))
{
srcPanel.MessageBox_Error_LangID(IDS_SELECT_ONE_FILE);
return;
}
const UString itemName = srcPanel.GetItemName(index);
const UString srcPath = srcPanel.GetFsPath() + srcPanel.GetItemPrefix(index);
UString path = srcPath;
unsigned destPanelIndex = (NumPanels <= 1) ? srcPanelIndex : (1 - srcPanelIndex);
CPanel &destPanel = Panels[destPanelIndex];
if (NumPanels > 1)
if (destPanel.IsFSFolder())
path = destPanel.GetFsPath();
CSplitDialog splitDialog;
splitDialog.FilePath = srcPanel.GetItemRelPath(index);
splitDialog.Path = path;
if (splitDialog.Create(srcPanel.GetParent()) != IDOK)
return;
NFind::CFileInfo fileInfo;
if (!fileInfo.Find(us2fs(srcPath + itemName)))
{
srcPanel.MessageBox_Error(L"Cannot find file");
return;
}
if (fileInfo.Size <= splitDialog.VolumeSizes.FrontItem())
{
srcPanel.MessageBox_Error_LangID(IDS_SPLIT_VOL_MUST_BE_SMALLER);
return;
}
const UInt64 numVolumes = GetNumberOfVolumes(fileInfo.Size, splitDialog.VolumeSizes);
if (numVolumes >= 100)
{
wchar_t s[32];
ConvertUInt64ToString(numVolumes, s);
if (::MessageBoxW(srcPanel, MyFormatNew(IDS_SPLIT_CONFIRM_MESSAGE, s),
LangString(IDS_SPLIT_CONFIRM_TITLE),
MB_YESNOCANCEL | MB_ICONQUESTION) != IDYES)
return;
}
path = splitDialog.Path;
NName::NormalizeDirPathPrefix(path);
if (!CreateComplexDir(us2fs(path)))
{
const HRESULT lastError = GetLastError_noZero_HRESULT();
srcPanel.MessageBox_Error_2Lines_Message_HRESULT(MyFormatNew(IDS_CANNOT_CREATE_FOLDER, path), lastError);
return;
}
{
CThreadSplit spliter;
spliter.NumVolumes = numVolumes;
CProgressDialog &progressDialog = spliter;
const UString progressWindowTitle ("7-Zip"); // LangString(IDS_APP_TITLE, 0x03000000);
const UString title = LangString(IDS_SPLITTING);
progressDialog.ShowCompressionInfo = false;
progressDialog.MainWindow = _window;
progressDialog.MainTitle = progressWindowTitle;
progressDialog.MainAddTitle = title;
progressDialog.MainAddTitle.Add_Space();
progressDialog.Sync.Set_TitleFileName(itemName);
spliter.FilePath = us2fs(srcPath + itemName);
spliter.VolBasePath = us2fs(path + srcPanel.GetItemName_for_Copy(index));
spliter.VolumeSizes = splitDialog.VolumeSizes;
// if (splitDialog.VolumeSizes.Size() == 0) return;
// CPanel::CDisableTimerProcessing disableTimerProcessing1(srcPanel);
// CPanel::CDisableTimerProcessing disableTimerProcessing2(destPanel);
if (spliter.Create(title, _window) != 0)
return;
}
RefreshTitleAlways();
// disableNotify.Restore();
// disableNotify.Restore();
// srcPanel.SetFocusToList();
// srcPanel.RefreshListCtrlSaveFocused();
}
class CThreadCombine: public CProgressThreadVirt
{
HRESULT ProcessVirt() Z7_override;
public:
FString InputDirPrefix;
FStringVector Names;
FString OutputPath;
UInt64 TotalSize;
};
HRESULT CThreadCombine::ProcessVirt()
{
NIO::COutFile outFile;
if (!outFile.Create_NEW(OutputPath))
{
const HRESULT res = GetLastError_noZero_HRESULT();
AddErrorPath(OutputPath);
return res;
}
CProgressSync &sync = Sync;
sync.Set_NumBytesTotal(TotalSize);
CMyBuffer bufferObject;
if (!bufferObject.Allocate(kBufSize))
return E_OUTOFMEMORY;
Byte *buffer = (Byte *)(void *)bufferObject;
UInt64 pos = 0;
FOR_VECTOR (i, Names)
{
NIO::CInFile inFile;
const FString nextName = InputDirPrefix + Names[i];
if (!inFile.Open(nextName))
{
const HRESULT res = GetLastError_noZero_HRESULT();
AddErrorPath(nextName);
return res;
}
sync.Set_FilePath(fs2us(nextName));
for (;;)
{
UInt32 processedSize;
if (!inFile.Read(buffer, kBufSize, processedSize))
{
const HRESULT res = GetLastError_noZero_HRESULT();
AddErrorPath(nextName);
return res;
}
if (processedSize == 0)
break;
const UInt32 needSize = processedSize;
if (!outFile.Write(buffer, needSize, processedSize))
{
const HRESULT res = GetLastError_noZero_HRESULT();
AddErrorPath(OutputPath);
return res;
}
if (needSize != processedSize)
throw g_Message_FileWriteError;
pos += processedSize;
RINOK(sync.Set_NumBytesCur(pos))
}
}
return S_OK;
}
extern void AddValuePair2(UString &s, UINT resourceID, UInt64 num, UInt64 size);
static void AddInfoFileName(UString &dest, const UString &name)
{
dest += "\n ";
dest += name;
}
void CApp::Combine()
{
const unsigned srcPanelIndex = GetFocusedPanelIndex();
CPanel &srcPanel = Panels[srcPanelIndex];
if (!srcPanel.IsFSFolder())
{
srcPanel.MessageBox_Error_LangID(IDS_OPERATION_IS_NOT_SUPPORTED);
return;
}
CRecordVector<UInt32> indices;
srcPanel.Get_ItemIndices_Operated(indices);
if (indices.IsEmpty())
return;
const unsigned index = indices[0];
if (indices.Size() != 1 || srcPanel.IsItem_Folder(index))
{
srcPanel.MessageBox_Error_LangID(IDS_COMBINE_SELECT_ONE_FILE);
return;
}
const UString itemName = srcPanel.GetItemName(index);
UString srcPath = srcPanel.GetFsPath() + srcPanel.GetItemPrefix(index);
UString path = srcPath;
unsigned destPanelIndex = (NumPanels <= 1) ? srcPanelIndex : (1 - srcPanelIndex);
CPanel &destPanel = Panels[destPanelIndex];
if (NumPanels > 1)
if (destPanel.IsFSFolder())
path = destPanel.GetFsPath();
CVolSeqName volSeqName;
if (!volSeqName.ParseName(itemName))
{
srcPanel.MessageBox_Error_LangID(IDS_COMBINE_CANT_DETECT_SPLIT_FILE);
return;
}
{
CThreadCombine combiner;
UString nextName = itemName;
combiner.TotalSize = 0;
for (;;)
{
NFind::CFileInfo fileInfo;
if (!fileInfo.Find(us2fs(srcPath + nextName)) || fileInfo.IsDir())
break;
combiner.Names.Add(us2fs(nextName));
combiner.TotalSize += fileInfo.Size;
nextName = volSeqName.GetNextName();
}
if (combiner.Names.Size() == 1)
{
srcPanel.MessageBox_Error_LangID(IDS_COMBINE_CANT_FIND_MORE_THAN_ONE_PART);
return;
}
if (combiner.TotalSize == 0)
{
srcPanel.MessageBox_Error(L"No data");
return;
}
UString info;
AddValuePair2(info, IDS_PROP_FILES, combiner.Names.Size(), combiner.TotalSize);
info.Add_LF();
info += srcPath;
unsigned i;
for (i = 0; i < combiner.Names.Size() && i < 2; i++)
AddInfoFileName(info, fs2us(combiner.Names[i]));
if (i != combiner.Names.Size())
{
if (i + 1 != combiner.Names.Size())
AddInfoFileName(info, L"...");
AddInfoFileName(info, fs2us(combiner.Names.Back()));
}
{
CCopyDialog copyDialog;
copyDialog.Value = path;
LangString(IDS_COMBINE, copyDialog.Title);
copyDialog.Title.Add_Space();
copyDialog.Title += srcPanel.GetItemRelPath(index);
LangString(IDS_COMBINE_TO, copyDialog.Static);
copyDialog.Info = info;
if (copyDialog.Create(srcPanel.GetParent()) != IDOK)
return;
path = copyDialog.Value;
}
NName::NormalizeDirPathPrefix(path);
if (!CreateComplexDir(us2fs(path)))
{
const HRESULT lastError = GetLastError_noZero_HRESULT();
srcPanel.MessageBox_Error_2Lines_Message_HRESULT(MyFormatNew(IDS_CANNOT_CREATE_FOLDER, path), lastError);
return;
}
UString outName = volSeqName.UnchangedPart;
while (!outName.IsEmpty())
{
if (outName.Back() != L'.')
break;
outName.DeleteBack();
}
if (outName.IsEmpty())
outName = "file";
NFind::CFileInfo fileInfo;
UString destFilePath = path + outName;
combiner.OutputPath = us2fs(destFilePath);
if (fileInfo.Find(combiner.OutputPath))
{
srcPanel.MessageBox_Error(MyFormatNew(IDS_FILE_EXIST, destFilePath));
return;
}
CProgressDialog &progressDialog = combiner;
progressDialog.ShowCompressionInfo = false;
const UString progressWindowTitle ("7-Zip"); // LangString(IDS_APP_TITLE, 0x03000000);
const UString title = LangString(IDS_COMBINING);
progressDialog.MainWindow = _window;
progressDialog.MainTitle = progressWindowTitle;
progressDialog.MainAddTitle = title;
progressDialog.MainAddTitle.Add_Space();
combiner.InputDirPrefix = us2fs(srcPath);
// CPanel::CDisableTimerProcessing disableTimerProcessing1(srcPanel);
// CPanel::CDisableTimerProcessing disableTimerProcessing2(destPanel);
if (combiner.Create(title, _window) != 0)
return;
}
RefreshTitleAlways();
// disableNotify.Restore();
// disableNotify.Restore();
// srcPanel.SetFocusToList();
// srcPanel.RefreshListCtrlSaveFocused();
}
|