Error "file is not Zlib-compressed"

Hello. I’m at the finish line of #JT4 (Create a blob object) but I get the following error when running the test:

remote: [your_program] Logs from your program will appear here!
remote: [your_program] Initialized git directory
remote: ^[[33m[tester::#JT4] $ echo "pineapple blueberry pear banana grape apple" > mango.txt
remote: [tester::#JT4] $ ./your_program.sh hash-object -w mango.txt
remote: [your_program] Logs from your program will appear here!
remote: [your_program] f742add8b2b3797df807f6ec86072a5e166feaef
remote: [tester::#JT4] Output is a 40-char SHA.
remote: [tester::#JT4] ^[[0mThe file at ".git/objects/f7/42add8b2b3797df807f6ec86072a5e166feaef" is not Zlib-compressed

This is my compression code:

    z_stream zstream;
    zstream.next_in = (Byte*)uncmpData.data();
    zstream.avail_in = uncmpData.size();
    // zstream.next_out     BELOW
    // zstream.avail_out    BELOW
    zstream.zalloc = Z_NULL;
    zstream.zfree = Z_NULL;

    deflateInit(&zstream, Z_DEFAULT_COMPRESSION);

    const uLong maxSize = deflateBound(&zstream, uncmpData.size());

    std::string out;
    out.resize(maxSize);
    zstream.next_out = (Byte*)out.data();
    zstream.avail_out = maxSize;

    deflate(&zstream, Z_FINISH);
    deflateEnd(&zstream);

    // out.resize(out.size() - zstream.total_out);
    // out.resize(zstream.total_out);

    return out;

I’ve tried with and without the out.resize() options (I saw other people were doing it) without change. Everything else seems to be working fine (I know there is no error control because I didn’t want to overcomplicate it but I’ve debugged and all the deflate* return no errors)

Hey @Nyerelia, a couple of things might be going wrong here:

  1. The data returned by cmpdData.data() may include null bytes (\0), which can cause the << operator to stop writing early, since it treats the input like a C-style string. To avoid truncation, try writing the data using zipFile.write instead.

  2. "\0" is a string with two characters, not a null byte. You’ll want to use '\0'.

Let me know if you’d like any further clarification!

1 Like

That was it! I missed the .data() ending when finding a \0. Thank you kindly @andy1li!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.