-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipUtils.py
More file actions
120 lines (97 loc) · 3.14 KB
/
ZipUtils.py
File metadata and controls
120 lines (97 loc) · 3.14 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
"""
Name: ZipUtils.py
Purpose: Zip file utilities
Dependencies: none
Version: 3.6
Author: ted.chapin
Created: 9/12/2018
"""
import json
import os
import zipfile
config_file = os.path.join(os.path.dirname(__file__), "config.json")
CONFIG = json.loads(open(config_file).read()) if os.path.isfile(config_file) else {}
def main():
pass
return
def zip_folder(folder_path):
"""
purpose:
Zip a folder to a .zip file with the same name as the folder.
arguments:
folder_path: string
path to the folder to zip
return value: string
path to the zip file if successful,
"" if not successful
"""
try:
if not os.path.isdir(folder_path):
return ""
parent_folder = os.path.dirname(folder_path)
zip_file = "{0}.zip".format(folder_path)
with zipfile.ZipFile(zip_file, "w", compression=zipfile.ZIP_DEFLATED) as out_file:
for root, folders, files in os.walk(folder_path):
for folder_name in folders:
absolute_path = os.path.join(root, folder_name)
relative_path = absolute_path.replace(parent_folder, "")
out_file.write(absolute_path, relative_path)
for file_name in files:
absolute_path = os.path.join(root, file_name)
relative_path = absolute_path.replace(parent_folder, "")
out_file.write(absolute_path, relative_path)
return zip_file
except Exception:
return ""
def zip_gdb(gdb_path):
"""
purpose:
Zip a file geodatabase.
arguments:
gdb_path: string
path to the file gdb to zip
return value: string
the path name of the zip file if successful
"" if not successful
"""
try:
if not os.path.isdir(gdb_path):
return ""
zip_file = "{0}.zip".format(gdb_path)
with zipfile.ZipFile(zip_file, "w", compression=zipfile.ZIP_DEFLATED) as out_file:
for root, dirs, files in os.walk(gdb_path):
for name in files:
out_file.write(os.path.join(root, name), os.path.join(
os.path.basename(gdb_path), name))
return zip_file
except Exception:
return ""
def unzip_file(file_path):
"""
purpose:
Unzip a zip file into a folder with the name of the zip file.
arguments:
file_path: string
path to the file to unzip
must be a .zip file
return value: boolean
True if successful
False if error
"""
try:
# make sure the zip file exists
if os.path.exists(file_path):
out_folder = os.path.splitext(file_path)[0]
# make sure the folder doesn't already exist
if os.path.isdir(out_folder):
return False
else:
with zipfile.ZipFile(file_path) as zip_file:
zip_file.extractall(out_folder)
return True
else:
return False
except Exception:
return False
if __name__ == '__main__':
main()