forked from lucasayres/python-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip_file.py
More file actions
22 lines (18 loc) · 728 Bytes
/
zip_file.py
File metadata and controls
22 lines (18 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding: utf-8 -*-
import os
import zipfile
def zip_file(directory, output):
"""Create a zip file.
Args:
directory (str): Directory to be compressed.
output (str): Output file .zip.
"""
relroot = os.path.abspath(os.path.join(directory, os.pardir))
with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as zip:
for root, dirs, files in os.walk(directory):
zip.write(root, os.path.relpath(root, relroot))
for file in files:
filename = os.path.join(root, file)
if os.path.isfile(filename):
arcname = os.path.join(os.path.relpath(root, relroot), file)
zip.write(filename, arcname)