-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharchives.py
49 lines (40 loc) · 1022 Bytes
/
archives.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
#! /usr/bin/env python3
import os
import gzip
import shutil
import tarfile
from .file import find_files
############
# Core Archive Functions
######
def gz_c(path, rm=False):
with open(path, 'rb') as f:
with gzip.open(path + '.gz', 'wb') as gz:
shutil.copyfileobj(f, gz)
if rm is True:
os.remove(path)
elif rm is False:
pass
def gz_d(path, rm=False):
with gzip.open(path, 'rb') as gz:
with open(path[:-3], 'wb') as f:
shutil.copyfileobj(gz, f)
if rm is True:
os.remove(path)
elif rm is False:
pass
def tar_dir(path, rm=False):
with tarfile.open(path + '.tar', 'w') as tar:
for f in find_files(path):
tar.add(f, f[len(path):])
if rm is True:
shutil.rmtree(path)
elif rm is False:
pass
def untar_dir(path, rm=False):
with tarfile.open(path, 'r:') as tar:
tar.extractall(path[:-4])
if rm is True:
os.remove(path)
elif rm is False:
pass