简体   繁体   中英

Extracting file from zip using wide string file path in C++

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM