forked from cycodehq/cycode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip_file.py
More file actions
33 lines (24 loc) · 929 Bytes
/
Copy pathzip_file.py
File metadata and controls
33 lines (24 loc) · 929 Bytes
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
import os.path
from zipfile import ZipFile, ZIP_DEFLATED
from io import BytesIO
class InMemoryZip(object):
def __init__(self):
# Create the in-memory file-like object
self.in_memory_zip = BytesIO()
self.zip = ZipFile(self.in_memory_zip, "a", ZIP_DEFLATED, False)
def append(self, filename, unique_id, content):
# Write the file to the in-memory zip
if unique_id:
filename = concat_unique_id(filename, unique_id)
self.zip.writestr(filename, content)
def close(self):
self.zip.close()
# to bytes
def read(self) -> bytes:
self.in_memory_zip.seek(0)
return self.in_memory_zip.read()
def concat_unique_id(filename: str, unique_id: str) -> str:
if filename.startswith(os.sep):
# remove leading slash to join path correctly
filename = filename[len(os.sep):]
return os.path.join(unique_id, filename)