Spaces:
Build error
Build error
Commit
·
39f5ef5
1
Parent(s):
bde49e1
Upload 2 files
Browse files
unzipper/lib/extractor/__init__.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ===================================================================== #
|
| 2 |
+
# Copyright (c) 2022 Itz-fork #
|
| 3 |
+
# #
|
| 4 |
+
# This program is distributed in the hope that it will be useful, #
|
| 5 |
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
| 6 |
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
|
| 7 |
+
# See the GNU General Public License for more details. #
|
| 8 |
+
# #
|
| 9 |
+
# You should have received a copy of the GNU General Public License #
|
| 10 |
+
# along with this program. If not, see <http://www.gnu.org/licenses/> #
|
| 11 |
+
# ===================================================================== #
|
| 12 |
+
|
| 13 |
+
from os import path, mkdir
|
| 14 |
+
from .errors import ExtractionFailed
|
| 15 |
+
from unzipper.helpers_nexa.utils import run_shell_cmds, run_cmds_on_cr
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class Extractor:
|
| 19 |
+
"""
|
| 20 |
+
Unzip archives using 7z and zstd
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
def __init__(self) -> None:
|
| 24 |
+
pass
|
| 25 |
+
|
| 26 |
+
async def extract(self, arc_path: str, out: str, password: str = "", splitted: bool = False):
|
| 27 |
+
"""
|
| 28 |
+
Extract archive using either 7z or zstd
|
| 29 |
+
|
| 30 |
+
Parameters:
|
| 31 |
+
|
| 32 |
+
- `arc_path` - Archive path
|
| 33 |
+
- `out` - Output path
|
| 34 |
+
- `password` - Password to use incase if the archive is password protected
|
| 35 |
+
- `splitted` - Pass True if the archive is a splitted archive which usually ends with .001 (+) extensions
|
| 36 |
+
"""
|
| 37 |
+
if path.splitext(arc_path)[1] == ".zst":
|
| 38 |
+
mkdir(out)
|
| 39 |
+
ex = await self._ext_zstd(out, arc_path)
|
| 40 |
+
await self.__check_output(ex)
|
| 41 |
+
return ex
|
| 42 |
+
else:
|
| 43 |
+
ex = await self._ext_7z(out, arc_path, password, splitted)
|
| 44 |
+
await self.__check_output(ex)
|
| 45 |
+
return ex
|
| 46 |
+
|
| 47 |
+
async def _ext_7z(self, out: str, arc_path: str, password: str = "", splitted: bool = False):
|
| 48 |
+
if password:
|
| 49 |
+
command = f"7z x -o\"{out}\" -p\"{password}\" \"{arc_path}\" -y"
|
| 50 |
+
else:
|
| 51 |
+
command = f"7z x -o\"{out}\" \"{arc_path}\" -y"
|
| 52 |
+
command += " -tsplit" if splitted else ""
|
| 53 |
+
return await run_cmds_on_cr(run_shell_cmds, command)
|
| 54 |
+
|
| 55 |
+
async def _ext_zstd(self, out: str, arc_path: str):
|
| 56 |
+
command = f"zstd -f --output-dir-flat \"{out}\" -d \"{arc_path}\""
|
| 57 |
+
return await run_cmds_on_cr(run_shell_cmds, command)
|
| 58 |
+
|
| 59 |
+
async def __check_output(self, out: str):
|
| 60 |
+
if any(e in out for e in ["Error", "Can't open as archive"]):
|
| 61 |
+
raise ExtractionFailed
|
unzipper/lib/extractor/errors.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Exceptions raised by the Extractor class
|
| 2 |
+
|
| 3 |
+
class ExtractionFailed(Exception):
|
| 4 |
+
def __init__(self) -> None:
|
| 5 |
+
super().__init__("Extraction failed due to an unknown error! Please make sure that your archive isn't corrupted")
|