Fork me on GitHub

source: svn/trunk/Utilities/FROG/Includes/FROG/FROG_ZLib.cpp@ 253

Last change on this file since 253 was 253, checked in by severine ovyn, 16 years ago

add string.h

File size: 8.3 KB
Line 
1
2#include <stdio.h>
3#include <string.h>
4#include "FROG_ZLib.h"
5
6bool copy(const char* Input, const char* Output){
7 FILE* src_stream = fopen(Input,"rb");
8 if(!src_stream)return false;
9
10 FILE* dst_stream = fopen(Output,"wb");
11 if(!dst_stream){fclose(src_stream);return false;}
12
13 unsigned char buffer[1024];
14 unsigned int Read;
15 while(feof(src_stream)==0){
16 Read = fread(buffer,1,1024,src_stream);
17 if(fwrite(buffer,1,1024,dst_stream)!=Read){printf("There is an error in the copy of %s to %s\n",Input,Output);return false;}
18 }
19
20 fclose(src_stream);
21 fclose(dst_stream);
22
23 return true;
24}
25
26bool FROG_ZLIB_IsGZip(char* fileName){
27 unsigned int N = strlen(fileName);
28
29 if( fileName[N-3]!='.') return false;
30 if(!(fileName[N-2]=='g' || fileName[N-2]=='G'))return false;
31 if(!(fileName[N-1]=='z' || fileName[N-1]=='Z'))return false;
32 return true;
33}
34
35bool FROG_ZLIB_IsUnZippedFileExist(char* source_path){
36 unsigned int N = strlen(source_path);
37
38 char* dest_path = new char[N];
39
40 if(FROG_ZLIB_IsGZip(source_path)){
41 strncpy(dest_path,source_path,N-3);
42 dest_path[N-3] = '\0';
43 }else{
44 strcpy(dest_path,source_path);
45 }
46
47 // Check the dest file do not already exist!
48 FILE* dest_stream = fopen(dest_path,"r");
49 if(dest_stream){
50 fclose(dest_stream);
51 delete dest_path;
52 return true;
53 }
54
55 delete dest_path;
56 return false;
57}
58
59
60bool FROG_ZLIB_Deflate(char* source_path, bool keepIntermediateFile){
61 unsigned int N = strlen(source_path);
62
63 printf("Start Deflation of %s\n",source_path);
64
65 char* dest_path = new char[N+3];
66 sprintf(dest_path,"%s.gz",source_path);
67
68 FILE* source_stream = fopen(source_path,"rb");
69 if(!source_stream){return false;}
70
71 FILE* dest_stream = fopen(dest_path,"wb");
72 if(!dest_stream){return false;}
73
74 int returnedValue = FROG_ZLIB_Deflate(source_stream, dest_stream, 9);
75
76 fclose(source_stream);
77 fclose(dest_stream);
78
79 if(returnedValue==Z_OK){
80 printf("%s has been deflate corretly\n",source_path);
81 printf("new name is %s\n",dest_path);
82 if(!keepIntermediateFile && remove(source_path)!=0)perror( "Error deleting file" );
83 return true;
84 }else if(returnedValue==Z_MEM_ERROR){
85 printf("### ERROR ### Not Enough Memory to deflate the file : %s\n",source_path);
86 }else if(returnedValue==Z_STREAM_ERROR){
87 printf("### ERROR ### Stream error during the deflation of the file : %s\n",source_path);
88 }else if(returnedValue==Z_VERSION_ERROR){
89 printf("### ERROR ### Version error during the deflation of the file : %s\n",source_path);
90 }else if(returnedValue==Z_ERRNO){
91 printf("### ERROR ### Reading/Writing error during the deflation of the file : %s\n",source_path);
92 }
93
94 return false;
95}
96
97
98/* Compress from file source to file dest until EOF on source.
99 def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
100 allocated for processing, Z_STREAM_ERROR if an invalid compression
101 level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
102 version of the library linked do not match, or Z_ERRNO if there is
103 an error reading or writing the files. */
104int FROG_ZLIB_Deflate(FILE* source, FILE* dest, int level)
105{
106 int ret, flush;
107 unsigned have;
108 z_stream strm;
109 unsigned char* in = new unsigned char [CHUNK];
110 unsigned char* out= new unsigned char [CHUNK];
111
112 /* allocate deflate state */
113 strm.zalloc = Z_NULL;
114 strm.zfree = Z_NULL;
115 strm.opaque = Z_NULL;
116// ret = deflateInit(&strm, level);
117 ret = deflateInit2(&strm, level, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
118 if (ret != Z_OK)
119 return ret;
120
121 /* compress until end of file */
122 do {
123 strm.avail_in = fread(in, 1, CHUNK, source);
124 if (ferror(source)) {
125 (void)deflateEnd(&strm);
126 return Z_ERRNO;
127 }
128 flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
129 strm.next_in = in;
130
131 /* run deflate() on input until output buffer not full, finish
132 compression if all of source has been read in */
133 do {
134 strm.avail_out = CHUNK;
135 strm.next_out = out;
136 ret = deflate(&strm, flush); /* no bad return value */
137 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
138 have = CHUNK - strm.avail_out;
139 if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
140 (void)deflateEnd(&strm);
141 return Z_ERRNO;
142 }
143 } while (strm.avail_out == 0);
144 assert(strm.avail_in == 0); /* all input will be used */
145
146 /* done when last data in file processed */
147 } while (flush != Z_FINISH);
148 assert(ret == Z_STREAM_END); /* stream will be complete */
149
150 /* clean up and return */
151 (void)deflateEnd(&strm);
152 return Z_OK;
153}
154
155
156
157
158bool FROG_ZLIB_Inflate(char* source_path, bool keepIntermediateFile){
159 unsigned int N = strlen(source_path);
160
161 if(!FROG_ZLIB_IsGZip(source_path))return false;
162
163
164 char* dest_path = new char[N];
165 strncpy(dest_path,source_path,N-3);
166 dest_path[N-3] = '\0';
167
168 // Check the dest file do not already exist!
169 FILE* dest_stream = fopen(dest_path,"r");
170 if(dest_stream){
171 strcpy(source_path,dest_path);
172 fclose(dest_stream);
173 return true;
174 }
175
176 printf("Start Inflation of %s\n",source_path);
177
178 FILE* src_stream = fopen(source_path,"rb");
179 if(!src_stream){return false;}
180
181 dest_stream = fopen(dest_path,"wb");
182 if(!dest_stream){return false;}
183
184 int returnedValue = FROG_ZLIB_Inflate(src_stream, dest_stream);
185 fclose(src_stream);
186 fclose(dest_stream);
187
188 if(returnedValue==Z_OK){
189 printf("File %s has been inflate corretly\n",source_path);
190 printf("new name is %s\n",dest_path);
191 if(!keepIntermediateFile && remove(source_path)!=0)perror( "Error deleting file" );
192 strcpy(source_path,dest_path);
193 return true;
194 }else if(returnedValue==Z_MEM_ERROR){
195 printf("### ERROR ### Not Enough Memory to inflate the file : %s\n",source_path);
196 }else if(returnedValue==Z_STREAM_ERROR){
197 printf("### ERROR ### Stream error during the inflation of the file : %s\n",source_path);
198 }else if(returnedValue==Z_VERSION_ERROR){
199 printf("### ERROR ### Version error during the inflation of the file : %s\n",source_path);
200 }else if(returnedValue==Z_ERRNO){
201 printf("### ERROR ### Reading/Writing error during the inflation of the file : %s\n",source_path);
202 }else{
203 printf("%i\n",returnedValue);
204 }
205
206 return false;
207}
208
209/* Decompress from file source to file dest until stream ends or EOF.
210 inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
211 allocated for processing, Z_DATA_ERROR if the deflate data is
212 invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
213 the version of the library linked do not match, or Z_ERRNO if there
214 is an error reading or writing the files. */
215int FROG_ZLIB_Inflate(FILE* source, FILE* dest)
216{
217 int ret;
218 unsigned have;
219 z_stream strm;
220 unsigned char* in = new unsigned char[CHUNK];
221 unsigned char* out= new unsigned char[CHUNK];
222
223 /* allocate inflate state */
224 strm.zalloc = Z_NULL;
225 strm.zfree = Z_NULL;
226 strm.opaque = Z_NULL;
227 strm.avail_in = 0;
228 strm.next_in = Z_NULL;
229 //ret = inflateInit(&strm);
230 ret = inflateInit2(&strm,31);
231 if (ret != Z_OK)
232 return ret;
233
234 /* decompress until deflate stream ends or end of file */
235 do {
236 strm.avail_in = fread(in, 1, CHUNK, source);
237 if (ferror(source)) {
238 (void)inflateEnd(&strm);
239 return Z_ERRNO;
240 }
241 if (strm.avail_in == 0)
242 break;
243 strm.next_in = in;
244
245 /* run inflate() on input until output buffer not full */
246 do {
247 strm.avail_out = CHUNK;
248 strm.next_out = out;
249 ret = inflate(&strm, Z_NO_FLUSH);
250 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
251 switch (ret) {
252 case Z_NEED_DICT:
253 ret = Z_DATA_ERROR; /* and fall through */
254 case Z_DATA_ERROR:
255 case Z_MEM_ERROR:
256 (void)inflateEnd(&strm);
257 return ret;
258 }
259 have = CHUNK - strm.avail_out;
260 if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
261 (void)inflateEnd(&strm);
262 return Z_ERRNO;
263 }
264 } while (strm.avail_out == 0);
265
266 /* done when inflate() says it's done */
267 } while (ret != Z_STREAM_END);
268
269 /* clean up and return */
270 (void)inflateEnd(&strm);
271 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
272}
Note: See TracBrowser for help on using the repository browser.