Skip to content

Commit

Permalink
improves: zip file (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsneto authored Jul 31, 2021
1 parent 012d59d commit 5c86f4c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cereja/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from . import experimental
from ._requests import request

VERSION = "1.4.9.final.0"
VERSION = "1.5.0.final.0"

__version__ = get_version_pep440_compliant(VERSION)

Expand Down
20 changes: 7 additions & 13 deletions cereja/file/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,30 +745,24 @@ class _ZipFileIO(_FileIO):
_newline = False
_ext_allowed = ('.zip',)

def __init__(self, path_: Path, load_on_memory=False, **kwargs):
def __init__(self, path_: Path, **kwargs):

self._load_on_memory = load_on_memory
super().__init__(path_, **kwargs)

def add(self, data: Union[str, list, tuple]):
parsed = self.parse(data)
self._data += parsed

def _parse_fp(self, fp: Union[TextIO, BytesIO]) -> Any:
return self.unzip(fp.name, load_on_memory=self._load_on_memory)
return self.unzip(fp.name)

@classmethod
def unzip(cls, file_path, save_on: str = None, load_on_memory=False):
def unzip(cls, file_path, save_on: str = None, k=None, is_random=False):
with ZipFile(file_path, mode='r') as myzip:
if save_on or load_on_memory:
with tempfile.TemporaryDirectory() as tmpdirname:
unzip_dir = save_on or tmpdirname
unzip_dir = Path(unzip_dir).join(Path(myzip.filename).stem)
mkdir(unzip_dir)
myzip.extractall(unzip_dir)
if load_on_memory:
return FileIO.load_files(unzip_dir)
return myzip.namelist()
members = sample(myzip.namelist(), k, is_random)
if save_on:
mkdir(save_on)
myzip.extractall(save_on, members=members)

def _save_fp(self, fp: Union[TextIO, BytesIO]) -> None:
raise NotImplementedError
Expand Down

0 comments on commit 5c86f4c

Please sign in to comment.