using zlib, i want to be able to compresse numpy arrays and write them in a file and then be able to read them back. I did the following
with open(outputFile, 'wb') as zFile:
for row in array:
compressed = zlib.compress(row, compressionLevel)
zFile.write(compressed)
with open(os.path.join(path, fileName), 'rb') as zFile:
for line in zFile:
decompressed = zlib.decompress(line)
data.append(decompressed)
data = np.array(data)
The writing process works as it fills the file and if i write simpler data with compressionLevel = 0, it is ok. But i can't make the reading process to work. I tried to do to zlib.compress(row.tobytes() + '\n'.encode(), compressionLevel) so that i can have proper lines to be read, but some element in my data seems to be interpreted as \n, so it does not read the real lines.
I also tried to read the file doing zFile.read(bufferSize) in a while loop and break the loop when there is nothing more to read, but each element previously compressed have a varying size (due to varying performance regarding each row) so i can't know the buffersize in advance.
EDIT: regarding answers, it seems that np.savez_compress is better suited but for now, i am stuck with zlib as it could be used elsewhere in the project and i cannot change it by myself for now.