[Python] zlib.error : Error -3 while decompressing data : incorrect header check

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a jpg image that is in memory and therefore is in bytes. I then compress it with the following:

    zlib.compress(pickle.dumps(self.fileInstance))


    I compress it to store it into a MongoDB database. This image is one of the elements in the record that I save. I save the with the following.

    list_search_result = list(search_result)
    json_search_result = json.dumps(list_search_result, cls=Encoder, indent=2)


    I got an error with bytes and Json, so I simply convert the bytes image to a string with the following.

    from bson.objectid import ObjectId
    import json
    import datetime
    class Encoder(json.JSONEncoder):
    def default(self, obj):
    if isinstance(obj, ObjectId):
    return str(obj)
    elif isinstance(obj, datetime.datetime):
    return str(obj)
    elif isinstance(obj,bytes):
    return str(obj)
    return super(Encoder, self).default(obj)


    Now, I want to recover the image from the json file. I thought that doing the same steps in the opposite order would work but no.

    So here is what I do to store:

    image -> pickle -> compress -> str -> json


    I thought this would work:

    json -> bytes -> decompress -> depickle -> image


    I receive the zlib.error : Error -3 during the following:

    image = pickle.load(zlib.decompress(attachment[1].encode()))

    image = io.BytesIO(image)
    dt = Image.open(image)


    EDIT:

    Okay so I was toying around, and I think the issue might be the .encode(). I start with b" ". After str(b" "), I get "b' '". If I do the .encode(), I get b"b' '". How do I deal with this?

    Continue reading...

Compartilhe esta Página