2013-05-05 26 views
3

有沒有人能解釋我如何從zipfile文件中獲取文件。 我使用C++和miniz(code.google.com/p/miniz/)。先謝謝你。 這裏我的代碼我有我現在使用:用miniz解壓縮目錄裏面的文件

size_t uncomp_size; 
mz_bool status; 
mz_zip_archive zip_archive; 

memset(&zip_archive, 0, sizeof(zip_archive)); 
status = mz_zip_reader_init_file(&zip_archive, "data.zip", 0); 
if (!status){ 
    puts("failed to open zip file\n"); 
    return 0; 
} 
try{ 
    void* p = NULL; 
    std::string file_to_extract = "data//test.txt"; 
    int file_index = mz_zip_reader_locate_file(&zip_archive, file_to_extract.c_str(), NULL, MZ_ZIP_FLAG_IGNORE_PATH); 
    if (file_index < 0) 
    { 
     mz_bool is_dir = mz_zip_reader_is_file_a_directory(&zip_archive,file_index); 
     if(is_dir){ 
      throw std::exception("file_index = folder"); 
     }else{ 
      throw std::exception("cannot find file in zip(0)"); 
     } 
    } 

    p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, &uncomp_size, NULL); 
    if(!p){ 
     throw std::exception("cannot find file in zip(1)"); 
    } 
    std::fstream fp1("test.txt",ios::binary|ios::out); 
    fp1.write(reinterpret_cast<char*>(p),uncomp_size); 
    fp1.close(); 
    delete p; 
}catch(std::exception ex){ 
    cout << ex.what() << endl; 
} 
mz_zip_reader_end(&zip_archive); 

回答

3

該代碼使用miniz從here爲我工作。

string str_zip; // The zip archive in string form. 
string str_unzip; // The uncompressed contents of the first file in the zip archive. 

// Read in or assign zip contents to the string. 
// In my case I receive the zip file via a web service. 
// The processing all takes place in memory. 
// But you can easily read a file's contents into the zipfile string, as well. 

typedef unsigned char uint8; 
typedef unsigned short uint16; 
typedef unsigned int uint; 

mz_zip_archive zip_archive; 
mz_bool status; 

// Now try to open the archive. 
memset(&zip_archive, 0, sizeof(zip_archive)); 

// You can provide the zip data in memory as I did... 
status = mz_zip_reader_init_mem(&zip_archive, str_zip.c_str(), str_zip.size(), 0); 

// Or you can just give a filename... 
// status = mz_zip_reader_init_file(&zip_archive, "myfile.zip", 0); 

if (!status) 
{ 
    cout << "zip file appears invalid..." << endl; 
    return; 
} 

// Get the first file in the archive. 
if (mz_zip_reader_get_num_files(&zip_archive) != 1) 
{ 
    cout << "zip file does not contain our 1 file..." << endl; 
    return; 
} 

mz_zip_archive_file_stat file_stat; 
if (!mz_zip_reader_file_stat(&zip_archive, 0, &file_stat)) 
{ 
    cout << "zip file read error..." << endl; 
    mz_zip_reader_end(&zip_archive); 
    return; 
} 

// Unzip the file to heap. 
size_t uncompressed_size = file_stat.m_uncomp_size; 
void* p = mz_zip_reader_extract_file_to_heap(&zip_archive, file_stat.m_filename, &uncompressed_size, 0); 
if (!p) 
{ 
    cout << "mz_zip_reader_extract_file_to_heap() failed..." << endl; 
    mz_zip_reader_end(&zip_archive); 
    return; 
} 

str_unzip.assign((const char*)p,uncompressed_size); 

// Close the archive, freeing any resources it was using 
mz_free(p); 
mz_zip_reader_end(&zip_archive); 
+0

既然這得到了降低,我想知道它是否不適合某人。適用於我,但它現在可能與最新的miniz代碼不同步。讓我知道是否有更具體的反饋。 – moodboom 2014-10-15 17:05:26