简体   繁体   English

使用 C++ 中的宽字符串文件路径从 zip 中提取文件

[英]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?如何通过使用宽字符串文件路径打开 zip 从 zip 读取文件? 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.我只看到带有std::stringconst char *文件路径的库和代码示例,但我想它们可能会在 Windows 上以非 ASCII 字符失败。 I found this but I'm not using gzip .我找到了这个,但我没有使用gzip

Attempts尝试

minizip : 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

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. Zipper似乎也不支持宽字符串。 Is there any way it can currently be done?目前有什么办法可以做到吗?

You might be in luck with minizip.你可能会幸运地使用 minizip。 I haven't tested this, but I found the following code in mz_strm_os_win32.c :我没有对此进行测试,但我在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.所以它看起来非常像作者明确迎合了 Windows 缺乏对“窄字符串”文件 IO 函数的内置 UTF-8 支持。 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.至少值得一试,我们只希望 function 在您尝试打开 zip 文件时实际被调用。

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 .关于Minizip库, API function unzOpen unzOpen()仅在Unix系统上适用于 UTF-8,但在Windows上,路径将仅在当前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.要获得完整的 Unicode支持,需要使用新的 API 函数unzOpen2_64()zipOpen2_64() ,它们允许传递带有一组函数的结构以处理文件系统。 Please see my answer with details in the similar question.请在类似问题中查看我的详细回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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