Skip to content

Commit bbc470f

Browse files
authored
Add support for lz4 files (#9)
1 parent b3a48d8 commit bbc470f

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ jobs:
3232
- uses: actions/setup-python@v2
3333
with:
3434
python-version: ${{ matrix.python }}
35-
- run: pip install -e .[zstd]
35+
- run: pip install -e .[lz4,zstd]
3636
- run: python setup.py test

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
'compression formats.',
2626
long_description=long_description,
2727
extras_require={
28+
'lz4': ['lz4 >= 2.2.1'],
2829
'zstd': ['zstandard >= 0.10.2']
2930
},
3031
python_requires='>=3.4',

xtarfile/lz4.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from contextlib import contextmanager
2+
from tarfile import open as tarfile_open
3+
4+
try:
5+
import lz4.frame as lz4
6+
except ImportError:
7+
lz4 = None
8+
9+
10+
class Lz4Tarfile:
11+
def __init__(self, **kwargs):
12+
self.lz4_kwargs = kwargs
13+
14+
@contextmanager
15+
def read(self, path: str, mode: str):
16+
with lz4.LZ4FrameFile(path) as lz4d:
17+
archive = tarfile_open(mode=mode, fileobj=lz4d, **self.lz4_kwargs)
18+
try:
19+
yield archive
20+
finally:
21+
archive.close()
22+
23+
@contextmanager
24+
def write(self, path: str, mode: str):
25+
with lz4.LZ4FrameFile(path, mode=mode[0]) as lz4c:
26+
archive = tarfile_open(mode=mode, fileobj=lz4c, **self.lz4_kwargs)
27+
try:
28+
yield archive
29+
finally:
30+
archive.close()
31+
32+
33+
if lz4 is None:
34+
Lz4Tarfile = None # noqa F811

xtarfile/xtarfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
from tarfile import open as tarfile_open
33

44
from xtarfile.zstd import ZstandardTarfile
5+
from xtarfile.lz4 import Lz4Tarfile
56

67

78
_HANDLERS = {
89
'zstd': ZstandardTarfile,
910
'zst': ZstandardTarfile,
11+
'lz4': Lz4Tarfile,
1012
}
1113

1214
_NATIVE_FORMATS = ('gz', 'bz2', 'xz', 'tar')

0 commit comments

Comments
 (0)