我一直在研究 7z 存档格式的格式描述和源代码,但在编写有效的容器时仍然遇到困难。我想我可以创建一个空容器......无论如何,这是我的开始:
std::ofstream ofs(archivename.c_str(), std::ios::binary|std::ios::trunc);
Byte signature[6] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
Byte major = 0;
Byte minor = 3;
ofs.write((const char*)signature, 6);
ofs.write((const char*)major, 1);
ofs.write((const char*)minor, 1);
UInt64 offset = 0;
UInt64 size = 0;
UInt32 crc = 0;
ofs.write((const char*)offset, 4);
ofs.write((const char*)size, 8);
ofs.write((const char*)crc, 8);
ofs.write((const char*)CrcCalc(0, 0), 8);
ofs.close();
我认为我的主要问题是缺乏对 std::ofstream::write() 的理解。 Byte 是“unsigned char”,UInt64 和 UInt32 都是“unsigned long”。
UPDATE0:正如每个人都指出的那样,如果我在大端机器上运行它,就会出现问题。这里的情况并非如此。根据 Fredrik Janssen 的说法,我应该转换非数组的地址。我还应该提到 CrcCalc() 是 LZMA SDK 中的一个函数。添加 & 有点帮助,这是第一个 unsigned char[6] 有一些问题。
UPDATE1:用于获取下面的空存档文件的工作代码。
static void SetUInt32(Byte *p, UInt32 d)
{
for (int i = 0; i < 4; i++, d >>= 8)
p[i] = (Byte)d;
}
static void SetUInt64(Byte *p, UInt64 d)
{
for (int i = 0; i < 8; i++, d >>= 8)
p[i] = (Byte)d;
}
void make_7z_archive()
{
CrcGenerateTable();
std::ofstream ofs(archivename.c_str(), std::ios::binary|std::ios::trunc);
Byte signature[6] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C};
Byte major = 0;
Byte minor = 3;
ofs.write((const char*)signature, 6);
ofs.write((const char*)&major, 1);
ofs.write((const char*)&minor, 1);
UInt64 offset = 0;
UInt64 size = 0;
UInt32 crc = 0;
Byte buf[24];
SetUInt64(buf + 4, offset);
SetUInt64(buf + 12, size);
SetUInt32(buf + 20, crc);
SetUInt32(buf, CrcCalc(buf + 4, 20));
ofs.write((const char*)buf, 24);
ofs.close();
}
注意:CrcGenerateTable() 和 CrcCalc() 来自 LZMA SDK。