source: ZMS/trunk/_ziputil.py @ 1743

Revision 1743, 2.0 KB checked in by zmsdev, 6 months ago (diff)

applied minor fixes for (ajax) file-upload

Line 
1################################################################################
2# _ziputil.py
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17################################################################################
18
19# Imports.
20from cStringIO import StringIO
21from types import StringTypes
22import zipfile
23# Product Imports.
24import _globals
25
26
27"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
28_ziputil.importZip2Zodb:
29
30Extracts and imports zip-file to zodb.
31"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
32def importZip2Zodb(root, data):
33  if type( data) in StringTypes:
34    data = StringIO( data)
35  zf = zipfile.ZipFile( data, 'r')
36  for name in zf.namelist():
37    container = root
38    ids = name.split('/')
39    if len(ids) > 1:
40      for id in ids[:-1]:
41        if id not in container.objectIds():
42          container.manage_addFolder(id=id)
43        container = getattr(container,id)
44    id = ids[-1]
45    if id:
46      file = zf.read( name)
47      mt, enc  = _globals.guess_contenttype( id, file)
48      if id in container.objectIds():
49        container.manage_delObjects( [id])
50      if mt.startswith('image'):
51        file = container.manage_addImage(id=id,file=file)
52      else:
53        file = container.manage_addFile(id=id,file=file)
54
55################################################################################
Note: See TracBrowser for help on using the repository browser.