00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00041 #include "OpenCLibzheader.h"
00042 #include <limits.h>
00043 
00044 
00045 
00046 
00047 
00048 void GzCompress(FILE   *in,gzFile out)
00049 {
00050     static char buf[BUFLEN];
00051     int len;
00052     
00053 
00054 #ifdef USE_MMAP
00055     
00056 
00057 
00058     if (GzCompressMmap(in, out) == Z_OK) return;
00059 #endif
00060     for (;;) 
00061     {
00062         len = (int)fread(buf, 1, sizeof(buf), in);
00063         if (len < 0) 
00064         Error("error reading the file \n");
00065     
00066         if (len == 0) break;
00067 
00068         if (gzwrite(out, buf, (unsigned)len) != len) 
00069         Error("error writing to the file \n");;
00070         
00071     }
00072     fclose(in);
00073     
00074     if (gzclose(out) != Z_OK) 
00075     Error("failed gzclose");
00076 }
00077 
00078 
00079 
00080 
00081 
00082 
00083 #ifdef USE_MMAP 
00084 
00085 
00086 
00087 
00088 
00089 
00090 int GzCompressMmap(FILE   * in,gzFile out)
00091 {
00092     int len;
00093     int err;
00094     int ifd = fileno(in);
00095     caddr_t buf;    
00096     off_t buf_len;  
00097     struct stat sb;
00098 
00099     
00100     
00101     if (fstat(ifd, &sb) < 0) 
00102     return Z_ERRNO;
00103     
00104     buf_len = sb.st_size;
00105     
00106     if (buf_len <= 0) 
00107     return Z_ERRNO;
00108 
00109     
00110     
00111     buf =(char*) mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
00112     
00113     
00114     
00115     if (buf == (caddr_t)(-1)) 
00116     return Z_ERRNO;
00117 
00118     
00119     len = gzwrite(out, (char *)buf, (unsigned)buf_len);
00120 
00121     if (len != (int)buf_len) 
00122     Error(gzerror(out, &err));
00123 
00124     munmap(buf, buf_len);
00125     fclose(in);
00126     if (gzclose(out) != Z_OK) 
00127     Error("failed gzclose");
00128     
00129     return Z_OK;
00130 }
00131 #endif 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 
00143 void FileCompress(char  * file,char  * mode)
00144 {
00145     static char outfile[MAX_NAME_LEN];
00146     FILE  *in;
00147     gzFile out;
00148         char choice[3];
00149 
00150     strcpy(outfile, file);
00151     strcat(outfile, GZ_SUFFIX);
00152 
00153     in = fopen(file, "rb");
00154     if (in == NULL) 
00155     {    
00156     Error("file not found ...try again \n");
00157     }
00158     out = gzopen(outfile, mode);
00159     if (out == NULL) 
00160     {       
00161     Error("error opening outfile\n ");        
00162     }
00163     GzCompress(in, out);
00164    
00165     
00166     printf("do you want to delete the original file (y /n) \n");
00167     
00168     scanf("%s",choice);
00169     
00170 
00171     
00172     if(choice[0] == 'y')
00173     unlink(file);
00174     
00175     printf(" Congrats .... compression done ...u can find .gz file in the same directory as of the source file\n");
00176 }
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 void StringCompress()
00190 {
00191     Byte *compr, *uncompr;
00192     uLong len,comprLen,uncomprLen;
00193     char *hello;
00194      
00195     hello = (char *)malloc(100*sizeof(char));
00196   
00197     printf("enter the string(maximum 100 characters) to compress....\n");
00198 
00199 
00200 
00201     scanf("%s",hello); 
00202     len = (uLong)strlen(hello)+1;
00203     comprLen = 10 * len; 
00204     uncomprLen = comprLen;
00205     
00206     
00207     compr    = (Byte*)calloc((uInt)comprLen, 1);  
00208     uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
00209                 
00210                 
00211                 
00212     
00213     
00214     compress(compr, &comprLen, (const Bytef*)hello, len);
00215 
00216     strcpy((char*)uncompr, "garbage"); 
00217     
00218     printf("compressed string is : %s\n\n", (char *)compr);
00219 
00220     
00221     
00222     uncompress(uncompr, &uncomprLen, compr, comprLen);
00223 
00224    
00225     printf("press enter to uncompress it\n");
00226     
00227     getchar();
00228     getchar();
00229     
00230     if (strcmp((char*)uncompr, hello)) 
00231     {
00232         Error("bad uncompress\n");  
00233         free(compr);
00234         free(uncompr);        
00235     }
00236     else 
00237     {
00238         printf("uncompressed string is : %s\n", (char *)uncompr);
00239         free(compr);
00240         free(uncompr);
00241     }
00242     
00243 }
00244 
00245