-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import re | ||
from zipfile import ZipFile | ||
|
||
|
||
def zip_directory(path_dir, path_zip, filter_=None, remove=None, dirname=None): | ||
reg_filter = None | ||
reg_remove = None | ||
|
||
if filter_ is not None: | ||
reg_filter = re.compile(filter_) | ||
if remove is not None: | ||
reg_remove = re.compile(remove) | ||
|
||
path_dir = os.path.abspath(path_dir) | ||
with ZipFile(path_zip, 'w') as zf: | ||
for d, ds, fs in os.walk(path_dir): | ||
z_d = d.replace(path_dir, '') | ||
for f in fs: | ||
path = os.path.join(d, f) | ||
z_path = os.path.join(z_d, f).strip(os.path.sep) | ||
|
||
if reg_filter is not None and not reg_filter.match(z_path): | ||
continue | ||
|
||
if reg_remove is not None and reg_remove.match(z_path): | ||
continue | ||
|
||
if dirname is True: | ||
dirname = os.path.basename(path_dir) | ||
if dirname is not None: | ||
z_path = '%s/%s' % (dirname, z_path) | ||
|
||
zf.write(path, z_path) |