|
18 | 18 | import os |
19 | 19 | import platform |
20 | 20 |
|
| 21 | +from typing import ByteString |
| 22 | + |
21 | 23 | # Some Python implementations don't support C extensions. That's why we have |
22 | 24 | # a CFFI implementation in the first place. The code here import one of our |
23 | 25 | # "backends" then re-exports the symbols from this module. For convenience, |
@@ -170,3 +172,39 @@ def open( |
170 | 172 | ) |
171 | 173 | else: |
172 | 174 | return fh |
| 175 | + |
| 176 | + |
| 177 | +def compress(data: ByteString, level: int = 3) -> bytes: |
| 178 | + """Compress source data using the zstd compression format. |
| 179 | +
|
| 180 | + This performs one-shot compression using basic/default compression |
| 181 | + settings. |
| 182 | +
|
| 183 | + This method is provided for convenience and is equivalent to calling |
| 184 | + ``ZstdCompressor(level=level).compress(data)``. |
| 185 | +
|
| 186 | + If you find yourself calling this function in a tight loop, |
| 187 | + performance will be greater if you construct a single ``ZstdCompressor`` |
| 188 | + and repeatedly call ``compress()`` on it. |
| 189 | + """ |
| 190 | + cctx = ZstdCompressor(level=level) |
| 191 | + |
| 192 | + return cctx.compress(data) |
| 193 | + |
| 194 | + |
| 195 | +def decompress(data: ByteString, max_output_size: int = 0) -> bytes: |
| 196 | + """Decompress a zstd frame into its original data. |
| 197 | +
|
| 198 | + This performs one-shot decompression using basic/default compression |
| 199 | + settings. |
| 200 | +
|
| 201 | + This method is provided for convenience and is equivalent to calling |
| 202 | + ``ZstdDecompressor().decompress(data, max_output_size=max_output_size)``. |
| 203 | +
|
| 204 | + If you find yourself calling this function in a tight loop, performance |
| 205 | + will be greater if you construct a single ``ZstdDecompressor`` and |
| 206 | + repeatedly call ``decompress()`` on it. |
| 207 | + """ |
| 208 | + dctx = ZstdDecompressor() |
| 209 | + |
| 210 | + return dctx.decompress(data, max_output_size=max_output_size) |
0 commit comments