MINIZIP + zlib supports AES 256 encryption and is very easy to use!
Code based on minizip unzip.c. include stdio.h zip.h unzip.h
First create a zip with a file inside with a password. The zip file must be in the same directory as the executable file. Run the program from the prompt in the generated program directory. In this example, only the first file is retrieved!
unzFile zfile = unzOpen("teste.zip"); if(zfile==NULL) { printf("Error!"); return; } printf("OK Zip teste.zip opened...\n"); int err = unzGoToFirstFile(zfile); if (err != UNZ_OK) { printf("error %d with zipfile in unzGoToFirstFile\n", err); unzClose(zfile); } char filename_inzip[256] = {0}; unz_file_info file_info = {0}; err = unzGetCurrentFileInfo(zfile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); if (err != UNZ_OK) { printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); } else { int len = 8192; char buffer[8192]={0}; printf("name of first file is :%s\n",filename_inzip); printf("uncompressed_size = %d\n",file_info.uncompressed_size); err = unzOpenCurrentFilePassword(zfile, "yourpassword"); if (err != UNZ_OK) printf("error %d with zipfile in unzOpenCurrentFilePassword\n", err); else printf("password ok\n"); FILE *fp = fopen(filename_inzip, "wb"); if (fp != NULL) { do { err = unzReadCurrentFile(zfile, &buffer, len ); if (err < 0) { printf("error %d with zipfile in unzReadCurrentFile\n", err); break; } if (err == 0) break; if (fwrite(&buffer, err, 1, fp) != 1) { printf("error %d in writing extracted file\n", errno); err = UNZ_ERRNO; break; } } while (err > 0); fclose(fp); } } unzClose(zfile);
Edson K. Cury
source share