42 bool vl::compress(
const void* data,
size_t size, std::vector<unsigned char>& out_data,
int level)
44 const size_t CHUNK_SIZE = 128*1024;
48 const unsigned char* in = (
const unsigned char*)data;
49 unsigned char out[CHUNK_SIZE];
54 ret = deflateInit(&strm, level);
62 strm.avail_in =
std::min(avail, CHUNK_SIZE);
63 avail -= strm.avail_in;
64 strm.next_in = (
unsigned char*)in;
66 flush = avail == 0 ? Z_FINISH : Z_NO_FLUSH;
70 strm.avail_out = CHUNK_SIZE;
73 ret = deflate(&strm, flush);
74 if(ret == Z_STREAM_ERROR)
80 have = CHUNK_SIZE - strm.avail_out;
81 out_data.insert( out_data.end(), out, out+have );
82 }
while (strm.avail_out == 0);
86 }
while (flush != Z_FINISH);
96 const size_t CHUNK_SIZE = 128*1024;
100 unsigned char* in = (
unsigned char*)cdata;
101 unsigned char out[CHUNK_SIZE];
102 char* out_ptr = (
char*)data_out;
104 strm.zalloc = Z_NULL;
106 strm.opaque = Z_NULL;
108 strm.next_in = Z_NULL;
109 ret = inflateInit(&strm);
113 size_t avail = csize;
117 strm.avail_in =
std::min(avail, CHUNK_SIZE);
118 if (strm.avail_in == 0)
120 avail -= strm.avail_in;
126 strm.avail_out = CHUNK_SIZE;
129 ret = inflate(&strm, Z_NO_FLUSH);
140 have = CHUNK_SIZE - strm.avail_out;
142 memcpy(out_ptr, out, have);
145 while (strm.avail_out == 0);
149 }
while (ret != Z_STREAM_END);
154 return ret == Z_STREAM_END;
160 inline int zcompress(FILE *source, FILE *dest,
int level)
162 const int CHUNK_SIZE = 128*1024;
166 unsigned char in[CHUNK_SIZE];
167 unsigned char out[CHUNK_SIZE];
169 strm.zalloc = Z_NULL;
171 strm.opaque = Z_NULL;
172 ret = deflateInit(&strm, level);
178 strm.avail_in = (uInt)fread(in, 1, CHUNK_SIZE, source);
184 flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
189 strm.avail_out = CHUNK_SIZE;
192 ret = deflate(&strm, flush);
195 have = CHUNK_SIZE - strm.avail_out;
196 if (fwrite(out, 1, have, dest) != have || ferror(dest))
202 }
while (strm.avail_out == 0);
206 }
while (flush != Z_FINISH);
210 (void)deflateEnd(&strm);
214 inline int zdecompress(FILE *source, FILE *dest)
216 const int CHUNK_SIZE = 128*1024;
220 unsigned char in[CHUNK_SIZE];
221 unsigned char out[CHUNK_SIZE];
223 strm.zalloc = Z_NULL;
225 strm.opaque = Z_NULL;
227 strm.next_in = Z_NULL;
228 ret = inflateInit(&strm);
234 strm.avail_in = (uInt)fread(in, 1, CHUNK_SIZE, source);
240 if (strm.avail_in == 0)
246 strm.avail_out = CHUNK_SIZE;
249 ret = inflate(&strm, Z_NO_FLUSH);
261 have = CHUNK_SIZE - strm.avail_out;
262 if (fwrite(out, 1, have, dest) != have || ferror(dest))
268 while (strm.avail_out == 0);
272 }
while (ret != Z_STREAM_END);
275 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
278 inline int zdecompress(
VirtualFile *source,
char *dest,
unsigned int bytes_to_read)
280 const unsigned int CHUNK_SIZE = 128*1024;
284 unsigned char in[CHUNK_SIZE];
285 unsigned char out[CHUNK_SIZE];
287 strm.zalloc = Z_NULL;
289 strm.opaque = Z_NULL;
291 strm.next_in = Z_NULL;
293 ret = inflateInit2(&strm, -15);
299 unsigned int byte_count = CHUNK_SIZE < bytes_to_read ? CHUNK_SIZE : bytes_to_read;
300 strm.avail_in = (uInt)source->
read(in, byte_count);
301 bytes_to_read -= strm.avail_in;
303 if (strm.avail_in == 0)
309 strm.avail_out = CHUNK_SIZE;
312 ret = inflate(&strm, Z_NO_FLUSH);
324 have = CHUNK_SIZE - strm.avail_out;
325 memcpy(dest, out, have);
328 while (strm.avail_out == 0);
332 }
while (ret != Z_STREAM_END);
335 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
345 memset(
mZStream, 0,
sizeof(z_stream_s));
370 Log::error(
"ZippedFile::open(): unsupported archive version.\n");
382 Log::error(
"ZippedFile::open(): unsupported compression method.\n");
388 Log::error(
"ZippedFile::open(): file already open.\n");
394 Log::error(
"ZippedFile::open(): only OM_ReadOnly mode is supported.\n");
400 Log::error(
"ZippedFile::open(): no source zip stream defined.\n");
406 Log::error(
"ZippedFile::open(): source zip stream is already open. Only one ZippedFile at a time can read from the same source.\n");
412 Log::error(
"ZippedFile::open(): could not open source zip stream.\n");
418 Log::error(
"ZippedFile::open(): error seeking beginning of compressed file.\n");
448 memset(
mZStream, 0,
sizeof(z_stream_s));
475 unsigned char buffer[CHUNK_SIZE];
476 long long remained = pos -
position();
477 long long eat_bytes = remained < CHUNK_SIZE ? remained : CHUNK_SIZE;
478 while ( (remained -=
read(buffer, eat_bytes)) )
479 eat_bytes = remained < CHUNK_SIZE ? remained : CHUNK_SIZE;
486 if ( bytes_to_read < 1 )
498 bytes = bytes < bytes_to_read ? bytes : bytes_to_read;
507 long long read_bytes = 0;
523 bytes_to_read -= bytes;
549 Log::error(
"ZippedFile::extract(): the file is already open.\n");
555 Log::error(
"ZippedFile::extract(): unsupported archive version.\n");
561 Log::error(
"ZippedFile::extract(): encription not supported.\n");
569 Log::error(
"ZippedFile::extract(): no source zip stream defined.\n");
575 Log::error(
"ZippedFile::extract(): source zip stream is already open. Only one ZippedFile at a time can read from the same source.\n");
581 Log::error(
"ZippedFile::extract(): could not open source zip stream.\n");
587 Log::error(
"ZippedFile::extract(): not a seek-able zip stream.\n");
595 Log::error(
"ZippedFile::extract(): unsupported compression method.\n");
612 if ( zfile_info->
mCRC32 != crc )
657 ret = inflate(
mZStream, Z_NO_FLUSH);
665 memset(
mZStream, 0,
sizeof(z_stream_s));
666 Log::error(
"ZippedFile::read(): error reading zip stream.\n");
670 have = CHUNK_SIZE -
mZStream->avail_out;
685 file->operator=(*this);
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
bool decompress(const void *cdata, size_t csize, void *data_out)
unsigned int compute(const void *buf, int length)
Computes the a CRC32 checksum of a given buffer or VirtualFile.
An abstract class representing a file.
Collects the information about a ZippedFile.
unsigned char mZipBufferOut[CHUNK_SIZE]
bool extract(char *destination, bool check_sum=true)
virtual bool exists() const
This returns true if zippedFileInfo() has been properly set up but does not check the existence of th...
unsigned short mCompressionMethod
virtual bool isOpen() const =0
Returns true if the file has been opened.
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
ref< ZippedFileInfo > mZippedFileInfo
virtual void close()=0
Closes the file.
bool compress(const void *data, size_t size, std::vector< unsigned char > &out, int level)
Visualization Library main namespace.
void setZippedFileInfo(ZippedFileInfo *info)
unsigned int zippedFileOffset() const
virtual ref< VirtualFile > clone() const
Creates a clone of this class instance.
float min(float a, float b)
unsigned char mZipBufferIn[CHUNK_SIZE]
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
virtual bool seekSet_Implementation(long long)
unsigned short generalPurposeFlag() const
virtual long long size() const
Returns the size of the file in bytes.
unsigned int compressedSize() const
virtual bool open(EOpenMode mode)
Opens the file in the specified mode.
unsigned int uncompressedSize() const
virtual long long read_Implementation(void *buffer, long long bytes_to_read)
virtual void close()
Closes the file.
bool seekSet(long long offset)
Changes the current read/write position of a file.
int mUncompressedBufferPtr
virtual bool isOpen() const
Returns true if the file has been opened.
const VirtualFile * sourceZipFile() const
The ref<> class is used to reference-count an Object.
long long position() const
Returns the current position in the file.
virtual bool fillUncompressedBuffer()
unsigned short versionNeeded() const
std::vector< char > mUncompressedBuffer
unsigned int mCompressedSize
unsigned int mUncompressedSize
const ZippedFileInfo * zippedFileInfo() const