From ac76a91d2d41390938388daeea4dda79076cb666 Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Fri, 11 May 2012 23:30:46 +0200 Subject: [PATCH] Support creation of ZIP files with Python modules. --- doc/changelog.txt | 1 + patoolib/__init__.py | 2 +- patoolib/programs/pybz2.py | 2 ++ patoolib/programs/pygzip.py | 2 ++ patoolib/programs/pyzipfile.py | 65 ++++++++++++++++++++++++++++++++++ tests/test_archives.py | 6 ++++ 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 patoolib/programs/pyzipfile.py diff --git a/doc/changelog.txt b/doc/changelog.txt index 87a0598..9e0aa98 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -13,6 +13,7 @@ * Prevent overwriting the same file with repack. * Support extraction of BZIP2 (.bz2) files with the Python bz2 module. * Support extraction of GZIP (.gz) files with the Python gzip module. +* Support extraction of ZIP (.zip) files with the Python zipfile module. 0.15 "Contraband" (released 8.4.2012) diff --git a/patoolib/__init__.py b/patoolib/__init__.py index 89d8ebb..60f2afd 100644 --- a/patoolib/__init__.py +++ b/patoolib/__init__.py @@ -109,7 +109,7 @@ ArchivePrograms = { None: ('tar', 'star',), }, 'zip': { - None: ('7z', '7za'), + None: ('7z', '7za', 'pyzipfile'), 'extract': ('unzip',), 'list': ('unzip',), 'test': ('unzip',), diff --git a/patoolib/programs/pybz2.py b/patoolib/programs/pybz2.py index a4903df..c51ee69 100644 --- a/patoolib/programs/pybz2.py +++ b/patoolib/programs/pybz2.py @@ -26,6 +26,8 @@ READ_SIZE_BYTES = 1024*1024 def extract_bzip2 (archive, encoding, cmd, **kwargs): """Extract a BZIP2 archive with the bz2 Python module.""" verbose = kwargs['verbose'] + outdir = kwargs['outdir'] + # XXX honor outdir if verbose: util.log_info('extracting %s...' % archive) targetname = util.get_single_outfile(kwargs['outdir'], archive) diff --git a/patoolib/programs/pygzip.py b/patoolib/programs/pygzip.py index 946ac2e..b1be022 100644 --- a/patoolib/programs/pygzip.py +++ b/patoolib/programs/pygzip.py @@ -24,6 +24,8 @@ READ_SIZE_BYTES = 1024*1024 def extract_gzip (archive, encoding, cmd, **kwargs): """Extract a GZIP archive with the gzip Python module.""" verbose = kwargs['verbose'] + outdir = kwargs['outdir'] + # XXX honor outdir if verbose: util.log_info('extracting %s...' % archive) targetname = util.get_single_outfile(kwargs['outdir'], archive) diff --git a/patoolib/programs/pyzipfile.py b/patoolib/programs/pyzipfile.py new file mode 100644 index 0000000..dcadb3f --- /dev/null +++ b/patoolib/programs/pyzipfile.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2012 Bastian Kleineidam +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""Archive commands for the bz2 Python module.""" +from patoolib import util +import zipfile + +READ_SIZE_BYTES = 1024*1024 + + +def list_zip (archive, encoding, cmd, **kwargs): + verbose = kwargs['verbose'] + if verbose: + util.log_info('listing %s...' % archive) + zfile = zipfile.ZipFile(archive, "r") + try: + for name in zfile.namelist(): + util.log_info('member %s' % name) + finally: + zfile.close() + return None + +test_zip = list_zip + +def extract_zip (archive, encoding, cmd, **kwargs): + """Extract a ZIP archive with the zipfile Python module.""" + verbose = kwargs['verbose'] + outdir = kwargs['outdir'] + # XXX honor outdir + if verbose: + util.log_info('extracting %s...' % archive) + zfile = zipfile.ZipFile(archive) + try: + zfile.extractall() + finally: + zfile.close() + if verbose: + util.log_info('... extracted to %s' % outdir) + return None + + +def create_zip (archive, encoding, cmd, *args, **kwargs): + """Create a ZIP archive with the zipfile Python module.""" + verbose = kwargs['verbose'] + if verbose: + util.log_info('creating %s...' % archive) + zfile = zipfile.ZipFile(archive, 'w') + try: + for filename in args: + zfile.write(filename) + finally: + zfile.close() + return None diff --git a/tests/test_archives.py b/tests/test_archives.py index 0e7902e..1e963b9 100644 --- a/tests/test_archives.py +++ b/tests/test_archives.py @@ -145,6 +145,12 @@ class TestArchives (ArchiveTest): self.program = 'zip' self.archive_create('t.zip') + def test_pyzipfile (self): + self.program = 'pyzipfile' + self.archive_create('t.zip') + self.archive_extract('t.zip') + self.archive_list('t.zip') + @needs_program('gzip') def test_gzip (self): self.program = 'gzip'