Tuesday, 19 March 2019

How to decompress lzma2 (.xz) and zstd (.zst) files into a folder using Python 3?

I have been working for a long time with .bz2 files. To unpack/decompress .bz2 files into a specific folder I have been using the following function:

destination_folder = 'unpacked/'
def decompress_bz2_to_folder(input_file):
    unpackedfile = bz2.BZ2File(input_file)
    data = unpackedfile.read()
    open(destination_folder, 'wb').write(data)

Recently I obtained a list of files with the .xz (not .tar.xz) and .zst extensions. My poor research skills told me that the former is lzma2 compression and the latter is Zstandard.

However, I couldn't find of an easy way to unpack the contents of these archives into a folder (like I do with the .bz2 files).

How can I:

  1. Unpack the contents of an .xz (lzma2) file into a folder using Python 3?
  2. Unpack the contents of a .zst (Zstandard) file into a folder using Python 3?

Important Note: I'm unpacking very large files, so it would be great if the solution take into consideration any potential Memory Errors.



from How to decompress lzma2 (.xz) and zstd (.zst) files into a folder using Python 3?

No comments:

Post a Comment