diff --git a/patoolib/__init__.py b/patoolib/__init__.py index 39f8a8b..89d8ebb 100644 --- a/patoolib/__init__.py +++ b/patoolib/__init__.py @@ -102,7 +102,7 @@ ArchivePrograms = { None: ('7z', '7za'), 'extract': ('pbzip2', 'lbzip2', 'bzip2', 'pybz2'), 'test': ('pbzip2', 'lbzip2', 'bzip2'), - 'create': ('pbzip2', 'lbzip2', 'bzip2'), + 'create': ('pbzip2', 'lbzip2', 'bzip2', 'pybz2'), 'list': ('echo',), }, 'tar': { @@ -118,6 +118,7 @@ ArchivePrograms = { 'gzip': { None: ('pigz', 'gzip', '7z', '7za'), 'extract': ('pygzip',), + 'create': ('pygzip',), }, 'lzh': { None: ('lha',), diff --git a/patoolib/programs/pybz2.py b/patoolib/programs/pybz2.py index fa0c88d..a4903df 100644 --- a/patoolib/programs/pybz2.py +++ b/patoolib/programs/pybz2.py @@ -24,7 +24,7 @@ except ImportError: READ_SIZE_BYTES = 1024*1024 def extract_bzip2 (archive, encoding, cmd, **kwargs): - """Extract a BZIP2 archive with the bz2 Python module functionality.""" + """Extract a BZIP2 archive with the bz2 Python module.""" verbose = kwargs['verbose'] if verbose: util.log_info('extracting %s...' % archive) @@ -44,3 +44,28 @@ def extract_bzip2 (archive, encoding, cmd, **kwargs): if verbose: util.log_info('... extracted to %s' % targetname) return None + + +def create_bzip2 (archive, encoding, cmd, *args, **kwargs): + """Create a BZIP2 archive with the bz2 Python module.""" + verbose = kwargs['verbose'] + if verbose: + util.log_info('creating %s...' % archive) + if len(args) > 1: + util.log_error('multi-file compression not supported in Python bz2') + bz2file = bz2.BZ2File(archive, 'wb') + try: + filename = args[0] + srcfile = open(filename) + try: + data = srcfile.read(READ_SIZE_BYTES) + while data: + bz2file.write(data) + data = srcfile.read(READ_SIZE_BYTES) + if verbose: + util.log_info('... added %s' % filename) + finally: + srcfile.close() + finally: + bz2file.close() + return None diff --git a/patoolib/programs/pygzip.py b/patoolib/programs/pygzip.py index c930e7b..946ac2e 100644 --- a/patoolib/programs/pygzip.py +++ b/patoolib/programs/pygzip.py @@ -22,7 +22,7 @@ from patoolib import util READ_SIZE_BYTES = 1024*1024 def extract_gzip (archive, encoding, cmd, **kwargs): - """Extract a GZIP archive with the gzip Python module functionality.""" + """Extract a GZIP archive with the gzip Python module.""" verbose = kwargs['verbose'] if verbose: util.log_info('extracting %s...' % archive) @@ -42,3 +42,28 @@ def extract_gzip (archive, encoding, cmd, **kwargs): if verbose: util.log_info('... extracted to %s' % targetname) return None + + +def create_gzip (archive, encoding, cmd, *args, **kwargs): + """Create a GZIP archive with the gzip Python module.""" + verbose = kwargs['verbose'] + if verbose: + util.log_info('creating %s...' % archive) + if len(args) > 1: + util.log_error('multi-file compression not supported in Python gzip') + gzipfile = gzip.GzipFile(archive, 'wb') + try: + filename = args[0] + srcfile = open(filename) + try: + data = srcfile.read(READ_SIZE_BYTES) + while data: + gzipfile.write(data) + data = srcfile.read(READ_SIZE_BYTES) + if verbose: + util.log_info('... added %s' % filename) + finally: + srcfile.close() + finally: + gzipfile.close() + return None