-
Notifications
You must be signed in to change notification settings - Fork 1
/
zipper.py
50 lines (46 loc) · 1.48 KB
/
zipper.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
50
__author__ = 'Jubin & Raghava'
import os
import zipfile
import shutil
def zipFun(src, dst):
if not os.path.exists(os.getcwd() + "/output"):
os.makedirs(os.getcwd() + "/output")
zf = zipfile.ZipFile("temp.zip", "w")
for fil in src:
zf.write(fil, os.path.basename(fil))
zf.close()
with open('output/' + os.path.basename(dst), 'wb') as wfd:
for f in [dst, 'temp.zip']:
with open(f, 'rb') as fd:
shutil.copyfileobj(fd, wfd, 1024 * 1024 * 10)
fseek = str(os.path.getsize(dst))
fseek = (10 - len(fseek)) * "0" + fseek
wfd.write(fseek)
os.remove('temp.zip')
def unzipFun(src):
if not os.path.exists(os.getcwd() + "/output"):
os.makedirs(os.getcwd() + "/output")
shutil.copy2(src, 'test.zip')
temp = open('test.zip', "rb+")
temp.seek(-10, 2)
size = int(temp.read(8))
temp.seek(-10, 2)
temp.truncate()
tempzip = open(os.getcwd() + "/temp2.zip", "wb")
buffer_size = 1024 * 1024
temp.close()
temp = open('test.zip', "rb")
temp.seek(size)
while True:
copy_buffer = temp.read(buffer_size)
if not copy_buffer:
break
tempzip.write(copy_buffer)
# shutil.copyfileobj(temp,tempzip,1024 * 1024 * 10)
temp.close()
tempzip.close()
zip_ref = zipfile.ZipFile(os.getcwd() + "/temp2.zip", 'r')
zip_ref.extractall(os.getcwd() + "/" + "output")
zip_ref.close()
os.remove('test.zip')
os.remove('temp2.zip')