0

How can you read a file from a zip by opening the zip with a wide string file path? I only saw libraries and code examples with std::string or const char * file paths but I suppose they may fail on Windows with non-ASCII characters. I found this but I'm not using gzip.

Attempts

minizip:

const auto zip_file = unzOpen(jar_file_path.string().c_str()); // No wide string support
if (zip_file == nullptr)
{
    throw std::runtime_error("unzOpen() failed");
}

libzippp:

libzippp::ZipArchive zip_archive(jar_file_path.string()); // No wide string support
const auto file_opened_successfully = zip_archive.open(libzippp::ZipArchive::ReadOnly);
if (!file_opened_successfully)
{
    throw std::runtime_error("Failed to open the archive file");
}

Zipper does not seem to support wide strings either. Is there any way it can currently be done?

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • I don't know if this may help: https://stackoverflow.com/a/14628171/260313 (minizip), and https://stackoverflow.com/a/9746912/260313 (zlib). – rturrado Feb 17 '22 at 20:25

2 Answers2

1

You might be in luck with minizip. I haven't tested this, but I found the following code in mz_strm_os_win32.c:

int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode) {

...

    path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
    if (path_wide == NULL)
        return MZ_PARAM_ERROR;

#ifdef MZ_WINRT_API
    win32->handle = CreateFile2(path_wide, desired_access, share_mode,
        creation_disposition, NULL);
#else
    win32->handle = CreateFileW(path_wide, desired_access, share_mode, NULL,
        creation_disposition, flags_attribs, NULL);
#endif

    mz_os_unicode_string_delete(&path_wide);

...

So it looks very much as if the author catered explicitly for Windows' lack of built-in UTF-8 support for the 'narrow string' file IO functions. It's worth a try at least, let's just hope that that function actually gets called when you try to open a zip file.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
0

Regarding Minizip library, API function unzOpen() works well with UTF-8 only on Unix systems, but on Windows, path will be processed only in the current CodePage. For get full Unicode support, need to use new API functions unzOpen2_64() and zipOpen2_64() that allows to pass structure with set of functions for work with file system. Please see my answer with details in the similar question.

Pavel K.
  • 983
  • 9
  • 21