static bool create_ios_lproj_dirs(const char *zipname, const char *dir, export_config *conf) { const char *ptr = conf->supported_languages; char lang[100]; while (*ptr == ' ') ptr++; while (*ptr != 0) { int i = 0; while (*ptr != 0 && *ptr != ',') { if (i >= 100) { fprintf(stderr, "conf.lua: language id too long\n"); return false; } lang[i] = *ptr; i++; ptr++; } lang[i] = 0; char *name = am_format("%s/%s.lproj/x", dir, lang); if (!mz_zip_add_mem_to_archive_file_in_place(zipname, name, "x", 1, "", 0, 0, 0100644, ZIP_PLATFORM_UNIX)) { free(name); return false; } free(name); while (*ptr == ',' || *ptr == ' ') ptr++; } return true; }
static bool add_files_to_zip_renamed(const char *zipfile, const char *dir, const char *pat, const char *newdir, const char *newname, const char *newext, bool compress, bool executable, uint8_t platform) { CSimpleGlobTempl<char> glob(SG_GLOB_ONLYFILE); char *pattern = am_format("%s%c%s", dir, AM_PATH_SEP, pat); glob.Add(pattern); free(pattern); for (int n = 0; n < glob.FileCount(); ++n) { char *file = glob.File(n); size_t len; void *buf = am_read_file(file, &len); if (buf == NULL) return false; char *name; if (newname == NULL) { name = am_format("%s/%s", newdir, file + strlen(dir) + 1); } else if (newext == NULL) { name = am_format("%s/%s", newdir, newname); } else { name = am_format("%s/%s%s", newdir, newname, newext); } if (!mz_zip_add_mem_to_archive_file_in_place(zipfile, name, buf, len, "", 0, compress ? MZ_BEST_COMPRESSION : 0, executable ? 0100755 : 0100644, platform)) { free(buf); free(name); fprintf(stderr, "Error: failed to add %s to archive\n", file); return false; } free(name); free(buf); } return true; }
bool ZipIO::writeFile(const void *Data, U64 DataSize, bool Compress) { if (!_kisopen || !_kready || _kmode != OpenMode::WRITE && _kmode != OpenMode::APPEND) return 0; mz_uint cmpType = MZ_BEST_COMPRESSION; if (!Compress) cmpType = MZ_NO_COMPRESSION; return mz_zip_add_mem_to_archive_file_in_place(_kaname.c_str(), _kfname.c_str(), Data, (size_t)DataSize, "no comment", (U16)strlen("no comment"), cmpType); }
static bool add_matching_files_to_zip(const char *zipfile, const char *rootdir, const char *dir, const char *pat, bool compress, uint8_t platform) { CSimpleGlobTempl<char> glob(SG_GLOB_ONLYFILE); char *pattern = am_format("%s%c%s", dir, AM_PATH_SEP, pat); glob.Add(pattern); free(pattern); for (int n = 0; n < glob.FileCount(); ++n) { char *file = glob.File(n); size_t len; void *buf = am_read_file(file, &len); if (buf == NULL) return false; if (!mz_zip_add_mem_to_archive_file_in_place(zipfile, file + strlen(rootdir) + 1, buf, len, "", 0, compress ? MZ_BEST_COMPRESSION : 0, 0100644, platform)) { free(buf); fprintf(stderr, "Error: failed to add %s to archive\n", file); return false; } free(buf); } 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; }
void dataFile::addDataFileFromHeap( const std::string &filename, size_t inFileSize, void* mem ) { mz_zip_add_mem_to_archive_file_in_place( zip_archive_filename, filename.c_str(), mem, inFileSize, NULL, 0, MZ_BEST_COMPRESSION); }
gboolean Distro_WindowsCreate( const gchar * project_title, const gchar * game_path ) { gchar * blank_buffer = g_new(gchar, 512); gchar * game_buffer = NULL; gchar * binary_buffer = NULL; gsize game_buffer_length = 0; gsize binary_buffer_length = 0; gsize bytes_read = 0; gzFile binary_original; FILE * binary_temporary; gchar * shared_directory = Meg_Directory_Share("binaries"); gchar * binary_original_path; gchar * binary_temporary_path; gchar * target_name; gchar * zip_original_path; gchar * zip_target_path; if ( g_file_get_contents( game_path, &game_buffer, &game_buffer_length, NULL ) ) { target_name = g_strdup_printf("%s.exe", project_title ); binary_original_path = g_build_filename( shared_directory, "windows-exe.gz", NULL); binary_temporary_path = g_build_filename( g_get_tmp_dir(), target_name, NULL); zip_original_path = g_build_filename( shared_directory, "windows-dll.zip", NULL); zip_target_path = g_strdup_printf( "%s"G_DIR_SEPARATOR_S"%s-windows.zip", Meg_Directory_Document(), project_title ); binary_original = gzopen( binary_original_path, "rb" ); binary_temporary = g_fopen( binary_temporary_path, "wb" ); g_print( "binary_original: %p\n", binary_original ); g_print( "binary_temporary: %p\n", binary_temporary ); g_print( "binary_original_path: %s\n", binary_original_path ); g_print( "binary_temporary_path: %s\n", binary_temporary_path ); g_print( "zip_original_path: %s\n", zip_original_path ); g_print( "zip_target_path: %s\n", zip_target_path ); if ( binary_original && binary_temporary ) { while ( TRUE ) // Read gzip { bytes_read = gzread( binary_original, blank_buffer, 512); if ( bytes_read > 0 ) { fwrite( blank_buffer, bytes_read, 1, binary_temporary ); } else { break; } } fwrite( game_buffer, game_buffer_length, 1, binary_temporary ); if ( g_file_get_contents( binary_temporary_path, &binary_buffer, &binary_buffer_length, NULL ) ) { Meg_FileCopy( zip_original_path, zip_target_path ); mz_zip_add_mem_to_archive_file_in_place( zip_target_path, target_name, binary_buffer, binary_buffer_length, NULL, 0, 0 ); } g_free(binary_buffer); } else { g_warning("Distro_WindowsCreate Failed"); } gzclose(binary_original); fclose(binary_temporary); g_free(game_buffer); g_free(zip_original_path); g_free(zip_target_path); g_free(binary_temporary_path); g_free(binary_original_path); return TRUE; } return FALSE; }