def open_compressed(filename, open_flag='r', compression_type='bz2'):
"""Opens a compressed HDF5File with the given opening flags.
For the 'r' flag, the given compressed file will be extracted to a local space.
For 'w', an empty HDF5File is created.
In any case, the opened HDF5File is returned, which needs to be closed using the close_compressed() function.
"""
# create temporary HDF5 file name
hdf5_file_name = tempfile.mkstemp('.hdf5', 'bob_')[1]
if open_flag == 'r':
# extract the HDF5 file from the given file name into a temporary file name
tar = tarfile.open(filename, mode="r:" + compression_type)
memory_file = tar.extractfile(tar.next())
real_file = open(hdf5_file_name, 'wb')
real_file.write(memory_file.read())
del memory_file
real_file.close()
tar.close()
return bob.io.base.HDF5File(hdf5_file_name, open_flag)
评论列表
文章目录