def close_compressed(filename, hdf5_file, compression_type='bz2', create_link=False):
"""Closes the compressed hdf5_file that was opened with open_compressed.
When the file was opened for writing (using the 'w' flag in open_compressed), the created HDF5 file is compressed into the given file name.
To be able to read the data using the real tools, a link with the correct extension might is created, when create_link is set to True.
"""
hdf5_file_name = hdf5_file.filename
is_writable = hdf5_file.writable
hdf5_file.close()
if is_writable:
# create compressed tar file
tar = tarfile.open(filename, mode="w:" + compression_type)
tar.add(hdf5_file_name, os.path.basename(filename))
tar.close()
if create_link:
extension = {'': '.tar', 'bz2': '.tar.bz2',
'gz': 'tar.gz'}[compression_type]
link_file = filename + extension
if not os.path.exists(link_file):
os.symlink(os.path.basename(filename), link_file)
# clean up locally generated files
os.remove(hdf5_file_name)
评论列表
文章目录