使用soui开发的mbc,只支持windows版本
w1146869587
2022-01-24 4905e2e7537d507f218e8e9595485e09d9f3a2b4
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2011 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////
 
#include "stdafx.h"
#include "ZipStorage.h"
#include "ZipArchive.h"
#include "ZipPlatform.h"
 
char CZipStorage::m_gszExtHeaderSignat[] = {0x50, 0x4b, 0x07, 0x08};
const ZIP_FILE_USIZE CZipStorage::SignatureNotFound = ZIP_FILE_USIZE(-1);
#define SIGNATURE_SIZE 4
 
using namespace ZipArchiveLib;
 
 
CZipStorage::CZipStorage()
{
    Initialize();
}
 
void CZipStorage::Initialize()
{    
    m_pSplitChangeVolumeFunc = m_pSpanChangeVolumeFunc = m_pChangeVolumeFunc = NULL;
    m_iWriteBufferSize = 65536;    
    m_pFile = NULL;    
    m_iLocateBufferSize = 32768;
    m_uBytesBeforeZip = 0;
    m_uCurrentVolume = ZIP_VOLUME_NUMBER_UNSPECIFIED;
    m_szArchiveName.Empty();
    m_pSplitNames = NULL;
    m_pCachedSizes = NULL;
    m_bAutoDeleteSplitNames = false;
    m_state = 0;
}
 
CZipStorage::~CZipStorage()
{
    ClearSplitNames();
    ClearCachedSizes();
}
 
DWORD CZipStorage::Read(void *pBuf, DWORD iSize, bool bAtOnce)
{
    if (iSize == 0)
        return 0;
    DWORD iRead;
    for(;;)
    {
        iRead = m_pFile->Read(pBuf, iSize);
        if (!iRead)
        {
            if (IsSegmented())
                ChangeVolume();
            else
                ThrowError(CZipException::badZipFile);
        }
        else
            break;
    }
 
    if (iRead == iSize)
        return iRead;
    else if ((bAtOnce && !IsBinarySplit()) || !IsSegmented())
        ThrowError(CZipException::badZipFile);
 
    while (iRead < iSize)
    {
        ChangeVolume();
        UINT iNewRead = m_pFile->Read((char*)pBuf + iRead, iSize - iRead);
        if (!iNewRead && iRead < iSize)
            ThrowError(CZipException::badZipFile);
        iRead += iNewRead;
    }
 
    return iRead;
}
 
void CZipStorage::Open(LPCTSTR lpszPathName, int iMode, ZIP_SIZE_TYPE uVolumeSize)
{
    m_uCurrentVolume = ZIP_VOLUME_NUMBER_UNSPECIFIED;
    m_pWriteBuffer.Allocate(m_iWriteBufferSize); 
    m_uBytesInWriteBuffer = 0;
    m_state.Set(stateOpened | stateAutoClose);
    m_pFile = &m_internalfile;
    m_szArchiveName = lpszPathName;
    m_pChangeVolumeFunc = NULL;
 
    CBitFlag mode(iMode);
 
    if (mode.IsSetAny(CZipArchive::zipCreate)) // create new archive
    {        
        m_uCurrentVolume = 0;
        if (!mode.IsSetAny(CZipArchive::zipModeSegmented))
        {
            OpenFile(lpszPathName, (mode.IsSetAll(CZipArchive::zipCreateAppend) ? CZipFile::modeNoTruncate : CZipFile::modeCreate) | CZipFile::modeReadWrite);
        }
        else // create a segmented archive
        {            
            m_uBytesWritten = 0;
            if (mode.IsSetAny(CZipArchive::zipModeSpan))
            {
                if (!m_pSpanChangeVolumeFunc)
                    ThrowError(CZipException::noCallback);
                if (!ZipPlatform::IsDriveRemovable(lpszPathName))
                    ThrowError(CZipException::nonRemovable);
                m_state.Set(stateSpan);
                m_pChangeVolumeFunc = m_pSpanChangeVolumeFunc;
            }
            else if (uVolumeSize <= 0)
                ThrowError(CZipException::noVolumeSize);
            else
            {
                m_uSplitData = uVolumeSize;
                if (mode.IsSetAll(CZipArchive::zipModeBinSplit))
                {
                    m_state.Set(stateBinarySplit);
                    ClearCachedSizes(); // just in case
                    m_pCachedSizes = new CZipArray<ZIP_FILE_USIZE>();
                }
                else
                    m_state.Set(stateSplit);
                EnsureSplitNames();
                m_pChangeVolumeFunc = m_pSplitChangeVolumeFunc;
            }
 
            NextVolume(4);
            Write(m_gszExtHeaderSignat, 4, true);
        }
    }
    else // open existing
    {
        m_state.Set(stateExisting);
        bool readOnly = mode.IsSetAll(CZipArchive::zipOpenReadOnly);
        if (readOnly)
            m_state.Set(stateReadOnly);
        OpenFile(lpszPathName, CZipFile::modeNoTruncate |
            (readOnly ? CZipFile::modeRead : CZipFile::modeReadWrite));
        // this will be revised in UpdateSegmMode
        if (mode.IsSetAny(CZipArchive::zipModeSpan))
            m_state.Set(stateSpan);
        else if (mode.IsSetAll(CZipArchive::zipModeBinSplit))
        {
            m_state.Set(stateBinarySplit);
            EnsureSplitNames();
            m_uCurrentVolume = m_pSplitNames->GetVolumeNumber(m_szArchiveName);
            if (m_uCurrentVolume == 0)
                ThrowError(CZipException::badZipFile);
            m_uCurrentVolume -= 1;
            if (m_uCurrentVolume > 0)
            {                
                m_uSplitData = m_uCurrentVolume;
                CacheSizes();
            }
            else
            {
                ClearSplitNames();
                m_state.Clear(stateBinarySplit);
            }
        }
        else if (mode.IsSetAny(CZipArchive::zipModeSplit))
            m_state.Set(stateSplit);
    }    
}
 
void CZipStorage::CacheSizes()
{
    ClearCachedSizes(); // just in case
    m_pCachedSizes = new CZipArray<ZIP_FILE_USIZE>();
    m_pCachedSizes->SetSize((ZIP_ARRAY_SIZE_TYPE)(m_uCurrentVolume + 1));
    ZIP_VOLUME_TYPE lastVolume = m_uCurrentVolume;
    for(;;)
    {
        m_pCachedSizes->SetAt((ZIP_ARRAY_SIZE_TYPE)m_uCurrentVolume, m_pFile->GetLength());
        if (m_uCurrentVolume == 0)
            break;
        ChangeVolume((ZIP_VOLUME_TYPE)(m_uCurrentVolume - 1));
    }
    ChangeVolume(lastVolume);
}
 
void CZipStorage::Open(CZipAbstractFile& af, int iMode, bool bAutoClose)
{
    m_pWriteBuffer.Allocate(m_iWriteBufferSize); 
    m_uBytesInWriteBuffer = 0;
    m_state.Set(stateOpened);
    if (bAutoClose)
        m_state.Set(stateAutoClose);
    m_pFile = &af;
 
    CBitFlag mode(iMode);
 
    if (mode.IsSetAny(CZipArchive::zipCreate))
    {        
        m_uCurrentVolume = 0;
        if (mode.IsSetAll(CZipArchive::zipCreateAppend))
            af.SeekToEnd();
        else
            af.SetLength(0);
    }
    else // open existing
    {
        m_state.Set(stateExisting);
        if (mode.IsSetAll(CZipArchive::zipOpenReadOnly))
            m_state.Set(stateReadOnly);
        af.SeekToBegin();
    }
}
 
 
void CZipStorage::ChangeVolume(ZIP_VOLUME_TYPE uNumber)
{
    if (uNumber == m_uCurrentVolume || !IsSegmented()) // the second condition may happen in some bad archives
        return;
 
    m_uCurrentVolume = uNumber;    
    OpenFile(IsSpanned() ? ChangeSpannedRead() : ChangeSplitRead(),
        CZipFile::modeNoTruncate | CZipFile::modeRead);
}
 
void CZipStorage::ThrowError(int err) const
{
    CZipException::Throw(err, m_pFile->GetFilePath());
}
 
bool CZipStorage::OpenFile(LPCTSTR lpszName, UINT uFlags, bool bThrow)
{
    return m_pFile->Open(lpszName, uFlags | CZipFile::shareDenyWrite, bThrow);
}
 
 
CZipString CZipStorage::ChangeSpannedRead()
{
    CZipString szTemp = m_pFile->GetFilePath();
    m_pFile->Close();
    CallCallback(0, CZipSegmCallback::scVolumeNeededForRead, szTemp);
    return m_pChangeVolumeFunc->m_szExternalFile;
}
 
CZipString CZipStorage::ChangeSplitRead()
{
    bool lastPart = (ZIP_SIZE_TYPE)m_uCurrentVolume == m_uSplitData;
    CZipString szVolumeName = GetSplitVolumeName(lastPart);
    if (m_pChangeVolumeFunc)
    {
        int iCode = CZipSegmCallback::scVolumeNeededForRead;
        for(;;)
        {
            CallCallback(lastPart ? ZIP_SPLIT_LAST_VOLUME : 0, iCode, szVolumeName);
            if (ZipPlatform::FileExists(m_pChangeVolumeFunc->m_szExternalFile))
            {
                szVolumeName = m_pChangeVolumeFunc->m_szExternalFile;
                break;
            }
            else
                iCode = CZipSegmCallback::scFileNotFound;
        }
    }
    m_pFile->Close();
    return szVolumeName;
}
 
CZipString CZipStorage::RenameLastFileInSplitArchive()
{
    ASSERT(IsSplit());
 
    // give to the last volume the zip extension
    CZipString szFileName = m_pFile->GetFilePath();
    CZipString szNewFileName = GetSplitVolumeName(true);
    if (m_pChangeVolumeFunc)
    {
        int code = CZipSegmCallback::scVolumeNeededForWrite;
        for(;;)
        {
            CallCallback(ZIP_SPLIT_LAST_VOLUME, code, szNewFileName);
            szNewFileName = m_pChangeVolumeFunc->m_szExternalFile;
            if (ZipPlatform::FileExists(szNewFileName))
                code = CZipSegmCallback::scFileNameDuplicated;
            else
                break;
        }
    }
 
    m_pFile->Flush();
    m_pFile->Close();
 
    ZIPSTRINGCOMPARE pCompare = GetCZipStrCompFunc(ZipPlatform::GetSystemCaseSensitivity());
    if ((szFileName.*(pCompare))(szNewFileName) == 0)
    {
        return szNewFileName;
    }
 
    if (!m_pChangeVolumeFunc && ZipPlatform::FileExists(szNewFileName))
    {
        ZipPlatform::RemoveFile(szNewFileName);
    }
    ZipPlatform::RenameFile(szFileName, szNewFileName);
    return szNewFileName;
}
 
CZipString CZipStorage::Close(bool bWrite, bool bGetLastVolumeName)
{
    bool bClose = true;
    CZipString sz;
    if (bWrite)
    {
        Flush();
        if (IsSplit() && !IsExisting())
        {
            sz = RenameLastFileInSplitArchive();
            bClose = false;// already closed in RenameLastFileInSplitArchive
        }
    }
 
    if (bGetLastVolumeName && sz.IsEmpty())
    {
        if (IsSplit() && IsExisting())
            sz = m_pSplitNames->GetVolumeName(m_szArchiveName, (ZIP_VOLUME_TYPE)(m_uSplitData + 1), CZipSplitNamesHandler::flLast | CZipSplitNamesHandler::flExisting);
        else
            sz = m_pFile->GetFilePath();
    }
    
    if (bClose)
    {
        if (bWrite)
            FlushFile();
        if (m_state.IsSetAny(stateAutoClose))
            m_pFile->Close();
    }
 
    m_pWriteBuffer.Release();
    m_uCurrentVolume = ZIP_VOLUME_NUMBER_UNSPECIFIED;
    m_state = 0;
    m_pFile = NULL;
    m_uBytesBeforeZip = 0;
    ClearSplitNames();
    ClearCachedSizes();
    return sz;
}
 
void CZipStorage::NextVolume(ZIP_SIZE_TYPE uNeeded)
{
    Flush();
    ASSERT(IsSegmented());
    bool bSpan = IsSpanned();
    if (m_uBytesWritten)
    {
        m_uBytesWritten = 0;
        m_uCurrentVolume++;
        ZIP_VOLUME_TYPE uMaxVolumes = (ZIP_VOLUME_TYPE)(bSpan ? 999 : 0xFFFF);
        if (m_uCurrentVolume >= uMaxVolumes) // m_uCurrentVolume is a zero-based index
            ThrowError(CZipException::tooManyVolumes);
    } 
 
    CZipString szFileName;
    
    if (bSpan)
        szFileName  = m_szArchiveName;
    else
        szFileName =  GetSplitVolumeName(false);
 
    if (!m_pFile->IsClosed())
    {
        m_pFile->Flush();
        if (IsBinarySplit())
            m_pCachedSizes->Add(m_pFile->GetLength());
        m_pFile->Close();
    }    
 
    if (m_pChangeVolumeFunc)
    {
        int iCode = CZipSegmCallback::scVolumeNeededForWrite;
        for(;;)
        {
            CallCallback(uNeeded, iCode, szFileName);
            // allow changing of the filename
            szFileName = m_pChangeVolumeFunc->m_szExternalFile;
 
            if (ZipPlatform::FileExists(szFileName))
                iCode = CZipSegmCallback::scFileNameDuplicated;
            else
            {
                if (bSpan)
                {
                    CZipString label;
                    label.Format(_T("pkback# %.3d"), m_uCurrentVolume + 1);
                    if (!ZipPlatform::SetVolLabel(szFileName, label))
                    {
                        iCode = CZipSegmCallback::scCannotSetVolLabel;
                        continue;
                    }
                }                    
                
                if (OpenFile(szFileName, CZipFile::modeCreate | CZipFile::modeReadWrite, false))
                    break;
                else
                    iCode = CZipSegmCallback::scFileCreationFailure;
            }
 
        }
        m_uCurrentVolSize = bSpan ? GetFreeVolumeSpace() : m_uSplitData;
    }
    else
    {
        if (bSpan)
            ThrowError(CZipException::internalError);
        m_uCurrentVolSize = m_uSplitData;
        OpenFile(szFileName, CZipFile::modeCreate | CZipFile::modeReadWrite);
    }
}
 
void CZipStorage::CallCallback(ZIP_SIZE_TYPE uNeeded, int iCode, CZipString szTemp)
{
    if (!m_pChangeVolumeFunc)
        ThrowError(CZipException::internalError);
    m_pChangeVolumeFunc->m_szExternalFile = szTemp;
    m_pChangeVolumeFunc->m_uVolumeNeeded = (ZIP_VOLUME_TYPE)(m_uCurrentVolume + 1);
    m_pChangeVolumeFunc->m_iCode = iCode; 
    if (!m_pChangeVolumeFunc->Callback(uNeeded))
        CZipException::Throw(CZipException::aborted, szTemp);
}
 
ZIP_SIZE_TYPE CZipStorage::GetFreeVolumeSpace() const
{
    ASSERT (IsSpanned());
    CZipString szTemp = m_pFile->GetFilePath();
    if (szTemp.IsEmpty()) // called once when creating a segmented archive
        return 0;
    else
    {
        CZipPathComponent zpc(szTemp);
        ULONGLONG ret = ZipPlatform::GetDeviceFreeSpace(zpc.GetFilePath());
        if (ret > (ZIP_SIZE_TYPE)(-1))
            return (ZIP_SIZE_TYPE)(-1);
        else
            return (ZIP_SIZE_TYPE)ret;
    }
}
 
 
void CZipStorage::UpdateSegmMode(ZIP_VOLUME_TYPE uLastDisk)
{
    bool binarySplit = IsBinarySplit();
    if (!binarySplit)
        m_uCurrentVolume = uLastDisk;
 
    if (uLastDisk > 0 || binarySplit)
    {
        // segmentation detected
        CZipString szFilePath = m_pFile->GetFilePath();
        if (!m_state.IsSetAny(stateSegmented))
            m_state.Set(ZipPlatform::IsDriveRemovable(szFilePath) ? stateSpan : stateSplit);
        
        if (IsSpanned())
        {
            if (!m_pSpanChangeVolumeFunc)
                ThrowError(CZipException::noCallback);
            m_pChangeVolumeFunc = m_pSpanChangeVolumeFunc;
        }
        else /*if (IsSplit())*/
        {
            EnsureSplitNames();
            if (!binarySplit)
                m_uSplitData = uLastDisk; // the last volume
            m_pChangeVolumeFunc = m_pSplitChangeVolumeFunc;
        }
        m_pWriteBuffer.Release(); // no need for this in this case
    }
    else
        // it will clear all segmented flags
        m_state.Clear(stateSpan | stateBinarySplit);
}
 
ZIP_SIZE_TYPE CZipStorage::AssureFree(ZIP_SIZE_TYPE uNeeded)
{
    ZIP_SIZE_TYPE uFree;
    while ((uFree = VolumeLeft()) < uNeeded)
    {
        if (IsSplit() && !m_uBytesWritten && !m_uBytesInWriteBuffer)
            // in the splitArchive mode, if the size of the archive is less 
            // than the size of the packet to be written at once,
            // increase once the size of the volume
            m_uCurrentVolSize = uNeeded;
        else
            NextVolume(uNeeded);
    }
    return uFree;
}
 
void CZipStorage::Write(const void *pBuf, DWORD iSize, bool bAtOnce)
{
    if (!IsSegmented())
        WriteInternalBuffer((char*)pBuf, iSize);
    else
    {
        bool atOnce = bAtOnce && !IsBinarySplit();
        // if not at once, one byte is enough of free space
        DWORD iNeeded = atOnce ? iSize : 1; 
        DWORD uTotal = 0;
 
        while (uTotal < iSize)
        {            
            ZIP_SIZE_TYPE uFree = AssureFree(iNeeded);
            DWORD uLeftToWrite = iSize - uTotal;
            DWORD uToWrite = uFree < uLeftToWrite ? (DWORD)uFree : uLeftToWrite;
            WriteInternalBuffer((char*)pBuf + uTotal, uToWrite);
            if (atOnce)
                return;
            else
                uTotal += uToWrite;
        }
 
    }
}
 
 
void CZipStorage::WriteInternalBuffer(const char *pBuf, DWORD uSize)
{
    DWORD uWritten = 0;
    while (uWritten < uSize)
    {
        DWORD uFreeInBuffer = GetFreeInBuffer();
        if (uFreeInBuffer == 0)
        {
            Flush();
            uFreeInBuffer = m_pWriteBuffer.GetSize();
        }
        DWORD uLeftToWrite = uSize - uWritten;
        DWORD uToCopy = uLeftToWrite < uFreeInBuffer ? uLeftToWrite : uFreeInBuffer;
        memcpy((char*)m_pWriteBuffer + m_uBytesInWriteBuffer, pBuf + uWritten, uToCopy);
        uWritten += uToCopy;
        m_uBytesInWriteBuffer += uToCopy;
    }
}
 
ZIP_SIZE_TYPE CZipStorage::VolumeLeft() const
{
    // for spanned archives m_uCurrentVolSize is updated after each flush()
    ZIP_SIZE_TYPE uBytes = m_uBytesInWriteBuffer + (IsSpanned() ? 0 : m_uBytesWritten);    
    return uBytes > m_uCurrentVolSize ? 0 : m_uCurrentVolSize - uBytes;
}
 
void CZipStorage::Flush()
{    
    if (m_uBytesInWriteBuffer)
    {
        m_pFile->Write(m_pWriteBuffer, m_uBytesInWriteBuffer);
        if (IsSegmented())
            m_uBytesWritten += m_uBytesInWriteBuffer;
        m_uBytesInWriteBuffer = 0;
    }
    if (IsSpanned()) 
        // after writing it is difficult to predict the free space due to 
        // not completely written clusters, write operation may start from a new cluster
        m_uCurrentVolSize = GetFreeVolumeSpace();    
}
 
ZIP_FILE_USIZE CZipStorage::LocateSignature(char* szSignature, ZIP_SIZE_TYPE uMaxDepth)
{
    m_pFile->SeekToEnd();
    int leftToFind = SIGNATURE_SIZE - 1;
    bool found = false; // for fast checking if leftToFind needs resetting    
    if (!IsBinarySplit())
    {
        return LocateSignature(szSignature, uMaxDepth, leftToFind, found, m_pFile->GetLength());
    }
    else
    {        
        for(;;)
        {
            ZIP_FILE_USIZE uFileLength = GetCachedSize(m_uCurrentVolume);
            ZIP_FILE_USIZE position = LocateSignature(szSignature, uMaxDepth, leftToFind, found, uFileLength);
            if (position != SignatureNotFound || uMaxDepth <= uFileLength)
                return position;
            uMaxDepth -= (ZIP_SIZE_TYPE)uFileLength;
            if (m_uCurrentVolume == 0)
                return SignatureNotFound;
            ChangeVolumeDec();
            m_pFile->SeekToEnd();
        }
    }
}
 
ZIP_FILE_USIZE CZipStorage::LocateSignature(char* szSignature, ZIP_SIZE_TYPE uMaxDepth, int& leftToFind, bool& found, ZIP_FILE_USIZE uFileLength)
{
    CZipAutoBuffer buffer(m_iLocateBufferSize);
    
    ZIP_SIZE_TYPE max = (ZIP_SIZE_TYPE)(uFileLength < uMaxDepth ? uFileLength : uMaxDepth);
    ZIP_SIZE_TYPE position = (ZIP_SIZE_TYPE)(uFileLength - m_pFile->GetPosition());
    int offset = 0;
    
    int toRead = m_iLocateBufferSize;
    
    while ( position < max )
    {
        position += toRead;
        if ( position > max )
        {
            int diff = (int) ( position - max );
            toRead -= diff;
            offset = diff;
            position = max;
        }
        Seek(position, seekFromEnd);    
        int actuallyRead = m_pFile->Read((char*)buffer + offset, toRead);
        if (actuallyRead != toRead)
            ThrowError(CZipException::badZipFile);
        int pos = m_iLocateBufferSize - 1;
        while ( pos >= offset )
        {
            if ( buffer[pos] == szSignature[leftToFind] )
            {
                if ( leftToFind == 0 )
                    return (ZIP_FILE_USIZE)(uFileLength - ( position - ( pos - offset ) ));
                if ( !found )
                    found = true;
                leftToFind--;
                pos--;
            }
            else if ( found )
            {
                leftToFind = SIGNATURE_SIZE - 1;
                found = false;
                // do not decrease position, the current pos may be the first to find
            }
            else
                pos--;
        }
    }
    return SignatureNotFound;    
}
 
void CZipStorage::SeekInBinary(ZIP_FILE_SIZE lOff, bool bSeekToBegin)
{
    ASSERT(IsBinarySplit());
    if (bSeekToBegin)
        m_pFile->SeekToBegin();
 
    if (lOff == 0)
        return;
 
    if (lOff > 0)
    {
        ZIP_SIZE_TYPE uPosition = (ZIP_SIZE_TYPE)m_pFile->GetPosition();
        ZIP_FILE_USIZE uLength = GetCachedSize(m_uCurrentVolume);
        if ((ZIP_FILE_USIZE)(uPosition + lOff) < uLength)
        {
            m_pFile->Seek(lOff, CZipAbstractFile::current);
            return;
        }
        ZIP_VOLUME_TYPE uVolume = (ZIP_VOLUME_TYPE)(m_uCurrentVolume + 1);
        lOff -= uLength - uPosition;
        for(;;)
        {
            uLength = GetCachedSize(uVolume);
            if ((ZIP_FILE_USIZE)lOff < uLength)
            {
                ChangeVolume(uVolume);
                if (lOff > 0)
                {                    
                    m_pFile->Seek(lOff, CZipAbstractFile::current);
                }
                return;
            }
            else
                lOff -= uLength;
            uVolume++;
        }
    }
    else
    {
        ZIP_SIZE_TYPE uPosition = (ZIP_SIZE_TYPE)m_pFile->GetPosition();
        if (uPosition >= (ZIP_SIZE_TYPE)(-lOff))
        {
            m_pFile->Seek(lOff, CZipAbstractFile::current);
            return;
        }
        lOff += uPosition;
        ZIP_VOLUME_TYPE uVolume = (ZIP_VOLUME_TYPE)(m_uCurrentVolume - 1);
        for(;;)
        {
            ZIP_FILE_USIZE uLength = GetCachedSize(uVolume);
            if (uLength >= (ZIP_SIZE_TYPE)(-lOff))
            {
                ChangeVolume(uVolume);
                if (lOff < 0)
                {                    
                    m_pFile->Seek(lOff, CZipAbstractFile::end);
                }
                return;
            }
            else
                lOff += uLength;
            if (uVolume == 0)
                ThrowError(CZipException::genericError);
            uVolume--;
        }
    }
}
 
ULONGLONG CZipStorage::Seek(ULONGLONG lOff, SeekType iSeekType)
{    
    if (iSeekType == seekCurrent)
    {        
        if (IsExistingSegmented())
        {
            ZIP_SIZE_TYPE uPosition = (ZIP_SIZE_TYPE)m_pFile->GetPosition();
            ZIP_FILE_USIZE uLength = m_pFile->GetLength();
            while (uPosition + lOff >= uLength)
            {
                ZIP_SIZE_TYPE uCanSeek = (ZIP_SIZE_TYPE)(uLength - uPosition);
                lOff -= uCanSeek;
                ChangeVolume();
                uPosition = 0;
                uLength = m_pFile->GetLength();
            }
            return lOff > 0 ? m_pFile->SafeSeek((ZIP_FILE_USIZE)lOff) : 0;
        }
        else
            return m_pFile->Seek((ZIP_FILE_SIZE)lOff, CZipAbstractFile::current);            
    }
    else
    {
        if (m_uCurrentVolume == 0 && m_uBytesBeforeZip > 0)
            lOff += m_uBytesBeforeZip;
        return m_pFile->SafeSeek((ZIP_FILE_USIZE)lOff, iSeekType == seekFromBeginning);
    }
}
 
void CZipStorage::FinalizeSegm()
{
    ASSERT(IsNewSegmented()); // spanned archive in creation
 
    CZipString szFileName;
    if (IsSplit())
    {
        // the file is already closed
        szFileName = RenameLastFileInSplitArchive();
        if (IsBinarySplit() && m_uCurrentVolume != 0)
        {
            ZIP_SIZE_TYPE size;
            ZipPlatform::GetFileSize(szFileName, size);
            m_pCachedSizes->Add(ZIP_FILE_USIZE(size));
        }
    }
    else
    {
        szFileName = m_pFile->GetFilePath();
        m_pFile->Close();
    }
    m_state.Set(stateExisting);
    if (m_uCurrentVolume == 0) // one-volume segmented archive was converted to a normal archive
    {
        if (IsSplit())
        {
            ClearSplitNames();
            if (IsBinarySplit())
                ClearCachedSizes();
        }
        m_state.Clear(stateSplit | stateBinarySplit | stateSpan);
    }
    else
    {
        m_uSplitData = m_uCurrentVolume;
        if (IsSplit())
        {
            m_szArchiveName = szFileName;
        }
    }
 
    OpenFile(szFileName, CZipFile::modeNoTruncate | (IsSegmented() ? CZipFile::modeReadWrite : CZipFile::modeRead));    
}