Support creation of ZIP files with Python modules.

This commit is contained in:
Bastian Kleineidam 2012-05-11 23:30:46 +02:00
parent a0669fd438
commit ac76a91d2d
6 changed files with 77 additions and 1 deletions

View File

@ -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)

View File

@ -109,7 +109,7 @@ ArchivePrograms = {
None: ('tar', 'star',),
},
'zip': {
None: ('7z', '7za'),
None: ('7z', '7za', 'pyzipfile'),
'extract': ('unzip',),
'list': ('unzip',),
'test': ('unzip',),

View File

@ -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)

View File

@ -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)

View File

@ -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 <http://www.gnu.org/licenses/>.
"""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

View File

@ -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'