确定文件是否为gzip文件 如果打开的文件是GZIP文件,我需要使用C++检查。 在Python中,我使用以下代码来标识文件是否已gzip: test_file = "junk.txt.gz" with open(test_file, "rb") as f: f_read_first_two_bytes = f.read(2) if f_read_first_two_bytes==b'\x1f\x8b': print("The file is a gzipped file", end='\n') < C++中的等价物是什么?< /P> C++版本的输出是什么?为了保证,你也可以检查第三字节是8。C++版本的输出是什么?为了保证额外的安全性,你也可以检查第三字节是8。而不是读到 char [/COND>数组并将其铸造为比较,你应该读到未签名的字符[]。buffer并在读取期间强制转换它:unsigned char p[2];is.read(reinterpret_cast(p),2);if(p[0]==0x1f&&p[1]==0x8b)而不是读入char[]数组并强制转换以进行比较,您应该读入unsigned char[]缓冲区,并在读取期间强制转换它:unsigned char p[2];is.read(重新解释转换(p),2);如果(p[0]==0x1f和&p[1]==0x8b)

确定文件是否为gzip文件 如果打开的文件是GZIP文件,我需要使用C++检查。 在Python中,我使用以下代码来标识文件是否已gzip: test_file = "junk.txt.gz" with open(test_file, "rb") as f: f_read_first_two_bytes = f.read(2) if f_read_first_two_bytes==b'\x1f\x8b': print("The file is a gzipped file", end='\n') < C++中的等价物是什么?< /P> C++版本的输出是什么?为了保证,你也可以检查第三字节是8。C++版本的输出是什么?为了保证额外的安全性,你也可以检查第三字节是8。而不是读到 char [/COND>数组并将其铸造为比较,你应该读到未签名的字符[]。buffer并在读取期间强制转换它:unsigned char p[2];is.read(reinterpret_cast(p),2);if(p[0]==0x1f&&p[1]==0x8b)而不是读入char[]数组并强制转换以进行比较,您应该读入unsigned char[]缓冲区,并在读取期间强制转换它:unsigned char p[2];is.read(重新解释转换(p),2);如果(p[0]==0x1f和&p[1]==0x8b),python,c++,gzip,Python,C++,Gzip,您可以安装libmagic库,其中包含不同文件类型的大量指纹,而不是自己执行指纹检查 Ubuntu:apt安装libmagic dev,Fedora:dnf安装文件devel-或从 检查您在命令行上提供的文件的示例程序: #include <magic.h> #include <filesystem> #include <iostream> #include <memory> #include <stdexcept> #include

您可以安装
libmagic
库,其中包含不同文件类型的大量指纹,而不是自己执行指纹检查

Ubuntu:
apt安装libmagic dev
,Fedora:
dnf安装文件devel
-或从

检查您在命令行上提供的文件的示例程序:

#include <magic.h>

#include <filesystem>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>

// A small class to manage the libmagic "cookie"
class MagicCookie {
public:
    // MAGIC_MIME - Return a MIME type string, instead of a textual description.
    MagicCookie() : cookie(magic_open(MAGIC_MIME), &magic_close) {
        if(not cookie)
            throw std::runtime_error("unable to initialize magic library");

        if(magic_load(cookie.get(), nullptr)) {
            throw std::runtime_error(std::string("cannot load magic database: ") +
                                     magic_error(cookie.get()));
        }
    }

    // A function that checks a file and returns its MIME type
    const char* File(const std::filesystem::path& file) {
        return magic_file(cookie.get(), file.string().c_str());
    }

private:
    std::unique_ptr<std::remove_pointer_t<magic_t>, decltype(&magic_close)> cookie;
};

int main(int argc, char* argv[]) {
    MagicCookie mc;

    // Find the MIME type for all files given on the command line:
    for(int idx = 1; idx < argc; ++idx) {
        std::cout << argv[idx] << ": MIME: " << mc.File(argv[idx]) << '\n';
    }
}
如果
MIME
不是您想要的,可以在此处找到其他打开模式:。如果需要,它甚至可以分析压缩文件的内容

编译时使用:

-std=c++17 -lmagic

您可以安装
libmagic
库,其中包含不同文件类型的大量指纹,而不是自己执行指纹检查

Ubuntu:
apt安装libmagic dev
,Fedora:
dnf安装文件devel
-或从

检查您在命令行上提供的文件的示例程序:

#include <magic.h>

#include <filesystem>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>

// A small class to manage the libmagic "cookie"
class MagicCookie {
public:
    // MAGIC_MIME - Return a MIME type string, instead of a textual description.
    MagicCookie() : cookie(magic_open(MAGIC_MIME), &magic_close) {
        if(not cookie)
            throw std::runtime_error("unable to initialize magic library");

        if(magic_load(cookie.get(), nullptr)) {
            throw std::runtime_error(std::string("cannot load magic database: ") +
                                     magic_error(cookie.get()));
        }
    }

    // A function that checks a file and returns its MIME type
    const char* File(const std::filesystem::path& file) {
        return magic_file(cookie.get(), file.string().c_str());
    }

private:
    std::unique_ptr<std::remove_pointer_t<magic_t>, decltype(&magic_close)> cookie;
};

int main(int argc, char* argv[]) {
    MagicCookie mc;

    // Find the MIME type for all files given on the command line:
    for(int idx = 1; idx < argc; ++idx) {
        std::cout << argv[idx] << ": MIME: " << mc.File(argv[idx]) << '\n';
    }
}
如果
MIME
不是您想要的,可以在此处找到其他打开模式:。如果需要,它甚至可以分析压缩文件的内容

编译时使用:

-std=c++17 -lmagic
但这显然不是正确的方式

int main() {
        char p[3] = {0};
        p[2] = '\n';
        // open the junk.txt.gz file. We do not want to just go by the '.gz' in the file name.
        // but want to check just like the way we did in the Python code.
        ifstream is("./junk.txt.gz", std::ios::in|std::ios::out|std::ios::binary);
        //read  two characters into p
        is.read(p,2);
        cout << std::hex << p[0] << " " << std::hex << p[1] << endl;
        return 0;
}
显然不是,因为你没有将字节与任何东西进行比较。否则,它和Python程序一样“正确”

进行比较的一种简单方法是将字节解释为
无符号字符

auto up = reinterpret_cast<unsigned char*>(p);
if (up[0] == 0x1f && up[1] == 0x8b)
auto-up=reinterpret\u cast(p);
if(向上[0]==0x1f和向上[1]==0x8b)

另外,这不一定是对gzip文件最准确的测试。它可能有误报

我建议不要尝试手动实现测试。有用于此目的的开源库(与大多数用途的开源库一样)

但这显然不是正确的方式

int main() {
        char p[3] = {0};
        p[2] = '\n';
        // open the junk.txt.gz file. We do not want to just go by the '.gz' in the file name.
        // but want to check just like the way we did in the Python code.
        ifstream is("./junk.txt.gz", std::ios::in|std::ios::out|std::ios::binary);
        //read  two characters into p
        is.read(p,2);
        cout << std::hex << p[0] << " " << std::hex << p[1] << endl;
        return 0;
}
显然不是,因为你没有将字节与任何东西进行比较。否则,它和Python程序一样“正确”

进行比较的一种简单方法是将字节解释为
无符号字符

auto up = reinterpret_cast<unsigned char*>(p);
if (up[0] == 0x1f && up[1] == 0x8b)
auto-up=reinterpret\u cast(p);
if(向上[0]==0x1f和向上[1]==0x8b)

另外,这不一定是对gzip文件最准确的测试。它可能有误报


我建议不要尝试手动实现测试。有用于此目的的开源库(与大多数用途的开源库一样)< /P> C++版本的输出是什么?为了保证,你也可以检查第三字节是8。C++版本的输出是什么?为了保证额外的安全性,你也可以检查第三字节是8。而不是读到<代码> char [/COND>数组并将其铸造为比较,你应该读到<代码>未签名的字符[]。buffer并在读取期间强制转换它:
unsigned char p[2];is.read(reinterpret_cast(p),2);if(p[0]==0x1f&&p[1]==0x8b)
而不是读入
char[]
数组并强制转换以进行比较,您应该读入
unsigned char[]
缓冲区,并在读取期间强制转换它:
unsigned char p[2];is.read(重新解释转换(p),2);如果(p[0]==0x1f和&p[1]==0x8b)