ZipStorage.h
Go to the documentation of this file.
1 
2 // This source file is part of the ZipArchive library source distribution and
3 // is Copyrighted 2000 - 2013 by Artpol Software - Tadeusz Dracz
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // For the licensing details refer to the License.txt file.
11 //
12 // Web Site: http://www.artpol-software.com
14 
21 #if !defined(ZIPARCHIVE_ZIPSTORAGE_DOT_H)
22 #define ZIPARCHIVE_ZIPSTORAGE_DOT_H
23 
24 #if _MSC_VER > 1000
25  #pragma once
26  #if defined ZIP_HAS_DLL
27  #pragma warning (push)
28  #pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class
29  #endif
30 #endif
31 
32 #include "ZipFile.h"
33 #include "ZipAutoBuffer.h"
34 #include "ZipString.h"
35 #include "ZipMemFile.h"
36 #include "ZipExport.h"
37 #include "ZipCallback.h"
38 #include "BitFlag.h"
39 #include "ZipSplitNamesHandler.h"
40 #include "ZipException.h"
41 #include "ZipCollections.h"
42 
46 class ZIP_API CZipStorage
47 {
48  friend class CZipArchive;
49  friend class CZipCentralDir;
50 public:
54  enum State
55  {
56  stateOpened = 0x0001,
57  stateReadOnly = 0x0002,
58  stateAutoClose = 0x0004,
59  stateExisting = 0x0008,
60  stateSegmented = 0x0010,
61  stateSplit = stateSegmented | 0x0020,
62  stateBinarySplit = stateSplit | 0x0040,
63  stateSpan = stateSegmented | 0x0080
64  };
65 
72  enum SeekType
73  {
76 
80  seekCurrent
81  };
82  CZipStorage();
83  virtual ~CZipStorage();
84 
85  void Initialize();
90  void Open(CZipAbstractFile& af, int iMode, bool bAutoClose);
91 
97  void Open(LPCTSTR lpszPathName, int iMode, ZIP_SIZE_TYPE uVolumeSize);
98 
99 
112  CZipString GetSplitVolumeName(ZIP_VOLUME_TYPE uVolume, bool bLast)
113  {
114  if (m_pSplitNames == NULL)
115  {
117  }
119  if (IsExisting())
121  return m_pSplitNames->GetVolumeName(m_szArchiveName, (ZIP_VOLUME_TYPE)(uVolume + 1), flags);
122  }
123 
129  void FinalizeSegm();
130 
131 
138  void UpdateSegmMode(ZIP_VOLUME_TYPE uLastVolume);
139 
150  ZIP_SIZE_TYPE AssureFree(ZIP_SIZE_TYPE uNeeded);
151 
166  void Write(const void *pBuf, DWORD iSize, bool bAtOnce);
167 
174  ZIP_SIZE_TYPE GetOccupiedSpace() const
175  {
176  return ZIP_SIZE_TYPE(m_pFile->GetLength() + m_uBytesInWriteBuffer);
177  }
178 
182  bool IsClosed(bool bArchive) const
183  {
184  if (bArchive)
185  return !m_state.IsSetAny(stateOpened);
186  else
187  // assume not auto-close files always opened
188  return !m_pFile || (m_state.IsSetAny(stateAutoClose) && m_pFile->IsClosed());
189  }
190 
205  DWORD Read(void* pBuf, DWORD iSize, bool bAtOnce);
206 
217  ZIP_SIZE_TYPE GetPosition() const
218  {
219  ZIP_SIZE_TYPE uPos = (ZIP_SIZE_TYPE)(m_pFile->GetPosition()) + m_uBytesInWriteBuffer;
220  if (m_uCurrentVolume == 0)
221  uPos -= m_uBytesBeforeZip;
222  else if (IsBinarySplit()) // not for the first volume
223  {
224  ZIP_VOLUME_TYPE uVolume = m_uCurrentVolume;
225  ASSERT(m_pCachedSizes->GetSize() > (ZIP_ARRAY_SIZE_TYPE)(uVolume - 1));
226  do
227  {
228  uVolume--;
229  uPos += (ZIP_SIZE_TYPE)m_pCachedSizes->GetAt((ZIP_ARRAY_SIZE_TYPE)uVolume);
230  }
231  while (uVolume > 0);
232  }
233  return uPos;
234  }
235 
236 
241  void Flush();
242 
243 
247  void FlushFile()
248  {
249  if (!IsReadOnly())
250  m_pFile->Flush();
251  }
252 
253  void FlushBuffers()
254  {
255  Flush();
256  FlushFile();
257  }
258 
266  void NextVolume(ZIP_SIZE_TYPE uNeeded);
267 
268 
272  ZIP_VOLUME_TYPE GetCurrentVolume() const {return m_uCurrentVolume;}
273 
274 
281  void ChangeVolume(ZIP_VOLUME_TYPE uNumber);
282 
286  void ChangeVolume()
287  {
288  ChangeVolume((ZIP_VOLUME_TYPE)(m_uCurrentVolume + 1));
289  }
290 
294  void ChangeVolumeDec()
295  {
296  if (m_uCurrentVolume == 0)
298  ChangeVolume((ZIP_VOLUME_TYPE)(m_uCurrentVolume - 1));
299  }
300 
307  bool IsSplit() const
308  {
309  return m_state.IsSetAll(stateSplit);
310  }
311 
318  bool IsBinarySplit() const
319  {
320  return m_state.IsSetAll(stateBinarySplit);
321  }
322 
329  bool IsRegularSplit() const
330  {
331  return m_state.IsSetAll(stateSplit) && !m_state.IsSetAll(stateBinarySplit);
332  }
333 
340  bool IsSpanned() const
341  {
342  return m_state.IsSetAll(stateSpan);
343  }
344 
348  bool IsReadOnly() const
349  {
350  return m_state.IsSetAny(stateReadOnly) || IsExistingSegmented();
351  }
352 
359  bool IsExistingSegmented() const
360  {
361  return m_state.IsSetAll(stateSegmented | stateExisting);
362  }
363 
370  bool IsNewSegmented() const
371  {
372  return m_state.IsSetAny(stateSegmented) && !IsExisting();
373  }
374 
381  bool IsSegmented() const
382  {
383  return m_state.IsSetAny(stateSegmented);
384  }
385 
392  bool IsExisting() const
393  {
394  return m_state.IsSetAny(stateExisting);
395  }
396 
405  bool SetSplitNamesHandler(CZipSplitNamesHandler* pNames, bool bAutoDelete)
406  {
407  if (m_state != 0)
408  {
409  ZIPTRACE("%s(%i) : The archive is already opened.\n");
410  return false;
411  }
412  ClearSplitNames();
413  m_pSplitNames = pNames;
414  m_bAutoDeleteSplitNames = bAutoDelete;
415  return true;
416  }
417 
426  CZipSplitNamesHandler* GetSplitNamesHandler()
427  {
428  return m_pSplitNames;
429  }
430 
439  const CZipSplitNamesHandler* GetSplitNamesHandler() const
440  {
441  return m_pSplitNames;
442  }
443 
454  ULONGLONG Seek(ULONGLONG lOff, SeekType iSeekType = seekFromBeginning);
455 
466  void SeekInBinary(ZIP_FILE_SIZE lOff, bool bSeekToBegin = false);
467 
474  ZIP_SIZE_TYPE VolumeLeft() const;
475 
489  CZipString Close(bool bWrite, bool bGetLastVolumeName = false);
490 
494  CZipAbstractFile* m_pFile;
495 
499  static char m_gszExtHeaderSignat[];
500 
501  ZipArchiveLib::CBitFlag& GetState()
502  {
503  return m_state;
504  }
505 
506 protected:
507 
514  ZIP_SIZE_TYPE GetLastDataOffset()
515  {
516  return (ZIP_SIZE_TYPE)m_pFile->GetLength() - m_uBytesBeforeZip;
517  }
518 
532  ZIP_FILE_USIZE LocateSignature(char* szSignature, ZIP_SIZE_TYPE uMaxDepth);
533 
534 
538  void EmptyWriteBuffer()
539  {
540  m_uBytesInWriteBuffer = 0;
541  }
542 
558  bool OpenFile(LPCTSTR lpszName, UINT uFlags, bool bThrow = true);
559 
566  CZipString RenameLastFileInSplitArchive();
567 
578  void WriteInternalBuffer(const char *pBuf, DWORD uSize);
579 
586  ZIP_SIZE_TYPE GetFreeVolumeSpace() const;
587 
605  void CallCallback(ZIP_SIZE_TYPE uNeeded, int iCode, CZipString szTemp);
606 
610  CZipString ChangeSplitRead();
611 
615  CZipString ChangeSpannedRead();
616 
623  DWORD GetFreeInBuffer() const {return m_pWriteBuffer.GetSize() - m_uBytesInWriteBuffer;}
624 
632  ZIP_SIZE_TYPE m_uSplitData;
633 
638 
644  ZIP_SIZE_TYPE m_uCurrentVolSize;
645 
649  CZipAutoBuffer m_pWriteBuffer;
650 
655  ZIP_SIZE_TYPE m_uBytesWritten;
656 
661  ZIP_VOLUME_TYPE m_uCurrentVolume;
662 
668  ZIP_SIZE_TYPE m_uBytesBeforeZip;
669 
670 
678 
686 
695 
704 
705 private:
706  ZIP_FILE_USIZE LocateSignature(char* szSignature, ZIP_SIZE_TYPE uMaxDepth, int& leftToFind, bool& found, ZIP_FILE_USIZE uFileLength);
707 
708  CZipString GetSplitVolumeName(bool bLast)
709  {
710  return GetSplitVolumeName(m_uCurrentVolume, bLast);
711  }
712 
713  void ClearSplitNames()
714  {
715  if (m_pSplitNames)
716  {
717  if (m_bAutoDeleteSplitNames)
718  delete m_pSplitNames;
719  m_pSplitNames = NULL;
720  m_bAutoDeleteSplitNames = false;
721  }
722  }
723 
724  void ClearCachedSizes()
725  {
726  if (m_pCachedSizes)
727  {
728  delete m_pCachedSizes;
729  m_pCachedSizes = NULL;
730  }
731  }
732 
733  void EnsureSplitNames()
734  {
735  if (IsSplit())
736  {
737  if (m_pSplitNames == NULL)
738  {
739  m_bAutoDeleteSplitNames = true;
740  if (m_state.IsSetAll(stateBinarySplit))
741  m_pSplitNames = new CZipBinSplitNamesHandler();
742  else
743  m_pSplitNames = new CZipRegularSplitNamesHandler();
744  }
745  m_pSplitNames->Initialize(m_szArchiveName);
746  }
747  }
748 
749  ZIP_FILE_USIZE GetCachedSize(ZIP_VOLUME_TYPE uVolume)
750  {
751  ASSERT(m_pCachedSizes);
752  if (m_pCachedSizes->GetSize() > (ZIP_ARRAY_SIZE_TYPE)uVolume)
753  return m_pCachedSizes->GetAt((ZIP_ARRAY_SIZE_TYPE)uVolume);
755  // for a compiler
756  return 0;
757  }
758 
759  void CacheSizes();
760 
761  ZipArchiveLib::CBitFlag m_state;
762  CZipSegmCallback* m_pChangeVolumeFunc;
763  CZipString m_szArchiveName;
764  CZipFile m_internalfile;
765  CZipSplitNamesHandler* m_pSplitNames;
766  CZipArray<ZIP_FILE_USIZE>* m_pCachedSizes;
767  bool m_bAutoDeleteSplitNames;
768  static const ZIP_FILE_USIZE SignatureNotFound;
769  void ThrowError(int err) const;
770 };
771 
772 #if (_MSC_VER > 1000) && (defined ZIP_HAS_DLL)
773  #pragma warning (pop)
774 #endif
775 
776 
777 #endif // !defined(ZIPARCHIVE_ZIPSTORAGE_DOT_H)

The ZipArchive Library Copyright © 2000 - 2013 Artpol Software - Tadeusz Dracz. Generated at Fri Dec 13 2013 00:05:37.