1 /* $NetBSD: minizip.c,v 1.1.1.1 2006/01/14 20:10:58 christos Exp $ */
5 Version 1.01e, February 12th, 2005
7 Copyright (C) 1998-2005 Gilles Vollant
20 # include <sys/types.h>
21 # include <sys/stat.h>
36 #define WRITEBUFFERSIZE (16384)
37 #define MAXFILENAME (256)
40 uLong
filetime(f
, tmzip
, dt
)
41 char *f
; /* name of file to get info on */
42 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
43 uLong
*dt
; /* dostime */
51 hFind
= FindFirstFile(f
,&ff32
);
52 if (hFind
!= INVALID_HANDLE_VALUE
)
54 FileTimeToLocalFileTime(&(ff32
.ftLastWriteTime
),&ftLocal
);
55 FileTimeToDosDateTime(&ftLocal
,((LPWORD
)dt
)+1,((LPWORD
)dt
)+0);
64 uLong
filetime(f
, tmzip
, dt
)
65 char *f
; /* name of file to get info on */
66 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
67 uLong
*dt
; /* dostime */
70 struct stat s
; /* results of stat() */
76 char name
[MAXFILENAME
+1];
78 if (len
> MAXFILENAME
)
81 strncpy(name
, f
,MAXFILENAME
-1);
82 /* strncpy doesnt append the trailing NULL, of the string is too long. */
83 name
[ MAXFILENAME
] = '\0';
85 if (name
[len
- 1] == '/')
87 /* not all systems allow stat'ing a file with / appended */
94 filedate
= localtime(&tm_t
);
96 tmzip
->tm_sec
= filedate
->tm_sec
;
97 tmzip
->tm_min
= filedate
->tm_min
;
98 tmzip
->tm_hour
= filedate
->tm_hour
;
99 tmzip
->tm_mday
= filedate
->tm_mday
;
100 tmzip
->tm_mon
= filedate
->tm_mon
;
101 tmzip
->tm_year
= filedate
->tm_year
;
106 uLong
filetime(f
, tmzip
, dt
)
107 char *f
; /* name of file to get info on */
108 tm_zip
*tmzip
; /* return value: access, modific. and creation times */
109 uLong
*dt
; /* dostime */
119 int check_exist_file(filename
)
120 const char* filename
;
124 ftestexist
= fopen(filename
,"rb");
125 if (ftestexist
==NULL
)
134 printf("MiniZip 1.01b, demo of zLib + Zip package written by Gilles Vollant\n");
135 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
140 printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_to_add]\n\n" \
141 " -o Overwrite existing file.zip\n" \
142 " -a Append to existing file.zip\n" \
144 " -1 Compress faster\n" \
145 " -9 Compress better\n\n");
148 /* calculate the CRC32 of a file,
149 because to encrypt a file, we need known the CRC32 of the file before */
150 int getFileCrc(const char* filenameinzip
,void*buf
,unsigned long size_buf
,unsigned long* result_crc
)
152 unsigned long calculate_crc
=0;
154 FILE * fin
= fopen(filenameinzip
,"rb");
155 unsigned long size_read
= 0;
156 unsigned long total_read
= 0;
166 size_read
= (int)fread(buf
,1,size_buf
,fin
);
167 if (size_read
< size_buf
)
170 printf("error in reading %s\n",filenameinzip
);
175 calculate_crc
= crc32(calculate_crc
,buf
,size_read
);
176 total_read
+= size_read
;
178 } while ((err
== ZIP_OK
) && (size_read
>0));
183 *result_crc
=calculate_crc
;
184 printf("file %s crc %x\n",filenameinzip
,calculate_crc
);
194 int opt_compress_level
=Z_DEFAULT_COMPRESSION
;
195 int zipfilenamearg
= 0;
196 char filename_try
[MAXFILENAME
+16];
201 const char* password
=NULL
;
216 const char *p
=argv
[i
]+1;
221 if ((c
=='o') || (c
=='O'))
223 if ((c
=='a') || (c
=='A'))
225 if ((c
>='0') && (c
<='9'))
226 opt_compress_level
= c
-'0';
228 if (((c
=='p') || (c
=='P')) && (i
+1<argc
))
236 if (zipfilenamearg
== 0)
241 size_buf
= WRITEBUFFERSIZE
;
242 buf
= (void*)malloc(size_buf
);
245 printf("Error allocating memory\n");
246 return ZIP_INTERNALERROR
;
249 if (zipfilenamearg
==0)
257 strncpy(filename_try
, argv
[zipfilenamearg
],MAXFILENAME
-1);
258 /* strncpy doesnt append the trailing NULL, of the string is too long. */
259 filename_try
[ MAXFILENAME
] = '\0';
261 len
=(int)strlen(filename_try
);
263 if (filename_try
[i
]=='.')
267 strcat(filename_try
,".zip");
269 if (opt_overwrite
==2)
271 /* if the file don't exist, we not append file */
272 if (check_exist_file(filename_try
)==0)
276 if (opt_overwrite
==0)
277 if (check_exist_file(filename_try
)!=0)
284 printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try
);
285 ret
= scanf("%1s",answer
);
291 if ((rep
>='a') && (rep
<='z'))
294 while ((rep
!='Y') && (rep
!='N') && (rep
!='A'));
306 # ifdef USEWIN32IOAPI
307 zlib_filefunc_def ffunc
;
308 fill_win32_filefunc(&ffunc
);
309 zf
= zipOpen2(filename_try
,(opt_overwrite
==2) ? 2 : 0,NULL
,&ffunc
);
311 zf
= zipOpen(filename_try
,(opt_overwrite
==2) ? 2 : 0);
316 printf("error opening %s\n",filename_try
);
320 printf("creating %s\n",filename_try
);
322 for (i
=zipfilenamearg
+1;(i
<argc
) && (err
==ZIP_OK
);i
++)
324 if (!((((*(argv
[i
]))=='-') || ((*(argv
[i
]))=='/')) &&
325 ((argv
[i
][1]=='o') || (argv
[i
][1]=='O') ||
326 (argv
[i
][1]=='a') || (argv
[i
][1]=='A') ||
327 (argv
[i
][1]=='p') || (argv
[i
][1]=='P') ||
328 ((argv
[i
][1]>='0') || (argv
[i
][1]<='9'))) &&
329 (strlen(argv
[i
]) == 2)))
333 const char* filenameinzip
= argv
[i
];
335 unsigned long crcFile
=0;
337 zi
.tmz_date
.tm_sec
= zi
.tmz_date
.tm_min
= zi
.tmz_date
.tm_hour
=
338 zi
.tmz_date
.tm_mday
= zi
.tmz_date
.tm_mon
= zi
.tmz_date
.tm_year
= 0;
342 filetime(filenameinzip
,&zi
.tmz_date
,&zi
.dosDate
);
345 err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
346 NULL,0,NULL,0,NULL / * comment * /,
347 (opt_compress_level != 0) ? Z_DEFLATED : 0,
350 if ((password
!= NULL
) && (err
==ZIP_OK
))
351 err
= getFileCrc(filenameinzip
,buf
,size_buf
,&crcFile
);
353 err
= zipOpenNewFileInZip3(zf
,filenameinzip
,&zi
,
354 NULL
,0,NULL
,0,NULL
/* comment*/,
355 (opt_compress_level
!= 0) ? Z_DEFLATED
: 0,
356 opt_compress_level
,0,
357 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
358 -MAX_WBITS
, DEF_MEM_LEVEL
, Z_DEFAULT_STRATEGY
,
362 printf("error in opening %s in zipfile\n",filenameinzip
);
365 fin
= fopen(filenameinzip
,"rb");
369 printf("error in opening %s for reading\n",filenameinzip
);
377 size_read
= (int)fread(buf
,1,size_buf
,fin
);
378 if (size_read
< size_buf
)
381 printf("error in reading %s\n",filenameinzip
);
387 err
= zipWriteInFileInZip (zf
,buf
,size_read
);
390 printf("error in writing %s in the zipfile\n",
395 } while ((err
== ZIP_OK
) && (size_read
>0));
404 err
= zipCloseFileInZip(zf
);
406 printf("error in closing %s in the zipfile\n",
411 errclose
= zipClose(zf
,NULL
);
412 if (errclose
!= ZIP_OK
)
413 printf("error in closing %s\n",filename_try
);