My goal is to write file to disk
If I use gio to create the file
GError *error;
char path[strlen(dirpath)];
sprintf(path, "%s", dirpath); // Create path to the file by copying from another variable
zip_file_t *contentfile = zip_fopen_index(book, index, ZIP_RDONLY);
zip_stat_index(book, index, ZIP_CHECKCONS, &fileinfo);
GFile *gfile = g_file_new_for_path(strcat(path, fileinfo.name));
GFileOutputStream *file = g_file_create(gfile, G_FILE_CREATE_NONE, g_cancellable_new(), &error);
if (error)
printf("Error :%i\n", error->code);
else
zip_fread(contentfile, file, fileinfo.size);
g_error_free(error);
I cant put anything in the file. Its just a file created with 0 bytes. I am extracting all files on the archive with a loo. Hence index in zip_file_t *contentfile = zip_fopen_index(book, index, ZIP_RDONLY);
. I am getting a segfault : GError set over the top of a previous GError or uninitialized memory.
If I use FILE
char path[strlen(dirpath)];
sprintf(path, "%s", dirpath); // Create path to the file by copying from another variable
zip_file_t *contentfile = zip_fopen_index(book, index, ZIP_RDONLY);
zip_stat_index(book, index, ZIP_CHECKCONS, &fileinfo);
FILE *file = fopen(strcat(path, fileinfo.name), "wb");
zip_fread(contentfile, file, fileinfo.size);
This gives a segmentation fault with : double free or corruption (out)
How do I properly write zip_read( contents to a file on disk?
@MikeCAT was right I first needed to fix my char path size. Using the same idea I changed my
void *buffer
tochar buffer[size]
and not a pointer. It does save files from some zips some are still giving segmentation error. I will investigate why [probably insufficient memory somewhere] but at least it does work for now. I will update laterHad to set buffer with malloc to avoid the error, now it works