ZipCompressor.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_ZIPCOMPRESSOR_DOT_H)
22 #define ZIPARCHIVE_ZIPCOMPRESSOR_DOT_H
23 
24 #if _MSC_VER > 1000
25  #pragma once
26  #pragma warning( push )
27  #pragma warning (disable: 4100) // unreferenced formal parameter
28  #pragma warning (disable: 4275) // non dll-interface class 'CObject' used as base for dll-interface class 'CMap<KEY,ARG_KEY,VALUE,ARG_VALUE>'
29 #endif
30 
31 #include "ZipExport.h"
32 #include "ZipAutoBuffer.h"
33 #include "ZipFileHeader.h"
34 #include "ZipStorage.h"
35 #include "ZipCryptograph.h"
36 #include "ZipException.h"
37 
41 class ZIP_API CZipCompressor
42 {
43 protected:
45  CZipAutoBuffer m_pBuffer;
48 
49 
57  :m_pStorage(pStorage)
58  {
59  m_pCryptograph = NULL;
60  m_uUncomprLeft = 0;
61  m_uComprLeft = 0;
62  m_uCrc32 = 0;
63  }
64 
65 public:
70  {
71  typeDeflate = 1,
73  typePPMd
74  };
75 
80  {
81  levelDefault = -1,
82  levelStore = 0,
83  levelFastest = 1,
84  levelBest = 9
85  };
86 
91  {
92  methodStore = 0,
93  methodDeflate = 8,
94 
100  methodBzip2 = 12,
101 
111  methodWinZipAes = 99
112  };
113 
122  struct ZIP_API COptions
123  {
124 
129  {
133  cDefaultBufferSize = 2 * 65536
134  };
135 
136  COptions()
137  {
138  m_iBufferSize = cDefaultBufferSize;
139  }
140 
147  virtual int GetType() const = 0;
148 
155  virtual COptions* Clone() const = 0;
156 
166  virtual ~COptions()
167  {
168  }
169  };
170 
171 
178  class ZIP_API COptionsMap : public CZipMap<int, COptions*>
179  {
180  public:
181  void Set(const COptions* pOptions);
182  void Remove(int iType);
183  COptions* Get(int iType) const;
184  ~COptionsMap();
185  };
186 
196  static bool IsCompressionSupported(WORD uCompressionMethod)
197  {
198  return uCompressionMethod == methodStore || uCompressionMethod == methodDeflate
199  ;
200  }
201 
202  ZIP_SIZE_TYPE m_uUncomprLeft;
203  ZIP_SIZE_TYPE m_uComprLeft;
204  DWORD m_uCrc32;
205 
216  virtual bool CanProcess(WORD uMethod) = 0;
217 
218 
236  virtual void InitCompression(int iLevel, CZipFileHeader* pFile, CZipCryptograph* pCryptograph)
237  {
238  InitBuffer();
239  m_uComprLeft = 0;
240  m_pFile = pFile;
241  m_pCryptograph = pCryptograph;
242  }
243 
258  virtual void InitDecompression(CZipFileHeader* pFile, CZipCryptograph* pCryptograph)
259  {
260  InitBuffer();
261  m_pFile = pFile;
262  m_pCryptograph = pCryptograph;
263 
264  m_uComprLeft = m_pFile->GetDataSize(true);
265  m_uUncomprLeft = m_pFile->m_uUncomprSize;
266  m_uCrc32 = 0;
267  }
268 
283  virtual void Compress(const void *pBuffer, DWORD uSize) = 0;
284 
305  virtual DWORD Decompress(void *pBuffer, DWORD uSize) = 0;
306 
318  virtual void FinishCompression(bool bAfterException){}
319 
331  virtual void FinishDecompression(bool bAfterException){}
332 
346  virtual const COptions* GetOptions() const
347  {
348  return NULL;
349  }
350 
363  void UpdateOptions(const COptionsMap& optionsMap);
364 
365 
366  virtual ~CZipCompressor()
367  {
368  }
369 
379  static CZipCompressor* CreateCompressor(WORD uMethod, CZipStorage* pStorage);
380 
381 
382 protected:
389  virtual void UpdateOptions(const COptions* pOptions)
390  {
391  }
401  void UpdateFileCrc(const void *pBuffer, DWORD uSize);
402 
412  void UpdateCrc(const void *pBuffer, DWORD uSize);
413 
418  void FlushWriteBuffer()
419  {
420  WriteBuffer(m_pBuffer, (DWORD)m_uComprLeft);
421  m_uComprLeft = 0;
422  }
423 
433  void WriteBuffer(char* pBuffer, DWORD uSize)
434  {
435  if (uSize == 0)
436  return;
437  if (m_pCryptograph)
438  m_pCryptograph->Encode(pBuffer, uSize);
439  m_pStorage->Write(pBuffer, uSize, false);
440  }
441 
448  DWORD FillBuffer()
449  {
450  DWORD uToRead = m_pBuffer.GetSize();
451  if (m_uComprLeft < uToRead)
452  uToRead = (DWORD)m_uComprLeft;
453 
454  if (uToRead > 0)
455  {
456  m_pStorage->Read(m_pBuffer, uToRead, false);
457  if (m_pCryptograph)
458  m_pCryptograph->Decode(m_pBuffer, uToRead);
459  m_uComprLeft -= uToRead;
460  }
461  return uToRead;
462  }
463 
470  void InitBuffer();
471 
478  void ReleaseBuffer()
479  {
480  m_pBuffer.Release();
481  }
482 
492  virtual int ConvertInternalError(int iErr) const
493  {
494  return iErr;
495  }
496 
510  void ThrowError(int iErr, bool bInternal = false)
511  {
512  if (bInternal)
513  iErr = ConvertInternalError(iErr);
514  CZipException::Throw(iErr, m_pStorage->IsClosed(true) ? _T("") : (LPCTSTR)m_pStorage->m_pFile->GetFilePath());
515  }
516 };
517 
518 #if _MSC_VER > 1000
519  #pragma warning( pop )
520 #endif
521 
522 #endif

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