Skip to content

Commit

Permalink
dev(narugo): optimize unpack for rar and 7z format
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Jan 20, 2024
1 parent 8e7e407 commit 1807884
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
9 changes: 3 additions & 6 deletions hfutils/archive/rar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Optional

from .base import register_archive_type
from ..utils import tqdm

try:
import rarfile
Expand All @@ -11,20 +10,18 @@


def _rar_pack(directory, zip_file, silent: bool = False, clear: bool = False):
_ = directory, zip_file, silent
_ = directory, zip_file, silent, clear
raise RuntimeError('RAR format packing is not supported.')


def _rar_unpack(rar_file, directory, silent: bool = False, password: Optional[str] = None):
_ = silent
directory = os.fspath(directory)
os.makedirs(directory, exist_ok=True)
with rarfile.RarFile(rar_file, 'r') as zf:
if password is not None:
zf.setpassword(password)
progress = tqdm(zf.namelist(), silent=silent, desc=f'Unpacking {directory!r} ...')
for rarinfo in progress:
progress.set_description(rarinfo)
zf.extract(rarinfo, directory)
zf.extractall(directory)


if rarfile is not None:
Expand Down
7 changes: 2 additions & 5 deletions hfutils/archive/sevenz.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ def _7z_pack(directory, sz_file, silent: bool = False, clear: bool = False):


def _7z_unpack(sz_file, directory, silent: bool = False, password: Optional[str] = None):
_ = silent
directory = os.fspath(directory)
os.makedirs(directory, exist_ok=True)
with py7zr.SevenZipFile(sz_file, 'r', password=password) as zf:
progress = tqdm(zf.getnames(), silent=silent, desc=f'Unpacking {directory!r} ...')
for name in progress:
progress.set_description(name)
zf.extract(path=directory, targets=[name])
zf.reset()
zf.extractall(directory)


if py7zr is not None:
Expand Down

0 comments on commit 1807884

Please sign in to comment.