如何为 7z 存档格式编写二进制数据? [英] How do I write binary data for 7z archive format?

查看:32
本文介绍了如何为 7z 存档格式编写二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在倾注 7z 存档格式的格式描述和源代码,但我仍然无法编写有效的容器.我假设我可以创建一个空容器......无论如何这是我的开始:

I've been pouring over the format description and source code for the 7z archive format, but I'm still having trouble writing a valid container. I assume I can create an empty container... anyway here's my start:

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() 的理解.字节是一个无符号字符",UInt64 &UInt32 都是 'unsigned long'.

I think my main problem is a lack of understanding of std::ofstream::write(). Byte is an 'unsigned char', UInt64 & UInt32 are both 'unsigned long'.

UPDATE0:正如大家所指出的,如果我在大端机器上运行它会出现问题.这不是这里的情况.根据 Fredrik Janssen,我应该投射非数组的地址.我还应该提到 CrcCalc() 是 LZMA SDK 中的一个函数.添加 &有点帮助,这是第一个 unsigned char[6] 有一些问题.

UPDATE0: As everyone points out, it'd be a problem if I ran this on a big-endian machine. That's not the case here. Per Fredrik Janssen, I should be casting the address of the non-arrays. I should also mention that CrcCalc() is a function in the LZMA SDK. Adding & helps a bit, it's that first unsigned char[6] that's having some problems.

UPDATE1:用于在下方获取空存档文件的工作代码.

UPDATE1: Working code to get an empty archive file below.

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.

NOTE: CrcGenerateTable() and CrcCalc() are from the LZMA SDK.

推荐答案

不知道 7z 的格式,但是我注意到当你写下 offset、size 和 crc 时,这些会以 little-endian 写入文件格式(我假设你有一个小端 CPU).

don't know the format of 7z, but I notice when you write down offset, size and crc that these will be written to the file in little-endian format (I assume you have a little-endian CPU).

可能更糟的是,您错过了 &在主要、次要、偏移、大小和 crc 之前,即将实际值转换为指针.

An probably worse, you are missing the & before major, minor, offset, size and crc, i.e. you are casting the actual values to a pointer.

这篇关于如何为 7z 存档格式编写二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆