Fork me on GitHub

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

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

first commit frog

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