Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ZippedFile.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef ZippedFile_INCLUDE_ONCE
33 #define ZippedFile_INCLUDE_ONCE
34 
35 #include <vlCore/VirtualFile.hpp>
36 struct z_stream_s;
37 
38 namespace vl
39 {
40 //---------------------------------------------------------------------------
41 // ZippedFileInfo
42 //---------------------------------------------------------------------------
46  class ZippedFileInfo: public Object
47  {
48  friend class ZippedFile;
49  friend class ZippedDirectory;
50 
52 
53  public:
55  {
56  mVersionNeeded = 0;
59  mCRC32 = 0;
60  mCompressedSize = 0;
62  mFileNameLength = 0;
64  mSecond = 0;
65  mMinute = 0;
66  mHour = 0;
67  mDay = 0;
68  mMonth = 0;
69  mYear = 0;
71  }
72 
73  public:
74  unsigned short versionNeeded() const { return mVersionNeeded; }
75  unsigned short generalPurposeFlag() const { return mGeneralPurposeFlag; }
76  unsigned short compressionMethod() const { return mCompressionMethod; }
77  unsigned int crc32() const { return mCRC32; }
78  unsigned int compressedSize() const { return mCompressedSize; }
79  unsigned int uncompressedSize() const { return mUncompressedSize; }
80  unsigned short fileNameLength() const { return mFileNameLength; }
81  unsigned short extraFieldLength() const { return mExtraFieldLength; }
82  int second() const { return mSecond; }
83  int minute() const { return mMinute; }
84  int hour() const { return mHour; }
85  int day() const { return mDay; }
86  int month() const { return mMonth; }
87  int year() const { return mYear; }
88  const String& path() const { return mFileName; }
89  // offset of the compressed data in the source zip file
90  unsigned int zippedFileOffset() const { return mZippedFileOffset; }
91  // source stream used to seek and read the compressed zip data
92  const VirtualFile* sourceZipFile() const { return mSourceZipFile.get(); }
95 
96  public:
97  unsigned short mVersionNeeded;
98  unsigned short mGeneralPurposeFlag;
99  unsigned short mCompressionMethod;
100  unsigned int mCRC32;
101  unsigned int mCompressedSize;
102  unsigned int mUncompressedSize;
103  unsigned short mFileNameLength;
104  unsigned short mExtraFieldLength;
105  int mSecond;
106  int mMinute;
107  int mHour;
108  int mDay;
109  int mMonth;
110  int mYear;
112  // offset of the compressed data in the zip file
113  unsigned int mZippedFileOffset;
114  // source stream used to seek and read the compressed zip data
116  };
117 //---------------------------------------------------------------------------
118 // ZippedFile
119 //---------------------------------------------------------------------------
134  {
136 
137  // Lower this if you need to limit the amount of data allocated to the stack, for example to 16K.
138  static const int CHUNK_SIZE = 128*1024;
139 
140  public:
141 
142  ZippedFile();
143  ~ZippedFile();
144 
145  const ZippedFileInfo* zippedFileInfo() const;
146 
147  ZippedFileInfo* zippedFileInfo();
148 
149  void setZippedFileInfo(ZippedFileInfo* info);
150 
153  virtual bool exists() const;
154 
155  virtual bool open(EOpenMode mode);
156 
157  virtual bool isOpen() const;
158 
159  virtual void close();
160 
161  virtual long long size() const;
162 
163  bool extract(char* destination, bool check_sum = true);
164 
166  {
167  close();
168  super::operator=(other);
169  mZippedFileInfo = new ZippedFileInfo(*other.mZippedFileInfo);
170  if (mZippedFileInfo->sourceZipFile())
171  {
172  ref<VirtualFile> src_zip_copy = mZippedFileInfo->sourceZipFile()->clone();
173  mZippedFileInfo->setSourceZipFile(src_zip_copy.get());
174  }
175  return *this;
176  }
177 
178  virtual ref<VirtualFile> clone() const;
179 
180  void resetStream();
181 
182  protected:
183  virtual long long read_Implementation(void* buffer, long long bytes_to_read);
184 
185  virtual long long write_Implementation(const void* /*buffer*/, long long /*byte_count*/) { return 0; } // not supported yet
186 
187  virtual bool fillUncompressedBuffer();
188 
189  virtual long long position_Implementation() const { return mReadBytes; }
190 
191  virtual bool seekSet_Implementation(long long);
192 
193  protected:
195  long long mReadBytes;
196 
197  z_stream_s* mZStream;
198  unsigned char mZipBufferIn[CHUNK_SIZE];
199  unsigned char mZipBufferOut[CHUNK_SIZE];
200  std::vector<char> mUncompressedBuffer;
202  };
203  //---------------------------------------------------------------------------
204  // utilty functions
205  bool compress(const void* data, size_t size, std::vector<unsigned char>& out, int level);
206  bool decompress(const void* cdata, size_t csize, void* data_out);
207  //---------------------------------------------------------------------------
208 }
209 
210 #endif
bool decompress(const void *cdata, size_t csize, void *data_out)
Definition: ZippedFile.cpp:94
unsigned short compressionMethod() const
Definition: ZippedFile.hpp:76
const T * get() const
Definition: Object.hpp:128
An abstract class representing a file.
Definition: VirtualFile.hpp:60
Collects the information about a ZippedFile.
Definition: ZippedFile.hpp:46
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
friend class ZippedFile
Definition: ZippedFile.hpp:48
unsigned short mCompressionMethod
Definition: ZippedFile.hpp:99
virtual long long position_Implementation() const
Definition: ZippedFile.hpp:189
unsigned short mExtraFieldLength
Definition: ZippedFile.hpp:104
ref< ZippedFileInfo > mZippedFileInfo
Definition: ZippedFile.hpp:194
const String & path() const
Definition: ZippedFile.hpp:88
int minute() const
Definition: ZippedFile.hpp:83
int second() const
Definition: ZippedFile.hpp:82
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
bool compress(const void *data, size_t size, std::vector< unsigned char > &out, int level)
Definition: ZippedFile.cpp:42
virtual long long write_Implementation(const void *, long long)
Definition: ZippedFile.hpp:185
int month() const
Definition: ZippedFile.hpp:86
ref< VirtualFile > mSourceZipFile
Definition: ZippedFile.hpp:115
Visualization Library main namespace.
z_stream_s * mZStream
Definition: ZippedFile.hpp:197
int day() const
Definition: ZippedFile.hpp:85
unsigned short extraFieldLength() const
Definition: ZippedFile.hpp:81
unsigned int zippedFileOffset() const
Definition: ZippedFile.hpp:90
int hour() const
Definition: ZippedFile.hpp:84
unsigned short mFileNameLength
Definition: ZippedFile.hpp:103
int year() const
Definition: ZippedFile.hpp:87
The base class for all the reference counted objects.
Definition: Object.hpp:158
A VirtualDirectory capable of reading files from a .zip file.
unsigned short generalPurposeFlag() const
Definition: ZippedFile.hpp:75
unsigned int compressedSize() const
Definition: ZippedFile.hpp:78
unsigned int uncompressedSize() const
Definition: ZippedFile.hpp:79
void setSourceZipFile(VirtualFile *file)
Definition: ZippedFile.hpp:94
unsigned short mGeneralPurposeFlag
Definition: ZippedFile.hpp:98
unsigned int mZippedFileOffset
Definition: ZippedFile.hpp:113
int mUncompressedBufferPtr
Definition: ZippedFile.hpp:201
const VirtualFile * sourceZipFile() const
Definition: ZippedFile.hpp:92
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
unsigned short mVersionNeeded
Definition: ZippedFile.hpp:97
unsigned int crc32() const
Definition: ZippedFile.hpp:77
VirtualFile * sourceZipFile()
Definition: ZippedFile.hpp:93
ZippedFile & operator=(const ZippedFile &other)
Definition: ZippedFile.hpp:165
unsigned short fileNameLength() const
Definition: ZippedFile.hpp:80
unsigned short versionNeeded() const
Definition: ZippedFile.hpp:74
std::vector< char > mUncompressedBuffer
Definition: ZippedFile.hpp:200
long long mReadBytes
Definition: ZippedFile.hpp:195
A VirtualFile used to read a file contained in a .zip archive.
Definition: ZippedFile.hpp:133
unsigned int mCompressedSize
Definition: ZippedFile.hpp:101
unsigned int mCRC32
Definition: ZippedFile.hpp:100
unsigned int mUncompressedSize
Definition: ZippedFile.hpp:102