examples/PIPS/openclibz/src/uncompress.c

Go to the documentation of this file.
00001 /*
00002 * ==============================================================================
00003 *  Name        : UnCompress.c
00004 *  Part of     : OpenCLibz
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2007 Nokia Corporation.
00010 *  This material, including documentation and any related
00011 *  computer programs, is protected by copyright controlled by
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 
00021 #include "OpenCLibzheader.h"
00022 
00023 //
00024 // In case of any error , display the error message .
00025 //
00026 void Error(const char * msg)
00027 {
00028     printf( " %s\n",  msg);
00029     getchar();
00030     getchar();
00031     exit(1);
00032 }
00033 
00034 
00035 
00036 //Compresses the file using libz api's
00037 
00038 void GzUnCompress(gzFile   in, FILE     * out)
00039 
00040 {
00041     static char buf[BUFLEN];
00042     int len;
00043     int err;
00044 
00045     for (;;)
00046     {
00047         len = gzread(in, buf, sizeof(buf)); //libz api to read a compressed file
00048         if (len < 0) Error (gzerror(in, &err));
00049         if (len == 0) break;
00050 
00051         if ((int)fwrite(buf, 1, (unsigned)len, out) != len)
00052         {
00053             Error("failed fwrite");
00054         }
00055     }
00056    if (fclose(out)) Error("failed fclose");
00057 
00058    if (gzclose(in) != Z_OK) // libz api to close a compressed file
00059 
00060    Error("failed gzclose");
00061 
00062 
00063 
00064 }
00065 
00066 
00067 
00068 
00069 //UnCompresses the file using libz api's
00070 
00071 void FileUnCompress( char  * file )
00072 
00073 {
00074     static char buf[MAX_NAME_LEN];
00075     char *infile, *outfile;
00076     FILE  *out;
00077     gzFile in;
00078     char choice[3];
00079     uInt len = (uInt)strlen(file);
00080     strcpy(buf, file);
00081 
00082     if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0)
00083     {
00084         infile = file;
00085         outfile = buf;
00086         outfile[len-3] = '\0'; //create the output filename
00087     }
00088     else
00089     {
00090         outfile = file;
00091         infile = buf;
00092         strcat(infile, GZ_SUFFIX); //create the input filename
00093     }
00094     in = gzopen(infile, "rb"); // libz api to open a compressed file
00095 
00096     if (in == NULL)
00097     {
00098 
00099       Error("file not found ...try again \n");
00100 
00101     }
00102     out = fopen(outfile, "wb"); //open the output filename
00103     if (out == NULL)
00104     {
00105     gzclose(in);       // libz api to close a compressed file
00106     }
00107 
00108     GzUnCompress(in, out);
00109 
00110     printf("do you want to delete the original file (y /n) \n");
00111 
00112     scanf("%s",choice);
00113 
00114     if(choice[0] == 'y')
00115     unlink(file);
00116     printf(" Congrats .... uncompression done ...u can find uncompressed file in the same directory as of the source file\n");
00117 
00118 }
00119 
00120 /*  End of File */

Generated by  doxygen 1.6.2