| | 221 | |
| | 222 | === Getting boinc_zip === |
| | 223 | |
| | 224 | boinc_zip is no longer in the main boinc subversion "trunk" but resides in this "depends" brance: |
| | 225 | |
| | 226 | svn co http://boinc.berkeley.edu/svn/trunk/depends_projects/zip |
| | 227 | |
| | 228 | Note for Linux/Mac: To build along with the other boinc libraries, you will need to add the following lines to the bottom of the '''configure.ac''' file (where the various Makefiles are listed): |
| | 229 | |
| | 230 | {{{ |
| | 231 | zip/Makefile |
| | 232 | zip/zip/Makefile |
| | 233 | zip/unzip/Makefile |
| | 234 | }}} |
| | 235 | |
| | 236 | |
| | 237 | Similarly for the '''Makefile.am''' file -- add zip, zip/zip and zip/unzip to the library subdirs: |
| | 238 | |
| | 239 | {{{ |
| | 240 | if ENABLE_LIBRARIES |
| | 241 | API_SUBDIRS = api lib zip zip/zip zip/unzip |
| | 242 | endif |
| | 243 | }}} |
| | 244 | |
| | 245 | == Client and Server Compression and Decompression using gzip (zlib) == #gzip |
| | 246 | |
| | 247 | These basic routines may be useful if you want to compress/decompress a file using the zlib library (usually called "libz.a" and available for most platforms). Include the header file below (qcn_gzip.h) in your program, and link against libz, and you will gain two simple to use functions for gzip'ing or gunzip'ing a file. This is for simple single file or file-by-file compression or decompression (i.e. one file that is to be compressed into a .gz or decompressed back to it's original uncompressed state). You can check for boinc client status if you want the ability to quit inside an operation etc. |
| | 248 | |
| | 249 | qcn_gzip.h: |
| | 250 | |
| | 251 | {{{ |
| | 252 | #ifndef _QCN_GZIP_H_ |
| | 253 | #define _QCN_GZIP_H_ |
| | 254 | |
| | 255 | #include <zlib.h> |
| | 256 | |
| | 257 | int do_gzip(const char* strGZ, const char* strInput); |
| | 258 | int do_gunzip(const char* strGZ, const char* strInput, bool bKeep = false); |
| | 259 | |
| | 260 | #endif |
| | 261 | }}} |
| | 262 | |
| | 263 | |
| | 264 | qcn_gzip.cpp: |
| | 265 | |
| | 266 | {{{ |
| | 267 | #include <stdio.h> |
| | 268 | #include "filesys.h" |
| | 269 | #include "qcn_gzip.h" |
| | 270 | |
| | 271 | int do_gzip(const char* strGZ, const char* strInput) |
| | 272 | { |
| | 273 | // take an input file (strInput) and turn it into a compressed file (strGZ) |
| | 274 | // get rid of the input file after |
| | 275 | FILE* fIn = boinc_fopen(strInput, "rb"); |
| | 276 | if (!fIn) return 1; //error |
| | 277 | gzFile fOut = gzopen(strGZ, "wb"); |
| | 278 | if (!fOut) return 1; //error |
| | 279 | fseek(fIn, 0, SEEK_SET); // go to the top of the files |
| | 280 | gzseek(fOut, 0, SEEK_SET); |
| | 281 | unsigned char buf[1024]; |
| | 282 | long lRead = 0, lWrite = 0; |
| | 283 | while (!feof(fIn)) { // read 1KB at a time until end of file |
| | 284 | memset(buf, 0x00, 1024); |
| | 285 | lRead = 0; |
| | 286 | lRead = (long) fread(buf, 1, 1024, fIn); |
| | 287 | lWrite = (long) gzwrite(fOut, buf, lRead); |
| | 288 | if (lRead != lWrite) break; |
| | 289 | } |
| | 290 | gzclose(fOut); |
| | 291 | fclose(fIn); |
| | 292 | if (lRead != lWrite) return 1; //error -- read bytes != written bytes |
| | 293 | // if we made it here, it compressed OK, can erase strInput and leave |
| | 294 | boinc_delete_file(strInput); |
| | 295 | return 0; |
| | 296 | } |
| | 297 | |
| | 298 | // CMC - commented out status calls, are they too paranoid? |
| | 299 | // if needed use sm->statusBOINC instead (for quit_request etc) |
| | 300 | |
| | 301 | int do_gunzip(const char* strGZ, const char* strInput, bool bKeep) |
| | 302 | { |
| | 303 | // take an input file (strInput) and turn it into a compressed file (strGZ) |
| | 304 | // get rid of the input file after |
| | 305 | //s.quit_request = 0; |
| | 306 | //checkBOINCStatus(); |
| | 307 | FILE* fIn = boinc_fopen(strInput, "wb"); |
| | 308 | if (!fIn) return 1; //error |
| | 309 | gzFile fOut = gzopen(strGZ, "rb"); |
| | 310 | if (!fOut) return 1; //error |
| | 311 | fseek(fIn, 0, SEEK_SET); // go to the top of the files |
| | 312 | gzseek(fOut, 0, SEEK_SET); |
| | 313 | unsigned char buf[1024]; |
| | 314 | long lRead = 0, lWrite = 0; |
| | 315 | while (!gzeof(fOut)) { // read 1KB at a time until end of file |
| | 316 | memset(buf, 0x00, 1024); |
| | 317 | lRead = 0; |
| | 318 | lRead = (long) gzread(fOut,buf,1024); |
| | 319 | lWrite = (long) fwrite(buf, 1, 1024, fIn); |
| | 320 | if (lRead != lWrite) break; |
| | 321 | //boinc_get_status(&s); |
| | 322 | //if (s.quit_request || s.abort_request || s.no_heartbeat) break; |
| | 323 | } |
| | 324 | gzclose(fOut); |
| | 325 | fclose(fIn); |
| | 326 | //checkBOINCStatus(); |
| | 327 | if (lRead != lWrite) return 1; //error -- read bytes != written bytes |
| | 328 | // if we made it here, it compressed OK, can erase strInput and leave |
| | 329 | if (!bKeep) boinc_delete_file(strGZ); |
| | 330 | return 0; |
| | 331 | } |
| | 332 | |
| | 333 | }}} |