Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 121783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:01:31+00:00 2026-05-11T04:01:31+00:00

I’ve been pouring over the format description and source code for the 7z archive

  • 0

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(); 

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: 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: 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(); } 

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:01:31+00:00Added an answer on May 11, 2026 at 4:01 am

    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).

    Edit: 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.