Skip to content

Commit

Permalink
Add support for lz4 files (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
meganomic authored Dec 17, 2020
1 parent b3a48d8 commit bbc470f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ jobs:
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- run: pip install -e .[zstd]
- run: pip install -e .[lz4,zstd]
- run: python setup.py test
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'compression formats.',
long_description=long_description,
extras_require={
'lz4': ['lz4 >= 2.2.1'],
'zstd': ['zstandard >= 0.10.2']
},
python_requires='>=3.4',
Expand Down
34 changes: 34 additions & 0 deletions xtarfile/lz4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from contextlib import contextmanager
from tarfile import open as tarfile_open

try:
import lz4.frame as lz4
except ImportError:
lz4 = None


class Lz4Tarfile:
def __init__(self, **kwargs):
self.lz4_kwargs = kwargs

@contextmanager
def read(self, path: str, mode: str):
with lz4.LZ4FrameFile(path) as lz4d:
archive = tarfile_open(mode=mode, fileobj=lz4d, **self.lz4_kwargs)
try:
yield archive
finally:
archive.close()

@contextmanager
def write(self, path: str, mode: str):
with lz4.LZ4FrameFile(path, mode=mode[0]) as lz4c:
archive = tarfile_open(mode=mode, fileobj=lz4c, **self.lz4_kwargs)
try:
yield archive
finally:
archive.close()


if lz4 is None:
Lz4Tarfile = None # noqa F811
2 changes: 2 additions & 0 deletions xtarfile/xtarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from tarfile import open as tarfile_open

from xtarfile.zstd import ZstandardTarfile
from xtarfile.lz4 import Lz4Tarfile


_HANDLERS = {
'zstd': ZstandardTarfile,
'zst': ZstandardTarfile,
'lz4': Lz4Tarfile,
}

_NATIVE_FORMATS = ('gz', 'bz2', 'xz', 'tar')
Expand Down

0 comments on commit bbc470f

Please sign in to comment.