Back to index

freevial  1.2
uncompress.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 
00003 #
00004 # Freevial
00005 # Compressed Files Extractor
00006 #
00007 # Copyright (C) 2007, 2008 The Freevial Team
00008 #
00009 # By Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
00010 #
00011 # This program is free software: you can redistribute it and/or modify
00012 # it under the terms of the GNU General Public License as published by
00013 # the Free Software Foundation, either version 3 of the License, or
00014 # (at your option) any later version.
00015 #
00016 # This program is distributed in the hope that it will be useful,
00017 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 # GNU General Public License for more details.
00020 #
00021 # You should have received a copy of the GNU General Public License
00022 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 #
00024 
00025 import os
00026 
00027 class Uncompressor():
00028        """ Provides an interface to handle files compressed with both, tar and zip format, the same way.\n """
00029        
00030        def __init__(self, filename, mode='r'):
00031               
00032               self._open(filename, mode)
00033        
00034        
00035        def _detect(self, filename):
00036               """ Detect which module needs to be used for the given file, and returns an instace of it.\n """
00037               
00038               ext = os.path.splitext(filename)[1][1:]   # Get the extension
00039               
00040               if ext in ('gz', 'bz2'):
00041                      return 'tar'
00042               
00043               elif ext == 'zip':
00044                      return 'zip'
00045               
00046               else:
00047                      raise ValueError, 'Unsuported file format, expected .tar.gz, .tar.bz or .zip.'
00048        
00049        
00050        def _unzipAll(directory):
00051               """ Extract all members of the current zip file to the given directory. """
00052               
00053               for name in self.fileobj.namelist():
00054                      
00055                      if name.endswith('/'):
00056                             os.mkdir(os.path.join(directory, name))
00057                      
00058                      else:
00059                             outfile = open(os.path.join(directory, name), 'wb')
00060                             outfile.write(self.fileobj.read(name))
00061                             outfile.close()
00062        
00063        
00064        def _open(self, filename, mode='r'):
00065               """ Open a compressed file for reading, writing or appending. """
00066               
00067               self.type = self._detect(filename)
00068               
00069               if self.type == 'tar':
00070                      import tarfile
00071                      self.fileobj = tarfile.open(filename, mode)
00072               
00073               elif self.type == 'zip':
00074                      import zipfile
00075                      self.fileobj = zipfile.ZipFile(filename, mode)
00076        
00077        
00078        def extractall(self, directory='./'):
00079               """Extract all members from the archive to the given directory."""
00080               
00081               if self.type == 'tar':
00082                      return self.fileobj.extractall(directory)
00083               
00084               elif self.type == 'zip':
00085                      return self._unzipAll(directory)