-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathziptools.py
85 lines (74 loc) · 2.93 KB
/
ziptools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
#------------------------------------------------------------
# pelisalacarta 4
# Copyright 2015 [email protected]
#
# Distributed under the terms of GNU General Public License v3 (GPLv3)
# http://www.gnu.org/licenses/gpl-3.0.html
#------------------------------------------------------------
# This file is part of pelisalacarta 4.
#
# pelisalacarta 4 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.
#
# pelisalacarta 4 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 pelisalacarta 4. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------
import base64, re, urllib, string, sys, zipfile, os, os.path
import xbmc
import plugintools
class ziptools:
def extract(self, file, dir):
plugintools.log("file=%s" % file)
plugintools.log("dir=%s" % dir)
if not dir.endswith(':') and not os.path.exists(dir):
os.mkdir(dir)
zf = zipfile.ZipFile(file)
self._createstructure(file, dir)
num_files = len(zf.namelist())
for name in zf.namelist():
plugintools.log("name=%s" % name)
if not name.endswith('/'):
plugintools.log("no es un directorio")
try:
(path,filename) = os.path.split(os.path.join(dir, name))
plugintools.log("path=%s" % path)
plugintools.log("name=%s" % name)
os.makedirs( path )
except:
pass
outfilename = os.path.join(dir, name)
plugintools.log("outfilename=%s" % outfilename)
try:
outfile = open(outfilename, 'wb')
outfile.write(zf.read(name))
except:
plugintools.log("Error en fichero "+name)
def _createstructure(self, file, dir):
self._makedirs(self._listdirs(file), dir)
def create_necessary_paths(filename):
try:
(path,name) = os.path.split(filename)
os.makedirs( path)
except:
pass
def _makedirs(self, directories, basedir):
for dir in directories:
curdir = os.path.join(basedir, dir)
if not os.path.exists(curdir):
os.mkdir(curdir)
def _listdirs(self, file):
zf = zipfile.ZipFile(file)
dirs = []
for name in zf.namelist():
if name.endswith('/'):
dirs.append(name)
dirs.sort()
return dirs