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 
105  void FinalizeSegm();
106 
107 
114  void UpdateSegmMode(ZIP_VOLUME_TYPE uLastVolume);
115 
126  ZIP_SIZE_TYPE AssureFree(ZIP_SIZE_TYPE uNeeded);
127 
142  void Write(const void *pBuf, DWORD iSize, bool bAtOnce);
143 
150  ZIP_SIZE_TYPE GetOccupiedSpace() const
151  {
152  return ZIP_SIZE_TYPE(m_pFile->GetLength() + m_uBytesInWriteBuffer);
153  }
154 
158  bool IsClosed(bool bArchive) const
159  {
160  if (bArchive)
161  return !m_state.IsSetAny(stateOpened);
162  else
163  // assume not auto-close files always opened
164  return !m_pFile || (m_state.IsSetAny(stateAutoClose) && m_pFile->IsClosed());
165  }
166 
181  DWORD Read(void* pBuf, DWORD iSize, bool bAtOnce);
182 
193  ZIP_SIZE_TYPE GetPosition() const
194  {
195  ZIP_SIZE_TYPE uPos = (ZIP_SIZE_TYPE)(m_pFile->GetPosition()) + m_uBytesInWriteBuffer;
196  if (m_uCurrentVolume == 0)
197  uPos -= m_uBytesBeforeZip;
198  else if (IsBinarySplit()) // not for the first volume
199  {
200  ZIP_VOLUME_TYPE uVolume = m_uCurrentVolume;
201  ASSERT(m_pCachedSizes->GetSize() > (ZIP_ARRAY_SIZE_TYPE)(uVolume - 1));
202  do
203  {
204  uVolume--;
205  uPos += (ZIP_SIZE_TYPE)m_pCachedSizes->GetAt((ZIP_ARRAY_SIZE_TYPE)uVolume);
206  }
207  while (uVolume > 0);
208  }
209  return uPos;
210  }
211 
212 
217  void Flush();
218 
219 
223  void FlushFile()
224  {
225  if (!IsReadOnly())
226  m_pFile->Flush();
227  }
228 
229  void FlushBuffers()
230  {
231  Flush();
232  FlushFile();
233  }
234 
242  void NextVolume(ZIP_SIZE_TYPE uNeeded);
243 
244 
248  ZIP_VOLUME_TYPE GetCurrentVolume() const {return m_uCurrentVolume;}
249 
250 
257  void ChangeVolume(ZIP_VOLUME_TYPE uNumber);
258 
262  void ChangeVolume()
263  {
264  ChangeVolume((ZIP_VOLUME_TYPE)(m_uCurrentVolume + 1));
265  }
266 
270  void ChangeVolumeDec()
271  {
272  if (m_uCurrentVolume == 0)
274  ChangeVolume((ZIP_VOLUME_TYPE)(m_uCurrentVolume - 1));
275  }
276 
283  bool IsSplit() const
284  {
285  return m_state.IsSetAll(stateSplit);
286  }
287 
294  bool IsBinarySplit() const
295  {
296  return m_state.IsSetAll(stateBinarySplit);
297  }
298 
305  bool IsRegularSplit() const
306  {
307  return m_state.IsSetAll(stateSplit) && !m_state.IsSetAll(stateBinarySplit);
308  }
309 
316  bool IsSpanned() const
317  {
318  return m_state.IsSetAll(stateSpan);
319  }
320 
324  bool IsReadOnly() const
325  {
326  return m_state.IsSetAny(stateReadOnly) || IsExistingSegmented();
327  }
328 
335  bool IsExistingSegmented() const
336  {
337  return m_state.IsSetAll(stateSegmented | stateExisting);
338  }
339 
346  bool IsNewSegmented() const
347  {
348  return m_state.IsSetAny(stateSegmented) && !IsExisting();
349  }
350 
357  bool IsSegmented() const
358  {
359  return m_state.IsSetAny(stateSegmented);
360  }
361 
368  bool IsExisting() const
369  {
370  return m_state.IsSetAny(stateExisting);
371  }
372 
381  bool SetSplitNamesHandler(CZipSplitNamesHandler* pNames, bool bAutoDelete)
382  {
383  if (m_state != 0)
384  {
385  ZIPTRACE("%s(%i) : The archive is already opened.\n");
386  return false;
387  }
388  ClearSplitNames();
389  m_pSplitNames = pNames;
390  m_bAutoDeleteSplitNames = bAutoDelete;
391  return true;
392  }
393 
402  CZipSplitNamesHandler* GetSplitNamesHandler()
403  {
404  return m_pSplitNames;
405  }
406 
415  const CZipSplitNamesHandler* GetSplitNamesHandler() const
416  {
417  return m_pSplitNames;
418  }
419 
430  ULONGLONG Seek(ULONGLONG lOff, SeekType iSeekType = seekFromBeginning);
431 
442  void SeekInBinary(ZIP_FILE_SIZE lOff, bool bSeekToBegin = false);
443 
450  ZIP_SIZE_TYPE VolumeLeft() const;
451 
465  CZipString Close(bool bWrite, bool bGetLastVolumeName = false);
466 
470  CZipAbstractFile* m_pFile;
471 
475  static char m_gszExtHeaderSignat[];
476 
477  ZipArchiveLib::CBitFlag& GetState()
478  {
479  return m_state;
480  }
481 
482 protected:
483 
490  ZIP_SIZE_TYPE GetLastDataOffset()
491  {
492  return (ZIP_SIZE_TYPE)m_pFile->GetLength() - m_uBytesBeforeZip;
493  }
494 
508  ZIP_FILE_USIZE LocateSignature(char* szSignature, ZIP_SIZE_TYPE uMaxDepth);
509 
510 
514  void EmptyWriteBuffer()
515  {
516  m_uBytesInWriteBuffer = 0;
517  }
518 
534  bool OpenFile(LPCTSTR lpszName, UINT uFlags, bool bThrow = true);
535 
542  CZipString RenameLastFileInSplitArchive();
543 
554  void WriteInternalBuffer(const char *pBuf, DWORD uSize);
555 
562  ZIP_SIZE_TYPE GetFreeVolumeSpace() const;
563 
581  void CallCallback(ZIP_SIZE_TYPE uNeeded, int iCode, CZipString szTemp);
582 
586  CZipString ChangeSplitRead();
587 
591  CZipString ChangeSpannedRead();
592 
599  DWORD GetFreeInBuffer() const {return m_pWriteBuffer.GetSize() - m_uBytesInWriteBuffer;}
600 
608  ZIP_SIZE_TYPE m_uSplitData;
609 
614 
620  ZIP_SIZE_TYPE m_uCurrentVolSize;
621 
625  CZipAutoBuffer m_pWriteBuffer;
626 
631  ZIP_SIZE_TYPE m_uBytesWritten;
632 
637  ZIP_VOLUME_TYPE m_uCurrentVolume;
638 
644  ZIP_SIZE_TYPE m_uBytesBeforeZip;
645 
646 
654 
662 
671 
680 
681 private:
682  ZIP_FILE_USIZE LocateSignature(char* szSignature, ZIP_SIZE_TYPE uMaxDepth, int& leftToFind, bool& found, ZIP_FILE_USIZE uFileLength);
683  CZipString GetSplitVolumeName(bool bLast)
684  {
685  if (m_pSplitNames == NULL)
686  {
688  }
690  if (IsExisting())
692  return m_pSplitNames->GetVolumeName(m_szArchiveName, (ZIP_VOLUME_TYPE)(m_uCurrentVolume + 1), flags);
693  }
694 
695  void ClearSplitNames()
696  {
697  if (m_pSplitNames)
698  {
699  if (m_bAutoDeleteSplitNames)
700  delete m_pSplitNames;
701  m_pSplitNames = NULL;
702  m_bAutoDeleteSplitNames = false;
703  }
704  }
705 
706  void ClearCachedSizes()
707  {
708  if (m_pCachedSizes)
709  {
710  delete m_pCachedSizes;
711  m_pCachedSizes = NULL;
712  }
713  }
714 
715  void EnsureSplitNames()
716  {
717  if (IsSplit())
718  {
719  if (m_pSplitNames == NULL)
720  {
721  m_bAutoDeleteSplitNames = true;
722  if (m_state.IsSetAll(stateBinarySplit))
723  m_pSplitNames = new CZipBinSplitNamesHandler();
724  else
725  m_pSplitNames = new CZipRegularSplitNamesHandler();
726  }
727  m_pSplitNames->Initialize(m_szArchiveName);
728  }
729  }
730 
731  ZIP_FILE_USIZE GetCachedSize(ZIP_VOLUME_TYPE uVolume)
732  {
733  ASSERT(m_pCachedSizes);
734  if (m_pCachedSizes->GetSize() > (ZIP_ARRAY_SIZE_TYPE)uVolume)
735  return m_pCachedSizes->GetAt((ZIP_ARRAY_SIZE_TYPE)uVolume);
737  // for a compiler
738  return 0;
739  }
740 
741  void CacheSizes();
742 
743  ZipArchiveLib::CBitFlag m_state;
744  CZipSegmCallback* m_pChangeVolumeFunc;
745  CZipString m_szArchiveName;
746  CZipFile m_internalfile;
747  CZipSplitNamesHandler* m_pSplitNames;
748  CZipArray<ZIP_FILE_USIZE>* m_pCachedSizes;
749  bool m_bAutoDeleteSplitNames;
750  static const ZIP_FILE_USIZE SignatureNotFound;
751  void ThrowError(int err) const;
752 };
753 
754 #if (_MSC_VER > 1000) && (defined ZIP_HAS_DLL)
755  #pragma warning (pop)
756 #endif
757 
758 
759 #endif // !defined(ZIPARCHIVE_ZIPSTORAGE_DOT_H)

The ZipArchive Library Copyright © 2000 - 2013 Artpol Software - Tadeusz Dracz. Generated at Mon Feb 25 2013 16:29:21.