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
to char 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 later
Had to set buffer with malloc to avoid the error, now it works
static void extract_book(zip_t *book, char *bookname)
{
int size = strlen(pubdir) + strlen(bookname) + 1;
char pubpath[size];
strncat(strncat(strcpy(pubpath, pubdir), bookname, strlen(bookname)), "/", 2);
GFile *dir = g_file_new_for_path(pubpath);
g_file_make_directory_with_parents(dir, NULL, NULL);
for (int index = 0; index < zip_get_num_files(book); index++)
{
zip_file_t *contentfile = zip_fopen_index(book, index, 0);
zip_stat_t fileinfo;
int status = zip_stat_index(book, index, ZIP_FL_ENC_GUESS, &fileinfo);
if (status == 0)
{
char *buffer = malloc(fileinfo.size);
char filepath[size + strlen(fileinfo.name)];
strcat(strcpy(filepath, pubpath), fileinfo.name);
zip_fread(contentfile, buffer, fileinfo.size);
zip_fclose(contentfile);
FILE *file = fopen(filepath, "wb");
fwrite(buffer, fileinfo.size, 1, file);
fclose(file);
}
else
{
printf("%s at %i\n", puberror, zip_get_error(book)->zip_err);
}
}
}