ZipCompressor.h
Go to the documentation of this file.
1 
2 // This source file is part of the ZipArchive Library Open Source distribution
3 // and is Copyrighted 2000 - 2022 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: https://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  m_pFile = NULL;
64  }
65 
66 public:
71  {
72  typeDeflate = 1,
74  typePPMd
75  };
76 
81  {
82  levelDefault = -1,
83  levelStore = 0,
84  levelFastest = 1,
85  levelBest = 9
86  };
87 
92  {
93  methodStore = 0,
94  methodDeflate = 8,
95 
101  methodBzip2 = 12,
102 
112  methodWinZipAes = 99
113  };
114 
123  struct ZIP_API COptions
124  {
125 
130  {
134  cDefaultBufferSize = 2 * 65536
135  };
136 
137  COptions()
138  {
139  m_iBufferSize = cDefaultBufferSize;
140  }
141 
148  virtual int GetType() const = 0;
149 
156  virtual COptions* Clone() const = 0;
157 
167  virtual ~COptions()
168  {
169  }
170  };
171 
172 
179  class ZIP_API COptionsMap : public CZipMap<int, COptions*>
180  {
181  public:
182  void Set(const COptions* pOptions);
183  void Remove(int iType);
184  COptions* Get(int iType) const;
185  ~COptionsMap();
186  };
187 
197  static bool IsCompressionSupported(WORD uCompressionMethod)
198  {
199  return uCompressionMethod == methodStore || uCompressionMethod == methodDeflate
200  ;
201  }
202 
203  ZIP_SIZE_TYPE m_uUncomprLeft;
204  ZIP_SIZE_TYPE m_uComprLeft;
205  DWORD m_uCrc32;
206 
217  virtual bool CanProcess(WORD uMethod) = 0;
218 
219 
237  virtual void InitCompression(int iLevel, CZipFileHeader* pFile, CZipCryptograph* pCryptograph)
238  {
239  InitBuffer();
240  m_uComprLeft = 0;
241  m_pFile = pFile;
242  m_pCryptograph = pCryptograph;
243  }
244 
259  virtual void InitDecompression(CZipFileHeader* pFile, CZipCryptograph* pCryptograph)
260  {
261  InitBuffer();
262  m_pFile = pFile;
263  m_pCryptograph = pCryptograph;
264 
265  m_uComprLeft = m_pFile->GetDataSize(true);
266  m_uUncomprLeft = m_pFile->m_uUncomprSize;
267  m_uCrc32 = 0;
268  }
269 
284  virtual void Compress(const void *pBuffer, DWORD uSize) = 0;
285 
306  virtual DWORD Decompress(void *pBuffer, DWORD uSize) = 0;
307 
319  virtual void FinishCompression(bool bAfterException){}
320 
332  virtual void FinishDecompression(bool bAfterException){}
333 
347  virtual const COptions* GetOptions() const
348  {
349  return NULL;
350  }
351 
364  void UpdateOptions(const COptionsMap& optionsMap);
365 
366 
367  virtual ~CZipCompressor()
368  {
369  }
370 
380  static CZipCompressor* CreateCompressor(WORD uMethod, CZipStorage* pStorage);
381 
382 
383 protected:
390  virtual void UpdateOptions(const COptions* pOptions)
391  {
392  }
402  void UpdateFileCrc(const void *pBuffer, DWORD uSize);
403 
413  void UpdateCrc(const void *pBuffer, DWORD uSize);
414 
419  void FlushWriteBuffer()
420  {
421  WriteBuffer(m_pBuffer, (DWORD)m_uComprLeft);
422  m_uComprLeft = 0;
423  }
424 
434  void WriteBuffer(char* pBuffer, DWORD uSize)
435  {
436  if (uSize == 0)
437  return;
438  if (m_pCryptograph)
439  m_pCryptograph->Encode(pBuffer, uSize);
440  m_pStorage->Write(pBuffer, uSize, false);
441  }
442 
449  DWORD FillBuffer()
450  {
451  DWORD uToRead = m_pBuffer.GetSize();
452  if (m_uComprLeft < uToRead)
453  uToRead = (DWORD)m_uComprLeft;
454 
455  if (uToRead > 0)
456  {
457  m_pStorage->Read(m_pBuffer, uToRead, false);
458  if (m_pCryptograph)
459  m_pCryptograph->Decode(m_pBuffer, uToRead);
460  m_uComprLeft -= uToRead;
461  }
462  return uToRead;
463  }
464 
471  void InitBuffer();
472 
479  void ReleaseBuffer()
480  {
481  m_pBuffer.Release();
482  }
483 
493  virtual int ConvertInternalError(int iErr) const
494  {
495  return iErr;
496  }
497 
511  void ThrowError(int iErr, bool bInternal = false)
512  {
513  if (bInternal)
514  iErr = ConvertInternalError(iErr);
515  CZipException::Throw(iErr, m_pStorage->IsClosed(true) ? _T("") : (LPCTSTR)m_pStorage->m_pFile->GetFilePath());
516  }
517 };
518 
519 #if _MSC_VER > 1000
520  #pragma warning( pop )
521 #endif
522 
523 #endif

The ZipArchive Library Copyright © 2000 - 2022 Artpol Software - Tadeusz Dracz. Generated at Sat Dec 17 2022 19:57:03.