Reputation: 518
I've written some code to bz2-compress a file with the BZ2 Compressor Object:
def compressFile(file_name, new_name):
comp = bz2.BZ2Compressor()
comFile = open(new_name, "wb")
oldFile = open(file_name, "rb")
while True:
data = oldFile.read(1024*1024)
if(len(data) == 0):
break
compressed = comp.compress(data)
comFile.write(compressed)
comp.flush()
comFile.close()
I don't get an error and the file is created, but when I want to open it with an archive manager, I get a nonspecific error. I can't find my mistake and this module is poorly documented.
Upvotes: 2
Views: 1180
Reputation: 69022
When you use a BZ2Compressor
, you get data in chunks when you call compress()
, and a good chance is that you only get the data when you call flush()
.
It should work if you change your function like this:
def compressFile(file_name, new_name):
comp = bz2.BZ2Compressor()
comFile = open(new_name, "wb")
oldFile = open(file_name, "rb")
while True:
data = oldFile.read(1024*1024)
if(len(data) == 0):
break
comFile.write(comp.compress(data))
comFile.write(comp.flush())
comFile.close()
Upvotes: 6