2012-07-25 17:03:01 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
|
|
|
|
* Copyright (C) 2011-2012 - Daniel De Matteis
|
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "../boolean.h"
|
|
|
|
|
2012-07-27 14:10:35 +00:00
|
|
|
#ifdef HAVE_LIBRETRO_MANAGEMENT
|
2012-07-28 13:54:35 +00:00
|
|
|
#include "rarch_console_libretro_mgmt.h"
|
2012-07-27 14:10:35 +00:00
|
|
|
#endif
|
|
|
|
|
2012-07-28 13:54:35 +00:00
|
|
|
#include "rarch_console_rzlib.h"
|
2012-07-25 17:03:01 +00:00
|
|
|
|
2012-07-28 17:17:34 +00:00
|
|
|
static int rarch_extract_currentfile_in_zip(unzFile uf, const char *current_dir, char *slash, char *write_filename, size_t write_filename_size, unsigned extract_zip_mode)
|
2012-07-25 17:03:01 +00:00
|
|
|
{
|
|
|
|
char filename_inzip[PATH_MAX];
|
|
|
|
FILE *file_out = NULL;
|
|
|
|
|
|
|
|
unz_file_info file_info;
|
|
|
|
int err = unzGetCurrentFileInfo(uf,
|
|
|
|
&file_info, filename_inzip, sizeof(filename_inzip),
|
|
|
|
NULL, 0, NULL, 0);
|
|
|
|
|
|
|
|
if (err != UNZ_OK)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Error %d while trying to get ZIP file information.\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size_buf = WRITEBUFFERSIZE;
|
|
|
|
void *buf = malloc(size_buf);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Error allocating memory for ZIP extract operation.\n");
|
|
|
|
return UNZ_INTERNALERROR;
|
|
|
|
}
|
|
|
|
|
2012-07-28 17:17:34 +00:00
|
|
|
switch(extract_zip_mode)
|
2012-07-26 11:08:08 +00:00
|
|
|
{
|
|
|
|
case ZIP_EXTRACT_TO_CURRENT_DIR:
|
2012-07-27 12:31:16 +00:00
|
|
|
case ZIP_EXTRACT_TO_CURRENT_DIR_AND_LOAD_FIRST_FILE:
|
|
|
|
snprintf(write_filename, write_filename_size, "%s%s%s", current_dir, slash, filename_inzip);
|
2012-07-26 11:08:08 +00:00
|
|
|
break;
|
2012-07-25 17:03:01 +00:00
|
|
|
#ifdef HAVE_HDD_CACHE_PARTITION
|
2012-07-26 11:08:08 +00:00
|
|
|
case ZIP_EXTRACT_TO_CACHE_DIR:
|
2012-07-27 12:31:16 +00:00
|
|
|
snprintf(write_filename, write_filename_size, "%s%s", default_paths.cache_dir, filename_inzip);
|
2012-07-26 11:08:08 +00:00
|
|
|
break;
|
2012-07-25 17:03:01 +00:00
|
|
|
#endif
|
2012-07-26 11:08:08 +00:00
|
|
|
}
|
2012-07-25 17:03:01 +00:00
|
|
|
|
|
|
|
err = unzOpenCurrentFile(uf);
|
|
|
|
if (err != UNZ_OK)
|
|
|
|
RARCH_ERR("Error %d while trying to open ZIP file.\n", err);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* success */
|
|
|
|
file_out = fopen(write_filename, "wb");
|
|
|
|
|
|
|
|
if (!file_out)
|
|
|
|
RARCH_ERR("Error opening %s.\n", write_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_out)
|
|
|
|
{
|
|
|
|
RARCH_LOG("Extracting: %s..\n", write_filename);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
err = unzReadCurrentFile(uf, buf, size_buf);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Error %d while reading from ZIP file.\n", err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err > 0)
|
|
|
|
{
|
|
|
|
if (fwrite(buf, err, 1, file_out) != 1)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Error while extracting file(s) from ZIP.\n");
|
|
|
|
err = UNZ_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}while (err > 0);
|
|
|
|
|
|
|
|
if (file_out)
|
|
|
|
fclose(file_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == UNZ_OK)
|
|
|
|
{
|
|
|
|
err = unzCloseCurrentFile (uf);
|
|
|
|
if (err != UNZ_OK)
|
|
|
|
RARCH_ERR("Error %d while trying to close ZIP file.\n", err);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
unzCloseCurrentFile(uf);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-07-28 17:17:34 +00:00
|
|
|
int rarch_extract_zipfile(const char *zip_path, const char *current_dir, char *first_file, size_t first_file_size, unsigned extract_zip_mode)
|
2012-07-25 17:03:01 +00:00
|
|
|
{
|
2012-07-27 12:31:16 +00:00
|
|
|
bool found_first_file = false;
|
2012-07-27 14:10:35 +00:00
|
|
|
(void)found_first_file;
|
2012-07-25 17:03:01 +00:00
|
|
|
unzFile uf = unzOpen(zip_path);
|
|
|
|
|
|
|
|
unz_global_info gi;
|
|
|
|
int err = unzGetGlobalInfo(uf, &gi);
|
|
|
|
if (err != UNZ_OK)
|
|
|
|
RARCH_ERR("Error %d while trying to get ZIP file global info.\n",err);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < gi.number_entry; i++)
|
|
|
|
{
|
2012-07-27 12:31:16 +00:00
|
|
|
static char write_filename[PATH_MAX];
|
|
|
|
char slash[6];
|
|
|
|
#ifdef _XBOX
|
2012-07-27 12:59:45 +00:00
|
|
|
snprintf(slash, sizeof(slash), "\\");
|
2012-07-27 12:31:16 +00:00
|
|
|
#else
|
|
|
|
snprintf(slash, sizeof(slash), "/");
|
|
|
|
#endif
|
2012-07-28 17:17:34 +00:00
|
|
|
if (rarch_extract_currentfile_in_zip(uf, current_dir, slash, write_filename, sizeof(write_filename), extract_zip_mode) != UNZ_OK)
|
2012-07-27 12:31:16 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to extract current file from ZIP archive.\n");
|
2012-07-25 17:03:01 +00:00
|
|
|
break;
|
2012-07-27 12:31:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-27 14:10:35 +00:00
|
|
|
#ifdef HAVE_LIBRETRO_MANAGEMENT
|
2012-07-27 12:31:16 +00:00
|
|
|
if(!found_first_file)
|
|
|
|
{
|
|
|
|
found_first_file = rarch_manage_libretro_extension_supported(write_filename);
|
|
|
|
|
|
|
|
if(found_first_file)
|
|
|
|
snprintf(first_file, first_file_size, write_filename);
|
|
|
|
}
|
2012-07-27 14:10:35 +00:00
|
|
|
#endif
|
2012-07-27 12:31:16 +00:00
|
|
|
}
|
2012-07-25 17:03:01 +00:00
|
|
|
|
|
|
|
if ((i + 1) < gi.number_entry)
|
|
|
|
{
|
|
|
|
err = unzGoToNextFile(uf);
|
|
|
|
if (err != UNZ_OK)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Error %d while trying to go to the next file in the ZIP archive.\n",err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|