Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
| 4 | |
| 5 | import bb.compress._pipecompress |
| 6 | import shutil |
| 7 | |
| 8 | |
| 9 | def open(*args, **kwargs): |
| 10 | return bb.compress._pipecompress.open_wrap(ZstdFile, *args, **kwargs) |
| 11 | |
| 12 | |
| 13 | class ZstdFile(bb.compress._pipecompress.PipeFile): |
| 14 | def __init__(self, *args, num_threads=1, compresslevel=3, **kwargs): |
| 15 | self.num_threads = num_threads |
| 16 | self.compresslevel = compresslevel |
| 17 | super().__init__(*args, **kwargs) |
| 18 | |
| 19 | def _get_zstd(self): |
| 20 | if self.num_threads == 1 or not shutil.which("pzstd"): |
| 21 | return ["zstd"] |
| 22 | return ["pzstd", "-p", "%d" % self.num_threads] |
| 23 | |
| 24 | def get_compress(self): |
| 25 | return self._get_zstd() + ["-c", "-%d" % self.compresslevel] |
| 26 | |
| 27 | def get_decompress(self): |
| 28 | return self._get_zstd() + ["-d", "-c"] |