I am using the zstandard library to pack and unpack. I am essentially trying to recreate the following bash in Python:
curl -O - http://download.com/file.tar.zst | zstd -d - | tar xf -
I found this discussion saying that Python's tarfile module supports any stream so using zstd should be trivial. I am having trouble using the streams, though.
I tried to do the following:
import requests
import zstandard
import tarfile
# Write .tar.zst archive
path = "/some/path/to/tar"
to_zstd = zstd.ZstdCompressor()
with tempfile.NamedTemporaryFile() as temp_tar_file:
with tarfile.open(name="file.tar.zst", fileobj=to_zstd.stream_writer(temp_tar_file), mode="w|") as tf:
tf.add(path)
# Upload generated .tar.zst
# Extract .tar.zst
with closing(requests.get(url, stream=True) as r:
tardir = tempfile.mkdtemp()
unzstd = zstd.ZstdDecompressor()
with io.BytesIO() as tarbyte:
unzstd.copy_stream(resp.raw, tarbyte)
with tarfile.open(fileobj=tarbyte, mode="r|") as tf:
tf.extractall(path=tardir)
Running this I get tarfile.ReadError("empty file")
and running zstd -d file.tar.zst
I get Read error (39) : premature end
. The file size of file.tar.zst isn't zero and it does change depending on what's compressed so it's not empty, but I'm still not compressing right. Where am I going wrong?