2013-04-28 132 views
0

我正在嘗試使用zlib和minizip。當我在存檔中包含的一個soltuion中構建6個項目時,我下載了所有的作品和項目將創建exe文件(minizip和miniunz)。這裏是問題所在,我不知道如何在我的應用程序中使用miniunz和minizip源代碼,而谷歌沒有幫助。有人對這些庫有經驗,可以一步一步教程如何將這些庫包含在我的應用程序中?用於打開密碼保護文件的C zip庫

或者,如果您有其他庫有密碼保護的文件工作,並可以提供一些教程如何將其納入項目,這將有助於太多,我試圖找到的東西,但沒有教程如何將它們安裝到項目

謝謝

+0

爲什麼不[libzip](http://nih.at/libzip)?它有很好的記錄。 – 2013-04-28 19:07:12

+0

我認爲它只適用於基於Unix的系統 – user2321456 2013-04-28 19:13:46

回答

0

基於minizip unzip.c的代碼。

包含了stdio.h zip.h unzip.h

首先,創建內部使用密碼與文件的zip文件。 壓縮文件必須與可執行文件位於同一目錄中。 從生成的程序的目錄中的提示運行程序。 這個例子只提取第一個文件!

/*-----------start-------------- */ 
/*Tries to open the zip in the current directory.*/ 
unzFile zfile = unzOpen("teste.zip"); 
if(zfile==NULL) 
{ 
    printf("Error!"); 
    return; 
} 

printf("OK Zip teste.zip opened...\n"); 


int err = unzGoToFirstFile(zfile);/*go to first file in zip*/ 
if (err != UNZ_OK) 
{ 
    printf("error %d with zipfile in unzGoToFirstFile\n", err); 
    unzClose(zfile);/*close zip*/ 
} 

/*At this point zfile points to the first file contained in the zip*/ 

char filename_inzip[256] = {0};/* The file name will be returned here */ 
unz_file_info file_info = {0};/*strcuture with info of the first file*/ 

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;/*size of chunk*/ 
    char buffer[8192]={0};/*buffer used to save uncompressed data*/ 
    printf("name of first file is :%s\n",filename_inzip); 
    printf("uncompressed_size = %d\n",file_info.uncompressed_size); 

    /*Use your password here, the same one used to create your zip */ 
    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");/*file for data binary type*/ 
    if (fp != NULL) 
    { 
     do 
     {/*read the current file returned by unzGoToFirstFile to buffer in chunks of the 8192*/ 
      err = unzReadCurrentFile(zfile, &buffer, len); 
      if (err < 0) 
      { 
       printf("error %d with zipfile in unzReadCurrentFile\n", err); 
       break; 
      } 
      if (err == 0) 
       break; 
      /*Save the chunk read to the file*/ 
      if (fwrite(&buffer, err, 1, fp) != 1)/*if error break*/ 
      { 
       printf("error %d in writing extracted file\n", errno); 
       err = UNZ_ERRNO; 
       break; 
      }/*else continue*/ 
     } 
     while (err > 0); 
     /*close file*/ 
     fclose(fp); 
    } 
} 

unzClose(zfile);