-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArchive.py
More file actions
73 lines (53 loc) · 1.64 KB
/
Copy pathArchive.py
File metadata and controls
73 lines (53 loc) · 1.64 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
#!/usr/bin/env python3
from abc import ABCMeta, abstractmethod
from io import BytesIO
import os
import zipfile
import rarfile
def is_image(filename):
return filename.lower().endswith("jpg") or \
filename.lower().endswith("jpeg") or \
filename.lower().endswith("png") or \
filename.lower().endswith("gif")
class Archive(metaclass=ABCMeta):
@abstractmethod
def list(self):
pass
@abstractmethod
def open(self, filename):
pass
class Rar(Archive):
def __init__(self, filename):
self.rar = rarfile.RarFile(filename)
self.path = filename
def list(self):
return sorted([x for x in self.rar.namelist() if is_image(x)])
def open(self, filename):
imagefile = self.rar.open(filename)
image = BytesIO()
image.write(imagefile.read())
imagefile.close()
image.seek(0)
return image
class Tree(Archive):
def __init__(self, dirname):
self.path = dirname
def list(self):
return sorted([os.path.join(self.path, filename)
for filename in os.listdir(self.path)
if is_image(filename)])
def open(self, filename):
image = open(filename, "rb")
return image
class Zip(Archive):
def __init__(self, filename):
self.zip = zipfile.ZipFile(filename)
self.path = filename
def list(self):
return sorted([x for x in self.zip.namelist() if is_image(x)])
def open(self, filename):
imagefile = self.zip.open(filename)
image = BytesIO()
image.write(imagefile.read())
image.seek(0)
return image