簡體   English   中英

使用 C++ 中的寬字符串文件路徑從 zip 中提取文件

[英]Extracting file from zip using wide string file path in C++

如何通過使用寬字符串文件路徑打開 zip 從 zip 讀取文件? 我只看到帶有std::stringconst char *文件路徑的庫和代碼示例,但我想它們可能會在 Windows 上以非 ASCII 字符失敗。 我找到了這個,但我沒有使用gzip

嘗試

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似乎也不支持寬字符串。 目前有什么辦法可以做到嗎?

你可能會幸運地使用 minizip。 我沒有對此進行測試,但我在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);

...

所以它看起來非常像作者明確迎合了 Windows 缺乏對“窄字符串”文件 IO 函數的內置 UTF-8 支持。 至少值得一試,我們只希望 function 在您嘗試打開 zip 文件時實際被調用。

關於Minizip庫, API function unzOpen unzOpen()僅在Unix系統上適用於 UTF-8,但在Windows上,路徑將僅在當前CodePage中處理。 要獲得完整的 Unicode支持,需要使用新的 API 函數unzOpen2_64()zipOpen2_64() ,它們允許傳遞帶有一組函數的結構以處理文件系統。 請在類似問題中查看我的詳細回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM