-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip_preview.py
More file actions
172 lines (132 loc) · 5.84 KB
/
zip_preview.py
File metadata and controls
172 lines (132 loc) · 5.84 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import zipfile
import datetime
import os
import json
archive_name = 'data.zip' #raw_input('Enter file path: ')
TEMPLATE_TPL = """
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/tree-styles.css">
<style>
body {
padding-top: 150px;
padding-bottom: 20px;
}
</style>
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Tree View</a>
</div>
<div class="navbar-collapse collapse">
</div><!--/.navbar-collapse -->
</div>
</div>
<div class="container">
<!-- Example row of columns -->
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Zip Files Preview</h3>
</div>
<div class="file_info"><p><span><i class="glyphicon glyphicon-compressed"></i></span> {{ archive_name }} <span class="pull-right">Size: <b> {# Here should be the total size of the ziped folder #}Mb</b></span></p>
<hr>
</div>
<div class="panel-body">
<div class="tree">
<ul>
{%- for t in tree recursive %}
<li><span class="li_wrap">
<span><i class="glyphicon {{ 'glyphicon-folder-close' if t.type == 'folder' else 'glyphicon-file'}}"></i> {{ t.name }}</span>
{% if t.type != 'folder' %}
<span class="pull-right">{{ t.size|filesizeformat }} </span>
{% endif %}
</span>
{%- if t.children -%}
<ul>{{ loop(t.children) }}</ul>
{%- endif %}
</li>
{%- endfor %}
</ul>
</div>
</div> <!-- panel-body close -->
</div> <!-- panel panel-default close -->
</div> <!-- col-md-6 close -->
</div>
<hr>
<footer>
<p>© Tree View 2014</p>
</footer>
</div> <!-- /container --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.0.min.js"><\/script>')</script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
<script type="text/javascript" src="js/tree.js"></script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
<!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
<script>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create','UA-XXXXX-X');ga('send','pageview');
</script>
</body>
</html>
"""
def print_template(archive_name, tree):
from jinja2 import Template
template = Template(TEMPLATE_TPL)
print template.render(tree=tree, archive_name=archive_name)
def make_tree(archive_name):
zf = zipfile.ZipFile(archive_name)
tree = {'type': 'folder', 'id': -1, 'children': {}}
for i, info in enumerate(zf.infolist()):
comps = info.filename.split(os.sep)
node = tree
for c in comps:
if c not in node['children']:
if c == '':
node['type'] = 'folder'
continue
node['children'][c] = {'name': c, 'type': 'item', 'id': 'item%s' % i, 'children': {}}
node = node['children'][c]
node['size'] = info.file_size
return tree
def children_to_list(node):
if node['type'] == 'item':
del node['children']
else:
node['children'] = list(node['children'].values())
node['children'].sort(key=lambda x: x['name'])
node['children'] = map(children_to_list, node['children'])
return node
if __name__ == '__main__':
tree = children_to_list(make_tree(archive_name))['children']
print_template(archive_name, tree)