zlib

2012-05-03 13 views
29

का उपयोग कर .zip फ़ाइल को अनजिप करने का सरल तरीका क्या है .zip फ़ाइल को अनजिप करने और फ़ाइलों को निर्देशिका में निकालने का एक सरल उदाहरण है? मैं वर्तमान में zlib का उपयोग कर रहा हूं, और जब मैं समझता हूं कि zlib सीधे ज़िप फ़ाइलों से निपटता नहीं है, तो zlibs की "contrib" लाइब्रेरी में कई अतिरिक्त चीज़ें दिखाई देती हैं। मैंने देखा और "मिनीजिप" के बारे में पढ़ा, और कुछ दस्तावेज़ पढ़ने और कुछ कोड देखने के बाद, मुझे एक .zip फ़ाइल को अनजिप करने और फ़ाइलों को निर्देशिका में निकालने का एक सरल उदाहरण नहीं दिखाई देता है।zlib

मैं ऐसा करने का एक प्लेटफ़ॉर्म स्वतंत्र तरीका ढूंढना चाहता हूं, लेकिन यदि यह संभव नहीं है तो मुझे विंडोज़ और मैक के लिए एक रास्ता खोजने की आवश्यकता है।

+3

पाठकों के लिए कैसे विपरीत करने के लिए की तलाश में - zlib का उपयोग कर एक ज़िप फ़ाइल बनाने में, मेरा उत्तर यहाँ देखें: http://stackoverflow.com/questions/11370908/how-do-i-use-minizip-on-zlib – niemiro

उत्तर

41

zlib संभालती हवा निकालना संपीड़न/विसंपीड़न एल्गोरिथ्म, लेकिन वहाँ एक ज़िप फ़ाइल में है कि अधिक से अधिक है।

आप libzip आज़मा सकते हैं। यह मुफ़्त, पोर्टेबल और उपयोग करने में आसान है।

अद्यतन: यहाँ मैं libzip की quick'n'dirty उदाहरण देते हैं, सभी त्रुटि नियंत्रण के साथ ommited:

#include <zip.h> 

int main() 
{ 
    //Open the ZIP archive 
    int err = 0; 
    zip *z = zip_open("foo.zip", 0, &err); 

    //Search for the file of given name 
    const char *name = "file.txt"; 
    struct zip_stat st; 
    zip_stat_init(&st); 
    zip_stat(z, name, 0, &st); 

    //Alloc memory for its uncompressed contents 
    char *contents = new char[st.size]; 

    //Read the compressed file 
    zip_file *f = zip_fopen(z, name, 0); 
    zip_fread(f, contents, st.size); 
    zip_fclose(f); 

    //And close the archive 
    zip_close(z); 

    //Do something with the contents 
    //delete allocated memory 
    delete[] contents; 
} 
+2

क्या आप कृपया libzip का उपयोग करके फ़ाइल को अनजिप करने का एक सरल उदाहरण प्रदान कर सकते हैं? – judeclarke

+0

कोई समस्या नहीं है। अद्यतन उत्तर देखें। – rodrigo

+0

विंडोज़ पर संकलन करने के लिए libzip के साथ कुश्ती के दिनों के बाद, यह पता चला है कि आपकी पोस्ट में कोड संकलित नहीं होता है। zip_stat एक फ़ंक्शन है, जैसा कि आपने – judeclarke

25

Minizip एक उदाहरण कार्यक्रमों इसके उपयोग का प्रदर्शन करना होता है - फ़ाइलें minizip.c कहा जाता है और miniunz.c।

अद्यतन: मेरे पास कुछ मिनट थे इसलिए मैंने आपके लिए यह त्वरित, नंगे हड्डियों का उदाहरण छीन लिया। यह बहुत सुगंधित सी है, और मैं बिना किसी बड़े सुधार के इसका उपयोग नहीं करूंगा। उम्मीद है कि अब आप जा रहे हैं।

// uzip.c - Simple example of using the minizip API. 
// Do not use this code as is! It is educational only, and probably 
// riddled with errors and leaks! 
#include <stdio.h> 
#include <string.h> 

#include "unzip.h" 

#define dir_delimter '/' 
#define MAX_FILENAME 512 
#define READ_SIZE 8192 

int main(int argc, char **argv) 
{ 
    if (argc < 2) 
    { 
     printf("usage:\n%s {file to unzip}\n", argv[ 0 ]); 
     return -1; 
    } 

    // Open the zip file 
    unzFile *zipfile = unzOpen(argv[ 1 ]); 
    if (zipfile == NULL) 
    { 
     printf("%s: not found\n"); 
     return -1; 
    } 

    // Get info about the zip file 
    unz_global_info global_info; 
    if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK) 
    { 
     printf("could not read file global info\n"); 
     unzClose(zipfile); 
     return -1; 
    } 

    // Buffer to hold data read from the zip file. 
    char read_buffer[ READ_SIZE ]; 

    // Loop to extract all files 
    uLong i; 
    for (i = 0; i < global_info.number_entry; ++i) 
    { 
     // Get info about current file. 
     unz_file_info file_info; 
     char filename[ MAX_FILENAME ]; 
     if (unzGetCurrentFileInfo(
      zipfile, 
      &file_info, 
      filename, 
      MAX_FILENAME, 
      NULL, 0, NULL, 0) != UNZ_OK) 
     { 
      printf("could not read file info\n"); 
      unzClose(zipfile); 
      return -1; 
     } 

     // Check if this entry is a directory or file. 
     const size_t filename_length = strlen(filename); 
     if (filename[ filename_length-1 ] == dir_delimter) 
     { 
      // Entry is a directory, so create it. 
      printf("dir:%s\n", filename); 
      mkdir(filename); 
     } 
     else 
     { 
      // Entry is a file, so extract it. 
      printf("file:%s\n", filename); 
      if (unzOpenCurrentFile(zipfile) != UNZ_OK) 
      { 
       printf("could not open file\n"); 
       unzClose(zipfile); 
       return -1; 
      } 

      // Open a file to write out the data. 
      FILE *out = fopen(filename, "wb"); 
      if (out == NULL) 
      { 
       printf("could not open destination file\n"); 
       unzCloseCurrentFile(zipfile); 
       unzClose(zipfile); 
       return -1; 
      } 

      int error = UNZ_OK; 
      do  
      { 
       error = unzReadCurrentFile(zipfile, read_buffer, READ_SIZE); 
       if (error < 0) 
       { 
        printf("error %d\n", error); 
        unzCloseCurrentFile(zipfile); 
        unzClose(zipfile); 
        return -1; 
       } 

       // Write data to file. 
       if (error > 0) 
       { 
        fwrite(read_buffer, error, 1, out); // You should check return of fwrite... 
       } 
      } while (error > 0); 

      fclose(out); 
     } 

     unzCloseCurrentFile(zipfile); 

     // Go the the next entry listed in the zip file. 
     if ((i+1) < global_info.number_entry) 
     { 
      if (unzGoToNextFile(zipfile) != UNZ_OK) 
      { 
       printf("cound not read next file\n"); 
       unzClose(zipfile); 
       return -1; 
      } 
     } 
    } 

    unzClose(zipfile); 

    return 0; 
} 

मैं बनाया गया है और इस तरह विंडोज पर MinGW/MSYS के साथ यह परीक्षण किया:

contrib/minizip/$ gcc -I../.. -o unzip uzip.c unzip.c ioapi.c ../../libz.a 
contrib/minizip/$ ./unzip.exe /j/zlib-125.zip 
+0

मैंने पहले उस उदाहरण को देखा और यह एक बड़ा उदाहरण है। क्या आप एक सरल उदाहरण प्रदान कर सकते हैं? – judeclarke

+0

@judeclarke, मैं दिन के लिए अपने देव बॉक्स से दूर हूं, लेकिन जब मैं कर सकता हूं तो मैं कुछ छोटा/सरल पोस्ट करूंगा! –

+5

आपको हेडर फ़ाइलों और उदाहरणों को पढ़ने के लिए बस समय निकालना होगा। सबसे पहले zip.h और unzip.h को देखने के लिए देखें कि कौन से फ़ंक्शन प्रदान किए जाते हैं और वे क्या करते हैं। फिर minizip.c और miniunz.c को देखें कि उनका उपयोग कैसे किया जाता है। –

संबंधित मुद्दे