[Zope-CMF] CMFComplexDocument

Jens Quade jq@jquade.de
08 May 2001 17:58:28 +0200


--=-=-=

"Rolander, Dan" <Dan.Rolander@marriott.com> writes:

> I tried this but I received the following error when uploading the
> uncompressed zipfile:
> 
>   Error Type: IOError
>   Error Value: [Errno 5] Input/output error

I modified ZipImporter.py to not create temporary files. Please
replace ...Products/ZipFolder/ZipImporter.py with the attached
version. Please tell me if the new version works for you. I will then
release ZipFolder 0.22 with this change.
 

jens

-- 
Shipping software is an unnatural act

--=-=-=
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=ZipImporter.py
Content-Description: modified zipimporter script


import os
import string
import tempfile
import types

import StringIO
import zipfile


MINZIPFILESIZE=22


class ZipImporter:
    """A mix-in class to add zipfile-import-support to folders"""


    def import_zipfile(self,file):
        
        # minimum/maximum size check
        file.seek(0,2)
        fsize=file.tell()
        file.seek(0,0)
        if fsize > self.max_zipfile_size():
            raise RuntimeError,'ZIP file is too big'

        if fsize < MINZIPFILESIZE:
            raise RuntimeError,'ZIP file is too small'

##         # Write file
##         tmpfilename=tempfile.mktemp()
##         tmpfile=open(tmpfilename,'w+b')

##         try:
##             tmpfile.write(file.read())
##         finally:
##             tmpfile.close()

##         try:            
        zf=zipfile.ZipFile(file,'r')

        try:
            self.check_zip(zf)
                
            for name in zf.namelist():
                self._add_file_from_zip(zf,name)
                    
        finally:
            zf.close()

##        finally:
##            os.unlink(tmpfilename)


            

    def _add_file_from_zip(self,zipfile,name):

        basename=os.path.basename(name)
        pathname=os.path.dirname(name)
        if not self.check_filename(pathname,basename):
            return # skip ugly files
        
        sf=StringIO.StringIO(zipfile.read(name))
        self.add_file(pathname,basename,sf)
        sf.close()


    #
    #
    # To be overwritten:
     
    def max_zipfile_size(self):
        """return maximum size for zip files. Default 4MB"""
        return 4*1024*1024;
    

    def check_zip(self,zipfile):
        # a hook to check zipfile before import loop
        # maybe: Look for a special file in the zip...
        
        pass

    
    def check_filename(self,pathname,basename):
        # return 0 to skip file, raise an exception to annoy...
        return 1;

    def add_file(self,pathname,basename,file):
        #add file to whatever...
        #is called with path,basename and file handle

        # overwrite this!
        pass
    
    


--=-=-=--