rikyoz / bit7z

A C++ static library offering a clean and simple interface to the 7-zip shared libraries.
https://rikyoz.github.io/bit7z
Mozilla Public License 2.0
602 stars 110 forks source link

[Bug]: Inconsistent encoding of string parameter #217

Closed hcaihao closed 1 month ago

hcaihao commented 1 month ago

bit7z version

4.0.x

Compilation options

BIT7Z_7ZIP_VERSION

7-zip version

v23.01

7-zip shared library used

7z.dll / 7z.so

Compilers

MSVC

Compiler versions

No response

Architecture

x86_64

Operating system

Windows

Operating system versions

Windows 11

Bug description

std::string CompressFiles(const std::vector< std::string >& inPaths, const std::string& outFile)
{
        try
        {
            Bit7zLibrary lib{ "7z.dll" };
            BitFileCompressor compressor{ lib, BitFormat::Zip };
            compressor.setOverwriteMode(OverwriteMode::Overwrite);
            compressor.compress(inPaths, outFile);
        }
        catch (const bit7z::BitException& ex) {
            result = ex.what();
        }

        return result;
}

if inPaths inlcude unicode, std::string "must" be encoded by gbk(), otherwise throw "Invalid path: The system cannot find the path specified."

std::string CompressDirectory(const std::string& inDir, const std::string& outFile)
{
    std::string result;

    try
    {
        Bit7zLibrary lib{ "7z.dll" };
        BitFileCompressor compressor{ lib, BitFormat::Zip };
        compressor.compressDirectory(inDir, outFile);
    }
    catch (const bit7z::BitException& ex) {
        result = ex.what();
    }

    return result;
}

if inDir inlcude unicode, std::string "must" be encoded by utf8, otherwise throw "Invalid path: The system cannot find the path specified."

Steps to reproduce

No response

Expected behavior

No response

Relevant compilation output

No response

Code of Conduct

rikyoz commented 1 month ago

Hi! What version of bit7z are you using?

if inPaths inlcude unicode, std::string "must" be encoded by gbk(), otherwise throw "Invalid path: The system cannot find the path specified."

In the latest v4.0.7, I fixed a similar issue, so this is strange if you happen to use the latest version of the library.

if inDir inlcude unicode, std::string "must" be encoded by utf8, otherwise throw "Invalid path: The system cannot find the path specified."

By default bit7z expects UTF-8 encoded std::strings. So the second example behaves correctly.