char *get_contents_zip(const char *path, const char *name, time_t *last_modified) { zip_t *archive = zip_open(path, ZIP_RDONLY, NULL); if (archive == NULL) { char buffer[1024]; snprintf(buffer, 1024, "Could not open %s\n", path); engine_print(buffer); return NULL; } zip_stat_t stat; if (zip_stat(archive, name, 0, &stat) < 0) { goto close_archive; } zip_file_t *f = zip_fopen(archive, name, 0); if (f == NULL) { print_zip_err("zip_fopen", archive); goto close_archive; } if (last_modified != NULL) { *last_modified = stat.mtime; } char *buf = malloc(stat.size + 1); if (!buf) { engine_println("zip malloc"); goto close_f; } if (zip_fread(f, buf, stat.size) < 0) { print_zip_err("zip_fread", archive); goto free_buf; } buf[stat.size] = '\0'; zip_fclose(f); zip_close(archive); return buf; free_buf: free(buf); close_f: zip_fclose(f); close_archive: zip_close(archive); return NULL; }
char *get_contents_zip(char *path, char *name, time_t *last_modified) { zip_t *archive = zip_open(path, ZIP_RDONLY, NULL); if (archive == NULL) { print_zip_err("zip_open", archive); return NULL; } zip_stat_t stat; if (zip_stat(archive, name, 0, &stat) < 0) { print_zip_err("zip_stat", archive); goto close_archive; } zip_file_t *f = zip_fopen(archive, name, 0); if (f == NULL) { print_zip_err("zip_fopen", archive); goto close_archive; } if (last_modified != NULL) { *last_modified = stat.mtime; } char *buf = malloc(stat.size + 1); if (zip_fread(f, buf, stat.size) < 0) { print_zip_err("zip_fread", archive); goto free_buf; } buf[stat.size] = '\0'; zip_fclose(f); zip_close(archive); return buf; free_buf: free(buf); zip_fclose(f); close_archive: zip_close(archive); return NULL; }