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