簡體   English   中英

使用 miniz 提取目錄內的文件

[英]extracting file inside of directory with miniz

有沒有人可以解釋我如何從 zipfile 中的目錄獲取文件。 我使用 c++ 和 miniz(code.google.com/p/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);

此代碼適用於我使用這里的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);

因為我沒有足夠的代表來回復@moodboom 的回答:

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