gunzip.cc

Go to the documentation of this file.
00001 /*
00002  * This file is part of openSDK.
00003  *
00004  * Copyright (C) 2006-2007 openSDK team
00005  *
00006  * openSDK is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; version 2.
00009  *
00010  * openSDK is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with openSDK; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  *     $Id: gunzip.cc,v 1.3 2007/03/17 11:50:22 nuno-lopes Exp $
00020  */
00021 
00022 #include <errno.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <fcntl.h>
00026 #include <unistd.h>
00027 #include "gunzip.h"
00028 
00029 int gunzip_module(const char *name, const char *tmp)
00030 {
00031         gzFile file = gzopen(name, "rb");
00032         if (!file) {
00033                 if (errno == 0) {
00034                         return Z_MEM_ERROR;
00035                 }
00036                 return Z_ERRNO;
00037         }
00038 
00039         int dest = open(tmp, O_WRONLY | O_CREAT, 0666);
00040         if (dest == -1) {
00041                 gzclose(file);
00042                 return Z_ERRNO;
00043         }
00044 
00045         // files opened. now uncompress the data and store it
00046         // TODO: use mmap() where possible
00047         do {
00048                 char buf[8192];
00049                 int read = gzread(file, buf, sizeof(buf));
00050 
00051                 if (read == 0) { //EOF
00052                         gzclose(file);
00053                         close(dest);
00054                         return Z_OK;
00055 
00056                 } else if (read == -1) {
00057                         int err;
00058                         gzerror(file, &err);
00059                         return err;
00060 
00061                 } else { //OK, write the buffer to the file
00062                         write(dest, buf, read);
00063                 }
00064 
00065         } while (1);
00066 }

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1