// open from file bool ZipIO::openArchive(const std::string &ArchiveName, OpenMode Mode) { // first close last opened archive _kaname = ""; closeArchive(); // inite object memset(&_kzarchive, 0, sizeof(_kzarchive)); // open archive mz_bool status; if (Mode == OpenMode::READ) { status = mz_zip_reader_init_file(&_kzarchive, ArchiveName.c_str(), 0); } else if (Mode == OpenMode::WRITE){ remove(ArchiveName.c_str()); status = true; } else if (Mode == OpenMode::APPEND){ status = true; } if (!status) return false; _kmode = Mode; _kisopen = true; _kaname = ArchiveName; return true; }
BYTE uncomp_zip_file_from_archive(_uncomp_file_data *file) { mz_zip_archive zip_archive; memset(&zip_archive, 0, sizeof(zip_archive)); if (!mz_zip_reader_init_file(&zip_archive, info.rom_file, 0)) { fprintf(stderr, "mz_zip_reader_init_file() failed!\n"); return (EXIT_ERROR); } mz_zip_reader_get_filename(&zip_archive, file->num, uncomp.buffer, sizeof(uncomp.buffer)); snprintf(uncomp.uncompress_file, sizeof(uncomp.uncompress_file), "%s" TMP_FOLDER "/%s", info.base_folder, basename(uncomp.buffer)); if (mz_zip_reader_extract_to_file(&zip_archive, file->num, uncomp.uncompress_file, 0)) { strncpy(uncomp.compress_archive, info.rom_file, sizeof(uncomp.compress_archive)); strncpy(info.rom_file, uncomp.uncompress_file, sizeof(info.rom_file)); info.uncompress_rom = TRUE; } else { fprintf(stderr, "unzip file failed!\n"); } // Close the archive, freeing any resources it was using mz_zip_reader_end(&zip_archive); return (EXIT_OK); }
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); }
dataFile::dataFile( const char* dataFileName ) { remove( dataFileName ); zip_archive = malloc( sizeof(mz_zip_archive) ); memset( zip_archive, 0, sizeof(mz_zip_archive) ); mz_zip_reader_init_file( (mz_zip_archive*) zip_archive, dataFileName, 0 ); zip_archive_filename = strdup( dataFileName ); }
//--------------------------------------------------------------------------- ZipFile::ZipFile( const char* zipFilePath ) { memset( &m_zipArchive, 0, sizeof( mz_zip_archive ) ); mz_bool success = mz_zip_reader_init_file( &m_zipArchive, zipFilePath, 0 ); if ( !success ) { DebuggerPrintf( "Zip file creation failed!\n" ); } }
static int Lzip_read_file(lua_State *L) { const char *filename = luaL_checkstring(L, 1); mz_uint32 flags = (mz_uint32)luaL_optinteger(L, 2, 0); mz_zip_archive *za = lua_newuserdata(L, sizeof(mz_zip_archive)); mz_zip_zero_struct(za); if (!mz_zip_reader_init_file(za, filename, flags)) return lmz_zip_pusherror(L, za, filename); luaL_setmetatable(L, LMZ_ZIP_READER); return 1; }
BYTE uncomp_zip_control_in_archive(void) { mz_zip_archive zip_archive; int a, mode; memset(&zip_archive, 0, sizeof(zip_archive)); if (!mz_zip_reader_init_file(&zip_archive, info.rom_file, 0)) { fprintf(stderr, "mz_zip_reader_init_file() failed!\n"); return (EXIT_ERROR); } for (mode = UNCOMP_CTRL_FILE_COUNT_ROMS; mode <= UNCOMP_CTRL_FILE_SAVE_DATA; mode++) { uncomp.files_founded = 0; for (a = 0; a < (int) mz_zip_reader_get_num_files(&zip_archive); a++) { mz_zip_archive_file_stat file_stat; int b; if (!mz_zip_reader_file_stat(&zip_archive, a, &file_stat)) { fprintf(stderr, "mz_zip_reader_file_stat() failed!\n"); mz_zip_reader_end(&zip_archive); return (EXIT_ERROR); } /* se e' una directory continuo */ if (mz_zip_reader_is_file_a_directory(&zip_archive, a)) { continue; } for (b = 0; b < LENGTH(format_supported); b++) { char *ext = strrchr(file_stat.m_filename, '.'); if ((ext != NULL) && (strcasecmp(ext, format_supported[b].ext) == 0)) { if (mode == UNCOMP_CTRL_FILE_SAVE_DATA) { uncomp.file[uncomp.files_founded].num = file_stat.m_file_index; uncomp.file[uncomp.files_founded].format = format_supported[b].id; } uncomp.files_founded++; break; } } } if ((mode == UNCOMP_CTRL_FILE_COUNT_ROMS) && (uncomp.files_founded > 0)) { uncomp.file = (_uncomp_file_data *) malloc( uncomp.files_founded * sizeof(_uncomp_file_data)); } } mz_zip_reader_end(&zip_archive); return (EXIT_OK); }
JNIEXPORT void JNICALL Java_com_ktxsoftware_kt_KtLib_step(JNIEnv* env, jobject obj) { if (!initialized) { ++debuggingDelayCount; if (debuggingDelayCount > debuggingDelay) { memset(&apk, 0, sizeof(apk)); mz_bool status = mz_zip_reader_init_file(&apk, theApkPath, 0); if (!status) { return; } init(); Kt::Scheduler::executeFrame(); } } else Kt::Scheduler::executeFrame(); }
BYTE uncomp_zip_name_file_compress(_uncomp_file_data *file) { mz_zip_archive zip_archive; memset(&zip_archive, 0, sizeof(zip_archive)); if (!mz_zip_reader_init_file(&zip_archive, info.rom_file, 0)) { fprintf(stderr, "mz_zip_reader_init_file() failed!\n"); return (EXIT_ERROR); } mz_zip_reader_get_filename(&zip_archive, file->num, uncomp.buffer, sizeof(uncomp.buffer)); // Close the archive, freeing any resources it was using mz_zip_reader_end(&zip_archive); return (EXIT_OK); }
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; }
FileSourceZip::FileSourceZip(const std::string &zipPath) : FileSource(zipPath), m_archive(0) { mz_zip_archive *zip = reinterpret_cast<mz_zip_archive*>(std::calloc(1, sizeof(mz_zip_archive))); if (!mz_zip_reader_init_file(zip, zipPath.c_str(), 0)) { printf("FileSourceZip: unable to open '%s'\n", zipPath.c_str()); std::free(zip); return; } mz_zip_archive_file_stat zipStat; Uint32 numFiles = mz_zip_reader_get_num_files(zip); for (Uint32 i = 0; i < numFiles; i++) { if (mz_zip_reader_file_stat(zip, i, &zipStat)) { bool is_dir = mz_zip_reader_is_file_a_directory(zip, i); if (!mz_zip_reader_is_file_encrypted(zip, i)) AddFile(zipStat.m_filename, FileStat(i, zipStat.m_uncomp_size, MakeFileInfo(zipStat.m_filename, is_dir ? FileInfo::FT_DIR : FileInfo::FT_FILE))); } } m_archive = reinterpret_cast<void*>(zip); }
bool fe_zip_get_dir( const char *archive, std::vector<std::string> &result ) { mz_zip_archive zip; memset( &zip, 0, sizeof( zip ) ); if ( !mz_zip_reader_init_file( &zip, archive, 0 ) ) { std::cerr << "Error initializing zip: " << archive << std::endl; return false; } for ( int i=0; i<(int)mz_zip_reader_get_num_files(&zip); i++) { mz_zip_archive_file_stat file_stat; if ( mz_zip_reader_file_stat(&zip, i, &file_stat) ) result.push_back( file_stat.m_filename ); } mz_zip_reader_end( &zip ); return true; }
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; }
bool fe_zip_open_to_buff( const char *archive, const char *filename, FE_ZIP_ALLOC_CALLBACK callback, void **buff, size_t *buff_size ) { ASSERT( buff != NULL ); mz_zip_archive zip; memset( &zip, 0, sizeof( zip ) ); if ( !mz_zip_reader_init_file( &zip, archive, 0 ) ) { std::cerr << "Error initializing zip. zip: " << archive << std::endl; return false; } int index = mz_zip_reader_locate_file( &zip, filename, NULL, 0 ); if ( index < 0 ) { std::cerr << "Error locating file. zip: " << archive << ", file: " << filename << std::endl; mz_zip_reader_end( &zip ); return false; } mz_zip_archive_file_stat file_stat; if ( !mz_zip_reader_file_stat(&zip, index, &file_stat) ) { std::cerr << "Error reading filestats. zip: " << archive << ", file: " << filename << std::endl; mz_zip_reader_end( &zip ); return false; } void *tb = callback( file_stat.m_uncomp_size ); if ( tb == NULL ) { std::cerr << "Error allocating zip buffer. zip: " << archive << ", file: " << filename << std::endl; mz_zip_reader_end( &zip ); return false; } if ( !mz_zip_reader_extract_to_mem( &zip, index, tb, file_stat.m_uncomp_size, 0 ) ) { std::cerr << "Error extracting to buffer. zip: " << archive << ", file: " << filename << std::endl; mz_zip_reader_end( &zip ); delete (char *)tb; return false; } mz_zip_reader_end( &zip ); *buff = tb; if ( buff_size ) *buff_size = file_stat.m_uncomp_size; return true; }
int main(int argc,char *argv[]) { if(argc != 4) { fprintf(stderr,"USAGE: %s extract <saz> <bin>\n",argv[0]); return 1; } const char *inPath = argv[2]; const char *outPath = argv[3]; mz_zip_archive zip={0}; if(!mz_zip_reader_init_file(&zip,inPath,MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) { fprintf(stderr,"Failed to open '%s' for reading or invalid archive.\n",inPath); return 1; } FILE *outFile = fopen(outPath,"wb"); if(outFile == NULL) { fprintf(stderr,"Failed to open '%s' for writing.\n",outPath); return 1; } int files = mz_zip_reader_get_num_files(&zip); for(int i=0;i<files;i++) { char filename[1024]; if(!mz_zip_reader_get_filename(&zip,i,filename,sizeof(filename))) { continue; } int httpIdx = -1; char ext[1024]; if(sscanf(filename,"raw/%d_c.%s",&httpIdx,ext) != 2) { continue; } size_t postSize; char *post = (char *)mz_zip_reader_extract_to_heap(&zip,i,&postSize,0); if(post == NULL) { continue; } char *data = NULL; int nl=0; for(size_t j=0;j<postSize;j++) { if(post[j] == '\n' || post[j] == '\r') { nl++; if(nl == 4) { data = post+j+1; break; } } else { nl = 0; } } if(data && postSize-(data-post)) { fwrite(data,1,postSize-(data-post),outFile); } printf("%s\n",filename); free(post); } fclose(outFile); return 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; }