3 Version 1.01e, February 12th, 2005
5 Copyright (C) 1998-2005 Gilles Vollant
18 # include <sys/types.h>
19 # include <sys/stat.h>
34 #define WRITEBUFFERSIZE (16384)
35 #define MAXFILENAME (256)
38 uLong
filetime(f
, tmzip
, dt
)
39 char *f
; /* name of file to get info on */
40 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
41 uLong
*dt
; /* dostime */
49 hFind
= FindFirstFile(f
,&ff32
);
50 if (hFind
!= INVALID_HANDLE_VALUE
)
52 FileTimeToLocalFileTime(&(ff32
.ftLastWriteTime
),&ftLocal
);
53 FileTimeToDosDateTime(&ftLocal
,((LPWORD
)dt
)+1,((LPWORD
)dt
)+0);
62 uLong
filetime(f
, tmzip
, dt
)
63 char *f
; /* name of file to get info on */
64 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
65 uLong
*dt
; /* dostime */
68 struct stat s
; /* results of stat() */
74 char name
[MAXFILENAME
+1];
76 if (len
> MAXFILENAME
)
79 strncpy(name
, f
,MAXFILENAME
-1);
80 /* strncpy doesnt append the trailing NULL, of the string is too long. */
81 name
[ MAXFILENAME
] = '\0';
83 if (name
[len
- 1] == '/')
85 /* not all systems allow stat'ing a file with / appended */
92 filedate
= localtime(&tm_t
);
94 tmzip
->tm_sec
= filedate
->tm_sec
;
95 tmzip
->tm_min
= filedate
->tm_min
;
96 tmzip
->tm_hour
= filedate
->tm_hour
;
97 tmzip
->tm_mday
= filedate
->tm_mday
;
98 tmzip
->tm_mon
= filedate
->tm_mon
;
99 tmzip
->tm_year
= filedate
->tm_year
;
104 uLong
filetime(f
, tmzip
, dt
)
105 char *f
; /* name of file to get info on */
106 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
107 uLong
*dt
; /* dostime */
117 int check_exist_file(filename
)
118 const char* filename
;
122 ftestexist
= fopen(filename
,"rb");
123 if (ftestexist
==NULL
)
132 printf("MiniZip 1.01b, demo of zLib + Zip package written by Gilles Vollant\n");
133 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
138 printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_to_add]\n\n" \
139 " -o Overwrite existing file.zip\n" \
140 " -a Append to existing file.zip\n" \
142 " -1 Compress faster\n" \
143 " -9 Compress better\n\n");
146 /* calculate the CRC32 of a file,
147 because to encrypt a file, we need known the CRC32 of the file before */
148 int getFileCrc(const char* filenameinzip
,void*buf
,unsigned long size_buf
,unsigned long* result_crc
)
150 unsigned long calculate_crc
=0;
152 FILE * fin
= fopen(filenameinzip
,"rb");
153 unsigned long size_read
= 0;
154 unsigned long total_read
= 0;
164 size_read
= (int)fread(buf
,1,size_buf
,fin
);
165 if (size_read
< size_buf
)
168 printf("error in reading %s\n",filenameinzip
);
173 calculate_crc
= crc32(calculate_crc
,buf
,size_read
);
174 total_read
+= size_read
;
176 } while ((err
== ZIP_OK
) && (size_read
>0));
181 *result_crc
=calculate_crc
;
182 printf("file %s crc %x\n",filenameinzip
,calculate_crc
);
192 int opt_compress_level
=Z_DEFAULT_COMPRESSION
;
193 int zipfilenamearg
= 0;
194 char filename_try
[MAXFILENAME
+16];
199 const char* password
=NULL
;
214 const char *p
=argv
[i
]+1;
219 if ((c
=='o') || (c
=='O'))
221 if ((c
=='a') || (c
=='A'))
223 if ((c
>='0') && (c
<='9'))
224 opt_compress_level
= c
-'0';
226 if (((c
=='p') || (c
=='P')) && (i
+1<argc
))
234 if (zipfilenamearg
== 0)
239 size_buf
= WRITEBUFFERSIZE
;
240 buf
= (void*)malloc(size_buf
);
243 printf("Error allocating memory\n");
244 return ZIP_INTERNALERROR
;
247 if (zipfilenamearg
==0)
255 strncpy(filename_try
, argv
[zipfilenamearg
],MAXFILENAME
-1);
256 /* strncpy doesnt append the trailing NULL, of the string is too long. */
257 filename_try
[ MAXFILENAME
] = '\0';
259 len
=(int)strlen(filename_try
);
261 if (filename_try
[i
]=='.')
265 strcat(filename_try
,".zip");
267 if (opt_overwrite
==2)
269 /* if the file don't exist, we not append file */
270 if (check_exist_file(filename_try
)==0)
274 if (opt_overwrite
==0)
275 if (check_exist_file(filename_try
)!=0)
282 printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try
);
283 ret
= scanf("%1s",answer
);
289 if ((rep
>='a') && (rep
<='z'))
292 while ((rep
!='Y') && (rep
!='N') && (rep
!='A'));
304 # ifdef USEWIN32IOAPI
305 zlib_filefunc_def ffunc
;
306 fill_win32_filefunc(&ffunc
);
307 zf
= zipOpen2(filename_try
,(opt_overwrite
==2) ? 2 : 0,NULL
,&ffunc
);
309 zf
= zipOpen(filename_try
,(opt_overwrite
==2) ? 2 : 0);
314 printf("error opening %s\n",filename_try
);
318 printf("creating %s\n",filename_try
);
320 for (i
=zipfilenamearg
+1;(i
<argc
) && (err
==ZIP_OK
);i
++)
322 if (!((((*(argv
[i
]))=='-') || ((*(argv
[i
]))=='/')) &&
323 ((argv
[i
][1]=='o') || (argv
[i
][1]=='O') ||
324 (argv
[i
][1]=='a') || (argv
[i
][1]=='A') ||
325 (argv
[i
][1]=='p') || (argv
[i
][1]=='P') ||
326 ((argv
[i
][1]>='0') || (argv
[i
][1]<='9'))) &&
327 (strlen(argv
[i
]) == 2)))
331 const char* filenameinzip
= argv
[i
];
333 unsigned long crcFile
=0;
335 zi
.tmz_date
.tm_sec
= zi
.tmz_date
.tm_min
= zi
.tmz_date
.tm_hour
=
336 zi
.tmz_date
.tm_mday
= zi
.tmz_date
.tm_mon
= zi
.tmz_date
.tm_year
= 0;
340 filetime(filenameinzip
,&zi
.tmz_date
,&zi
.dosDate
);
343 err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
344 NULL,0,NULL,0,NULL / * comment * /,
345 (opt_compress_level != 0) ? Z_DEFLATED : 0,
348 if ((password
!= NULL
) && (err
==ZIP_OK
))
349 err
= getFileCrc(filenameinzip
,buf
,size_buf
,&crcFile
);
351 err
= zipOpenNewFileInZip3(zf
,filenameinzip
,&zi
,
352 NULL
,0,NULL
,0,NULL
/* comment*/,
353 (opt_compress_level
!= 0) ? Z_DEFLATED
: 0,
354 opt_compress_level
,0,
355 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
356 -MAX_WBITS
, DEF_MEM_LEVEL
, Z_DEFAULT_STRATEGY
,
360 printf("error in opening %s in zipfile\n",filenameinzip
);
363 fin
= fopen(filenameinzip
,"rb");
367 printf("error in opening %s for reading\n",filenameinzip
);
375 size_read
= (int)fread(buf
,1,size_buf
,fin
);
376 if (size_read
< size_buf
)
379 printf("error in reading %s\n",filenameinzip
);
385 err
= zipWriteInFileInZip (zf
,buf
,size_read
);
388 printf("error in writing %s in the zipfile\n",
393 } while ((err
== ZIP_OK
) && (size_read
>0));
402 err
= zipCloseFileInZip(zf
);
404 printf("error in closing %s in the zipfile\n",
409 errclose
= zipClose(zf
,NULL
);
410 if (errclose
!= ZIP_OK
)
411 printf("error in closing %s\n",filename_try
);