11 # include <sys/types.h>
12 # include <sys/stat.h>
27 #define WRITEBUFFERSIZE (16384)
28 #define MAXFILENAME (256)
31 uLong
filetime(f
, tmzip
, dt
)
32 char *f
; /* name of file to get info on */
33 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
34 uLong
*dt
; /* dostime */
42 hFind
= FindFirstFile(f
,&ff32
);
43 if (hFind
!= INVALID_HANDLE_VALUE
)
45 FileTimeToLocalFileTime(&(ff32
.ftLastWriteTime
),&ftLocal
);
46 FileTimeToDosDateTime(&ftLocal
,((LPWORD
)dt
)+1,((LPWORD
)dt
)+0);
55 uLong
filetime(f
, tmzip
, dt
)
56 char *f
; /* name of file to get info on */
57 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
58 uLong
*dt
; /* dostime */
61 struct stat s
; /* results of stat() */
67 char name
[MAXFILENAME
+1];
70 strncpy(name
, f
,MAXFILENAME
-1);
71 /* strncpy doesnt append the trailing NULL, of the string is too long. */
72 name
[ MAXFILENAME
] = '\0';
74 if (name
[len
- 1] == '/')
76 /* not all systems allow stat'ing a file with / appended */
83 filedate
= localtime(&tm_t
);
85 tmzip
->tm_sec
= filedate
->tm_sec
;
86 tmzip
->tm_min
= filedate
->tm_min
;
87 tmzip
->tm_hour
= filedate
->tm_hour
;
88 tmzip
->tm_mday
= filedate
->tm_mday
;
89 tmzip
->tm_mon
= filedate
->tm_mon
;
90 tmzip
->tm_year
= filedate
->tm_year
;
95 uLong
filetime(f
, tmzip
, dt
)
96 char *f
; /* name of file to get info on */
97 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
98 uLong
*dt
; /* dostime */
108 int check_exist_file(filename
)
109 const char* filename
;
113 ftestexist
= fopen(filename
,"rb");
114 if (ftestexist
==NULL
)
123 printf("MiniZip 1.00, demo of zLib + Zip package written by Gilles Vollant\n");
124 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
129 printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_to_add]\n\n" \
130 " -o Overwrite existing file.zip\n" \
131 " -a Append to existing file.zip\n" \
133 " -1 Compress faster\n" \
134 " -9 Compress better\n\n");
137 /* calculate the CRC32 of a file,
138 because to encrypt a file, we need known the CRC32 of the file before */
139 int getFileCrc(const char* filenameinzip
,void*buf
,unsigned long size_buf
,unsigned long* result_crc
)
141 unsigned long calculate_crc
=0;
143 FILE * fin
= fopen(filenameinzip
,"rb");
144 unsigned long size_read
= 0;
145 unsigned long total_read
= 0;
155 size_read
= (int)fread(buf
,1,size_buf
,fin
);
156 if (size_read
< size_buf
)
159 printf("error in reading %s\n",filenameinzip
);
164 calculate_crc
= crc32(calculate_crc
,buf
,size_read
);
165 total_read
+= size_read
;
167 } while ((err
== ZIP_OK
) && (size_read
>0));
172 *result_crc
=calculate_crc
;
173 printf("file %s crc %x\n",filenameinzip
,calculate_crc
);
183 int opt_compress_level
=Z_DEFAULT_COMPRESSION
;
184 int zipfilenamearg
= 0;
185 char filename_try
[MAXFILENAME
+16];
190 const char* password
=NULL
;
205 const char *p
=argv
[i
]+1;
210 if ((c
=='o') || (c
=='O'))
212 if ((c
=='a') || (c
=='A'))
214 if ((c
>='0') && (c
<='9'))
215 opt_compress_level
= c
-'0';
217 if (((c
=='p') || (c
=='P')) && (i
+1<argc
))
225 if (zipfilenamearg
== 0)
230 size_buf
= WRITEBUFFERSIZE
;
231 buf
= (void*)malloc(size_buf
);
234 printf("Error allocating memory\n");
235 return ZIP_INTERNALERROR
;
238 if (zipfilenamearg
==0)
246 strncpy(filename_try
, argv
[zipfilenamearg
],MAXFILENAME
-1);
247 /* strncpy doesnt append the trailing NULL, of the string is too long. */
248 filename_try
[ MAXFILENAME
] = '\0';
250 len
=(int)strlen(filename_try
);
252 if (filename_try
[i
]=='.')
256 strcat(filename_try
,".zip");
258 if (opt_overwrite
==2)
260 /* if the file don't exist, we not append file */
261 if (check_exist_file(filename_try
)==0)
265 if (opt_overwrite
==0)
266 if (check_exist_file(filename_try
)!=0)
272 printf("The file %s exist. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try
);
275 if ((rep
>='a') && (rep
<='z'))
278 while ((rep
!='Y') && (rep
!='N') && (rep
!='A'));
290 # ifdef USEWIN32IOAPI
291 zlib_filefunc_def ffunc
;
292 fill_win32_filefunc(&ffunc
);
293 zf
= zipOpen2(filename_try
,(opt_overwrite
==2) ? 2 : 0,NULL
,&ffunc
);
295 zf
= zipOpen(filename_try
,(opt_overwrite
==2) ? 2 : 0);
300 printf("error opening %s\n",filename_try
);
304 printf("creating %s\n",filename_try
);
306 for (i
=zipfilenamearg
+1;(i
<argc
) && (err
==ZIP_OK
);i
++)
308 if (((*(argv
[i
]))!='-') && ((*(argv
[i
]))!='/'))
312 const char* filenameinzip
= argv
[i
];
314 unsigned long crcFile
=0;
316 zi
.tmz_date
.tm_sec
= zi
.tmz_date
.tm_min
= zi
.tmz_date
.tm_hour
=
317 zi
.tmz_date
.tm_mday
= zi
.tmz_date
.tm_mon
= zi
.tmz_date
.tm_year
= 0;
321 filetime(filenameinzip
,&zi
.tmz_date
,&zi
.dosDate
);
324 err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
325 NULL,0,NULL,0,NULL / * comment * /,
326 (opt_compress_level != 0) ? Z_DEFLATED : 0,
329 if ((password
!= NULL
) && (err
==ZIP_OK
))
330 err
= getFileCrc(filenameinzip
,buf
,size_buf
,&crcFile
);
332 err
= zipOpenNewFileInZip3(zf
,filenameinzip
,&zi
,
333 NULL
,0,NULL
,0,NULL
/* comment*/,
334 (opt_compress_level
!= 0) ? Z_DEFLATED
: 0,
335 opt_compress_level
,0,
336 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
337 -MAX_WBITS
, DEF_MEM_LEVEL
, Z_DEFAULT_STRATEGY
,
341 printf("error in opening %s in zipfile\n",filenameinzip
);
344 fin
= fopen(filenameinzip
,"rb");
348 printf("error in opening %s for reading\n",filenameinzip
);
356 size_read
= (int)fread(buf
,1,size_buf
,fin
);
357 if (size_read
< size_buf
)
360 printf("error in reading %s\n",filenameinzip
);
366 err
= zipWriteInFileInZip (zf
,buf
,size_read
);
369 printf("error in writing %s in the zipfile\n",
374 } while ((err
== ZIP_OK
) && (size_read
>0));
383 err
= zipCloseFileInZip(zf
);
385 printf("error in closing %s in the zipfile\n",
390 errclose
= zipClose(zf
,NULL
);
391 if (errclose
!= ZIP_OK
)
392 printf("error in closing %s\n",filename_try
);