2013-05-05 5 views
3

zip 파일 내부의 디렉토리에서 파일을 가져 오는 방법을 설명 할 수있는 사람이 있습니까? 저는 C++과 miniz (code.google.com/p/miniz/)를 사용합니다. 미리 감사드립니다. 여기 내 코드 내가 지금 사용하는 것이 있습니다minives를 사용하여 디렉토리 내부에서 파일 압축 풀기

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

이 코드는 here에서 miniz를 사용하여 저를 위해 노력하고 있습니다.

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

이 때문에 downvoted, 나는 그것이 누군가를 위해 작동하지 않았다 궁금하네요. 나를 위해 작동하지만, 아마도 지금은 최신 miniz 코드와 동기화되지 않습니다. 더 구체적인 피드백이 있다면 알려주십시오. – moodboom