Skip to content

Instantly share code, notes, and snippets.

@d-v-b
Created August 8, 2015 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-v-b/5322a73a7bd6cd8a8735 to your computer and use it in GitHub Desktop.
Save d-v-b/5322a73a7bd6cd8a8735 to your computer and use it in GitHub Desktop.
compression and decompression
def bz2compress(raw_fname, wipe=False, overwrite=False):
"""
:param raw_fname: string, full path of file to be compressed
:param wipe: bool, optional. If set to True, raw file will be deleted after compression. Defaults to False.
:param overwrite: bool, optional. If set to True, overwrites file sharing name of output file. Defaults to False
:return:
"""
import bz2
import os
compressed_fname = raw_fname + '.bz2'
if os.path.exists(compressed_fname) and overwrite is False:
raise ValueError('File {0} already exists. Call bz2compress with '.format(compressed_fname) +
'overwrite=True to overwrite.')
with open(raw_fname, 'rb') as f:
data = f.read()
compressed_file = bz2.BZ2File(compressed_fname, "wb")
compressed_file.write(data)
compressed_file.close()
if wipe:
os.remove(raw_fname)
return
def bz2decompress(compressed_fname, wipe=False, overwrite=False):
"""
:param compressed_fname: string, full path of file to be decompressed
:param wipe: bool, optional. If set to True, raw file will be deleted after decompression
:param overwrite: bool, optional. If set to True, overwrites file sharing name of output file. Defaults to False.
:return:
"""
import bz2
import os
raw_fname = compressed_fname[:-4] # chopping the '.bz2' extension for raw file name
if os.path.exists(raw_fname) and overwrite is False:
raise ValueError('File {0} already exists. Call bz2decompress with '.format(raw_fname) +
'overwrite=True to overwrite.')
infile = bz2.BZ2File(compressed_fname, 'rb')
data = infile.read()
infile.close()
with open(raw_fname, 'wb') as f:
f.write(data)
if wipe:
os.remove(compressed_fname)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment