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:
- Unpack the contents of an
.xz
(lzma2
) file into a folder using Python 3? - 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 takes into consideration any potential Memory Errors.
zstd
cli can decompress both.xz
and.zst
files, if built with appropriate options. This can be checked withzstd -vV
. Example :zstd -vV
,*** zstd command line interface 64-bits v1.3.2, by Yann Collet ***
,*** supports: zstd, zstd legacy v0.4+, gzip, lz4, lzma, xz
– Loudish