简体   繁体   English

使用 miniz 提取目录内的文件

[英]extracting file inside of directory with miniz

Is there someone that can explain me how i can get files from directories inside a zipfile.有没有人可以解释我如何从 zipfile 中的目录获取文件。 I use c++ and miniz(code.google.com/p/miniz/).我使用 c++ 和 miniz(code.google.com/p/miniz/)。 thank you in advance.先感谢您。 here my code i have that i use right now:这是我现在使用的代码:

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);

This code is working for me using miniz from 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);

Since i dont have enough rep to respond to @moodboom answer:因为我没有足够的代表来回复@moodboom 的回答:

Sry for the necro but, for future reference, yes when reading directly from a file using mz_zip_reader_init_file it won't work, i faced this myself, the solution was just initialize the variable zip_archive so the full line would be mz_zip_archive zip_archive = mz_zip_archive(); Sry for the necro 但是,为了将来参考,是的,当使用mz_zip_reader_init_file直接从文件中mz_zip_reader_init_file它不起作用,我自己也遇到了这个问题,解决方案只是初始化变量zip_archive所以整行将是mz_zip_archive zip_archive = mz_zip_archive();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM