# Error "file is not Zlib-compressed"

**URL:** <https://forum.codecrafters.io/t/error-file-is-not-zlib-compressed/15422>\
**Category:** Challenges\
**Tags:** challenge:git\
**Created:** [October 21, 2025, 11:17am UTC](https://forum.codecrafters.io/t/error-file-is-not-zlib-compressed/15422 "2025-10-21T11:17:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nyerelia](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/nyerelia/32/17058_2.png) [@Nyerelia](https://forum.codecrafters.io/u/Nyerelia)\
**Post date:** [October 21, 2025, 11:17am UTC](https://forum.codecrafters.io/t/error-file-is-not-zlib-compressed/15422/1 "2025-10-21T11:17:32Z")

</div>

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

```auto
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:

```python
    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)

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 21, 2025, 4:06pm UTC](https://forum.codecrafters.io/t/error-file-is-not-zlib-compressed/15422/4 "2025-10-21T16:06:25Z")

</div>

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'`.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/b/a/ba770afbc2bf8960bc1faf1fb99016975d0d895c.png)

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

---

<div class="post-metadata">

**Author:** ![Nyerelia](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/nyerelia/32/17058_2.png) [@Nyerelia](https://forum.codecrafters.io/u/Nyerelia)\
**Post date:** [October 22, 2025, 8:46am UTC](https://forum.codecrafters.io/t/error-file-is-not-zlib-compressed/15422/6 "2025-10-22T08:46:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [October 27, 2025, 8:47am UTC](https://forum.codecrafters.io/t/error-file-is-not-zlib-compressed/15422/7 "2025-10-27T08:47:10Z")

</div>

Closed on October 27, 2025, 8:47am UTC

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