void LoadCreatureZipJSONData(const std::string& filename_in, CreatureLoadDataPacket& load_data) { mz_bool status; size_t uncomp_size; mz_zip_archive zip_archive; // Now try to open the archive. memset(&zip_archive, 0, sizeof(zip_archive)); status = mz_zip_reader_init_file(&zip_archive, filename_in.c_str(), 0); if (!status) { printf("mz_zip_reader_init_file() failed!\n"); return; } mz_zip_archive_file_stat file_stat; if (!mz_zip_reader_file_stat(&zip_archive, 0, &file_stat)) { printf("mz_zip_reader_file_stat() failed!\n"); mz_zip_reader_end(&zip_archive); return; } void * extract_obj = mz_zip_reader_extract_file_to_heap(&zip_archive, file_stat.m_filename, &uncomp_size, 0); if(!extract_obj) { printf("mz_zip_reader_extract_file_to_heap() failed!\n"); mz_zip_reader_end(&zip_archive); return; } std::string real_string((char *)extract_obj); LoadCreatureJSONDataFromString(real_string, load_data); }
//--------------------------------------------------------------------------- char* ZipFile::LoadFileToHeap( const char* fileLocationInsideZip, size_t& out_fileSize ) { char* result = nullptr; result = reinterpret_cast< char* > ( mz_zip_reader_extract_file_to_heap( &m_zipArchive, fileLocationInsideZip, &out_fileSize, 0 ) ); return result; }
int FileReader::unzipFile(QString &filename, QByteArray &file) { mz_bool status; mz_zip_archive archive; memset(&archive, 0, sizeof(archive)); status = mz_zip_reader_init_file(&archive, filename.toStdString().c_str(), 0); if (status < MZ_OK) return status; if (mz_zip_reader_get_num_files(&archive) != 1) { emit ErrorMessage(trUtf8("The archive %1 more than one file, or no files in the archive").arg(filename)); return MZ_PARAM_ERROR; } mz_zip_archive_file_stat file_stat; status = mz_zip_reader_file_stat(&archive, 0, &file_stat); if (status < MZ_OK) { emit ErrorMessage(trUtf8("Error reading the file %1").arg(filename)); mz_zip_reader_end(&archive); return MZ_PARAM_ERROR; } size_t uncompressed_size = file_stat.m_uncomp_size; void *p = mz_zip_reader_extract_file_to_heap(&archive, file_stat.m_filename, &uncompressed_size, 0); if (!p) { emit ErrorMessage(trUtf8("Error extracting file %1").arg(filename)); mz_zip_reader_end(&archive); return MZ_PARAM_ERROR; } file.append(static_cast<char *>(p)); mz_zip_reader_end(&archive); return 0; }
int test_zip(int argc, char *argv[]) { int i, sort_iter; mz_bool status; size_t uncomp_size; mz_zip_archive zip_archive; //mz_zip_archive is a struct of all stuff about the zip void *p; const int N = 50; char data[2048]; char archive_filename[64]; //static const char *s_Test_archive_filename = "__mz_example2_test__.zip"; #ifdef MSC static const char *s_Test_archive_filename = "hallelujah.zip"; #else static const char *s_Test_archive_filename = "E:\\hallelujah.zip"; #endif assert((strlen(s_pTest_str) + 64) < sizeof(data)); printf("miniz.c version: %s\n", MZ_VERSION); (void)argc, (void)argv; // Delete the test archive, so it doesn't keep growing as we run this test remove(s_Test_archive_filename); // Append a bunch of text files to the test archive for (i = (N - 1); i >= 0; --i) { sprintf(archive_filename, "%u.txt", i); sprintf(data, "%u %s %u", (N - 1) - i, s_pTest_str, i); //sprintf(archive_filename, "E://look_a.zip"); // Add a new file to the archive. Note this is an IN-PLACE operation, so if it fails your archive is probably hosed (its central directory may not be complete) but it should be recoverable using zip -F or -FF. So use caution with this guy. // A more robust way to add a file to an archive would be to read it into memory, perform the operation, then write a new archive out to a temp file and then delete/rename the files. // Or, write a new archive to disk to a temp file, then delete/rename the files. For this test this API is fine. status = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, archive_filename, data, strlen(data) + 1, s_pComment, (uint16)strlen(s_pComment), MZ_BEST_COMPRESSION); if (!status) { printf("mz_zip_add_mem_to_archive_file_in_place failed!\n"); return EXIT_FAILURE; } } // Add a directory entry for testing status = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, "directory/", NULL, 0, "no comment", (uint16)strlen("no comment"), MZ_BEST_COMPRESSION); if (!status) { printf("mz_zip_add_mem_to_archive_file_in_place failed!\n"); return EXIT_FAILURE; } // Now try to open the archive. memset(&zip_archive, 0, sizeof(zip_archive)); status = mz_zip_reader_init_file(&zip_archive, s_Test_archive_filename, 0); if (!status) { printf("mz_zip_reader_init_file() failed!\n"); return EXIT_FAILURE; } // Get and print information about each file in the archive. for (i = 0; i < (int)mz_zip_reader_get_num_files(&zip_archive); i++) { mz_zip_archive_file_stat file_stat; if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) { printf("mz_zip_reader_file_stat() failed!\n"); mz_zip_reader_end(&zip_archive); return EXIT_FAILURE; } printf("Filename: \"%s\", Comment: \"%s\", Uncompressed size: %u, Compressed size: %u, Is Dir: %u\n", file_stat.m_filename, file_stat.m_comment, (uint)file_stat.m_uncomp_size, (uint)file_stat.m_comp_size, mz_zip_reader_is_file_a_directory(&zip_archive, i)); if (!strcmp(file_stat.m_filename, "directory/")) { if (!mz_zip_reader_is_file_a_directory(&zip_archive, i)) { printf("mz_zip_reader_is_file_a_directory() didn't return the expected results!\n"); mz_zip_reader_end(&zip_archive); return EXIT_FAILURE; } } } // Close the archive, freeing any resources it was using mz_zip_reader_end(&zip_archive); // Now verify the compressed data for (sort_iter = 0; sort_iter < 2; sort_iter++) { memset(&zip_archive, 0, sizeof(zip_archive)); status = mz_zip_reader_init_file(&zip_archive, s_Test_archive_filename, sort_iter ? MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY : 0); if (!status) { printf("mz_zip_reader_init_file() failed!\n"); return EXIT_FAILURE; } for (i = 0; i < N; i++) { sprintf(archive_filename, "%u.txt", i); sprintf(data, "%u %s %u", (N - 1) - i, s_pTest_str, i); // Try to extract all the files to the heap. p = mz_zip_reader_extract_file_to_heap(&zip_archive, archive_filename, &uncomp_size, 0); if (!p) { printf("mz_zip_reader_extract_file_to_heap() failed!\n"); mz_zip_reader_end(&zip_archive); return EXIT_FAILURE; } // Make sure the extraction really succeeded. if ((uncomp_size != (strlen(data) + 1)) || (memcmp(p, data, strlen(data)))) { printf("mz_zip_reader_extract_file_to_heap() failed to extract the proper data\n"); mz_free(p); mz_zip_reader_end(&zip_archive); return EXIT_FAILURE; } printf("Successfully extracted file \"%s\", size %u\n", archive_filename, (uint)uncomp_size); printf("File data: \"%s\"\n", (const char *)p); // We're done. mz_free(p); } // Close the archive, freeing any resources it was using mz_zip_reader_end(&zip_archive); } printf("Success.\n"); return EXIT_SUCCESS; }
void* dataFile::getDataFileToHeap( const std::string &fileName, size_t &outFileSize ) { return mz_zip_reader_extract_file_to_heap( (mz_zip_archive*) zip_archive, fileName.c_str(), &outFileSize, 0 ); }
extern int process_pptx(char *pptx_file) { // Returns the number of slides, -1 in case of severe failure // (if the file doesn't exist, 0 is returned) mz_bool status; mz_zip_archive zip_archive; char *xml_slide; size_t xml_sz; char *p; char *q; char *s; char *t; int i; int deckid; int maxslide = 0; int notenum; int slidenum = 0; int slideid; char xmlrel[FILENAME_MAX]; short kw; short level; memset(&zip_archive, 0, sizeof(zip_archive)); status = mz_zip_reader_init_file(&zip_archive, pptx_file, 0); if (!status) { fprintf(stderr, "Cannot read %s!\n", pptx_file); } else { deckid = new_deck(pptx_file); // Get and print information about each file in the archive. for (i = 0; i < (int)mz_zip_reader_get_num_files(&zip_archive); i++) { mz_zip_archive_file_stat file_stat; if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) { fprintf(stderr, "mz_zip_reader_file_stat() failed!\n"); mz_zip_reader_end(&zip_archive); return -1; } if (!mz_zip_reader_is_file_a_directory(&zip_archive, i)) { if ((strncmp(file_stat.m_filename, "ppt/notesSlides/", 16) == 0) && strncmp(file_stat.m_filename, "ppt/notesSlides/_rels/", 22)) { // Notes // // *** ACHTUNG *** // Note numbers don't match the corresponding slide number, // that would be too easy. You have to dig into "_rels" // if (!sscanf(&(file_stat.m_filename[26]), "%d", ¬enum)) { fprintf(stderr, "Failed to extract note num from %s\n", &(file_stat.m_filename[16])); return -1; } sprintf(xmlrel, "ppt/notesSlides/_rels/notesSlide%d.xml.rels", notenum); if ((xml_slide = (char *)mz_zip_reader_extract_file_to_heap(&zip_archive, (const char *)xmlrel, &xml_sz, (mz_uint)0)) != (char *)NULL) { // Try to find the b****y slide number if ((p = strstr(xml_slide, "Target=\"../slides/slide")) != NULL) { if (!sscanf(&(p[23]), "%d", &slidenum)) { fprintf(stderr, "Failed to extract slidenum from %s\n", &(p[36])); return -1; } } free(xml_slide); } if (slidenum) { if (slidenum > maxslide) { maxslide = slidenum; } slideid = new_slide(deckid, slidenum); xml_sz = 0; if ((xml_slide = (char *)mz_zip_reader_extract_file_to_heap(&zip_archive, (const char *)file_stat.m_filename, &xml_sz, (mz_uint)0)) != (char *)NULL) { if (get_mode() & OPT_TAGS) { // Exclusively look for indexing words in the notes q = xml_slide; level = 0; while ((p = strchr(q, '[')) != (char *)NULL) { level++; do { p++; } while (isspace(*p)); q = p + 1; while (*q && ((*q != ']') || (level > 1))) { switch (*q) { case '[': level++; break; case ']': level--; break; default: break; } q++; } if (*q == ']') { *q = '\0'; if (get_sep() == ';') { // Replace HTML entities if there are any replace_entities(p); } while ((s = strchr(p, get_sep())) != (char *)NULL) { *s++ = '\0'; if (*p == get_kw()) { p++; kw = 1; } else { if (autokw()) { kw = lowercase_word(p); } else { kw = 0; } } if ((t = cleanup(p)) != (char *)NULL) { new_word_as_is(t, slideid, 'T', kw); free(t); } p = s; while (isspace(*p)) { p++; } } if (*p == get_kw()) { p++; kw = 1; } else { if (autokw()) { kw = lowercase_word(p); } else { kw = 0; } } if ((t = cleanup(p)) != (char *)NULL) { new_word_as_is(t, slideid, 'T', kw); free(t); } p = 1 + q; } else { break; } } } else { // Analyze text analyze_text(slideid, xml_slide, 'N'); } free(xml_slide); } else { fprintf(stderr, "Extract flopped\n"); return -1; } } } // We look at regular slides even if we are only interested // in tags to check that we aren't missing any slide without // notes and that our tag count is correct if ((strncmp(file_stat.m_filename, "ppt/slides/", 11) == 0) && strncmp(file_stat.m_filename, "ppt/slides/_rels/", 17)) { // Regular slide if (!sscanf(&(file_stat.m_filename[16]), "%d", &slidenum)) { fprintf(stderr, "Failed to extract num from %s\n", &(file_stat.m_filename[11])); return -1; } if (slidenum > maxslide) { maxslide = slidenum; } slideid = new_slide(deckid, slidenum); if (!(get_mode() & OPT_TAGS)) { xml_sz = 0; if ((xml_slide = (char *)mz_zip_reader_extract_file_to_heap(&zip_archive, (const char *)file_stat.m_filename, &xml_sz, (mz_uint)0)) != (char *)NULL) { // Analyze text analyze_text(slideid, xml_slide, 'S'); // Analyze images analyze_pic(slideid, xml_slide); free(xml_slide); } else { fprintf(stderr, "Extract flopped\n"); return -1; } } } } } // Close the archive, freeing any resources it was using mz_zip_reader_end(&zip_archive); } return maxslide; }
bool pak::bin( const std::string &bin_import ) //const { this->clear(); std::vector<pakfile> result; if( !bin_import.size() ) return true; // :) if( type == paktype::ZIP ) { // Try to open the archive. mz_zip_archive zip_archive; memset(&zip_archive, 0, sizeof(zip_archive)); mz_bool status = mz_zip_reader_init_mem( &zip_archive, (void *)bin_import.c_str(), bin_import.size(), 0 ); if( !status ) return "mz_zip_reader_init_file() failed!", false; // Get and print information about each file in the archive. for( unsigned int i = 0; i < mz_zip_reader_get_num_files(&zip_archive); i++ ) { mz_zip_archive_file_stat file_stat; if( !mz_zip_reader_file_stat( &zip_archive, i, &file_stat ) ) { mz_zip_reader_end( &zip_archive ); return "mz_zip_reader_file_stat() failed!", false; } result.push_back( pakfile() ); result.back()["filename"] = file_stat.m_filename; result.back()["comment"] = file_stat.m_comment; result.back()["size"] = (unsigned int)file_stat.m_uncomp_size; result.back()["size_z"] = (unsigned int)file_stat.m_comp_size; //result.back()["modify_time"] = ze.mtime; //result.back()["access_time"] = ze.atime; //result.back()["create_time"] = ze.ctime; //result.back()["attributes"] = ze.attr; if( const bool decode = true ) { // Try to extract file to the heap. size_t uncomp_size; void *p = mz_zip_reader_extract_file_to_heap(&zip_archive, file_stat.m_filename, &uncomp_size, 0); if( !p ) { mz_zip_reader_end(&zip_archive); return "mz_zip_reader_extract_file_to_heap() failed!", false; } // Make sure the extraction really succeeded. /* if ((uncomp_size != strlen(s_pStr)) || (memcmp(p, s_pStr, strlen(s_pStr)))) { free(p); mz_zip_reader_end(&zip_archive); return "mz_zip_reader_extract_file_to_heap() failed to extract the proper data", false; } */ result.back()["content"].resize( uncomp_size ); memcpy( (void *)result.back()["content"].data(), p, uncomp_size ); free(p); } } // We're done. mz_zip_reader_end(&zip_archive); } else {} this->resize( result.size() ); std::copy( result.begin(), result.end(), this->begin() ); return true; }