I have been tasked with taking different .txt files and converting them to .Z compressed files. We use python 3.11 to do our automation. I used the following code to create a simple .Z file using zlib. It creates the file and when I use python I can read it. However when I send the finished .Z file to someone else who uses a application like WinZip or 7-Zip to decompress and extract the data, the app "cannot open the archive" is generally the response. Is there something that I am missing when writing the compressed data to a .Z shareable file?
'test_file.txt' - blah blah blah
import zlib
import os, sys
#file to compress
filename_in = r'C:\Users\JC\Documents\Test8\test_file.txt'
#output file
filename_out = r'C:\Users\JC\Documents\Test_8\test_output.Z.'
with open(filename_in, mode="r") as fin, open(filename_out, mode="wb") as fout:
data = fin.read()
compressed_data = zlib.compress(bytearray(data,'utf-8'), zlib.Z_BEST_COMPRESSION)
fout.write(compressed_data)