Skip to content
Permalink
Browse files

add function unrar files (relies on 7z)

  • Loading branch information...
sdpython committed May 25, 2017
1 parent c8f5534 commit 84a65aace6e7fa028c2c2feeda7bc39a3aad339b
Binary file not shown.
@@ -27,7 +27,7 @@

from src.pyquickhelper.loghelper import fLOG
from src.pyquickhelper.pycode import get_temp_folder
from src.pyquickhelper.filehelper import gzip_files, zip_files, zip7_files, download, unzip_files, ungzip_files, un7zip_files
from src.pyquickhelper.filehelper import gzip_files, zip_files, zip7_files, download, unzip_files, ungzip_files, un7zip_files, unrar_files
from src.pyquickhelper.pycode import is_travis_or_appveyor


@@ -56,12 +56,12 @@ def test_compress_helper(self):
return

res = unzip_files(rz)
assert isinstance(res, list)
self.assertTrue(isinstance(res, list))
self.assertEqual(len(res), 1)
if not isinstance(res[0][1], (typbytes, str)):
raise TypeError(type(res[0][1]))
assert res[0][0].endswith(
"_unittests/ut_filehelper/test_compress_helper.py")
self.assertTrue(res[0][0].endswith(
"_unittests/ut_filehelper/test_compress_helper.py"))

# binary
rg = gzip_files(None, [f], fLOG=fLOG)
@@ -70,12 +70,12 @@ def test_compress_helper(self):
raise TypeError(type(rg))

res = ungzip_files(rg)
assert isinstance(res, list)
self.assertTrue(isinstance(res, list))
self.assertEqual(len(res), 1)
if not isinstance(res[0][1], (typbytes, str)):
raise TypeError(type(res[0][1]))
assert res[0][0].endswith(
"_unittests/ut_filehelper/test_compress_helper.py")
self.assertTrue(res[0][0].endswith(
"_unittests/ut_filehelper/test_compress_helper.py"))

def test_compress_helper_text(self):
fLOG(
@@ -95,7 +95,7 @@ def test_compress_helper_text(self):
raise TypeError(type(rg))

res = ungzip_files(rg, encoding="utf-8")
assert "test_compress_helper_text" in res
self.assertTrue("test_compress_helper_text" in res)

def test_uncompress_7zip(self):
fLOG(
@@ -107,7 +107,7 @@ def test_uncompress_7zip(self):
# use github version, not pypi version (2016-11-11)
# this version does not include a fix to read file produced by the
# latest version of 7z
assert pylzma
self.assertTrue(pylzma)

if is_travis_or_appveyor() == "appveyor":
warnings.warn(
@@ -152,12 +152,12 @@ def test_compress_7zip(self):
from py7zlib import COMPRESSION_METHOD_COPY
fLOG("***", COMPRESSION_METHOD_COPY)
res = un7zip_files(out7)
assert isinstance(res, list)
self.assertTrue(isinstance(res, list))
self.assertEqual(len(res), 1)
fLOG(res[0][0])
if not isinstance(res[0][1], (typbytes, str)):
raise TypeError(type(res[0][1]))
assert res[0][0].endswith("ftplib.html")
self.assertTrue(res[0][0].endswith("ftplib.html"))

fold = get_temp_folder(__file__, "temp_compress_7zip2")
res = un7zip_files(out7, where_to=fold, fLOG=fLOG)
@@ -170,6 +170,29 @@ def test_compress_7zip(self):
if not s.endswith("_unittests/ut_filehelper/temp_compress_7zip2/ftplib.html"):
raise Exception(res[0])

def test_uncompress_rar(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
# fold = get_temp_folder(__file__, "temp_compress_helper")

if sys.version_info[0] == 2:
typbytes = bytearray
else:
typbytes = bytes

fold = get_temp_folder(__file__, "temp_compress_rar")
rz = os.path.join(fold, "..", "data", "rar5-blake.rar")
res = unrar_files(rz, where_to=fold)
self.assertTrue(isinstance(res, list))
self.assertEqual(len(res), 2)
if not isinstance(res[0][1], (typbytes, str)):
raise TypeError(type(res[0][1]))
res[0] = res[0].replace("\\", "/")
if not res[0].endswith("ut_filehelper/temp_compress_rar/stest1.txt"):
raise Exception(res[0])


if __name__ == "__main__":
unittest.main()
@@ -4,7 +4,7 @@
"""
import os
from .anyfhelper import change_file_status, read_content_ufs
from .compression_helper import zip_files, gzip_files, zip7_files, unzip_files, ungzip_files, un7zip_files
from .compression_helper import zip_files, gzip_files, zip7_files, unzip_files, ungzip_files, un7zip_files, unrar_files
from .download_helper import get_url_content_timeout, InternetException
from .encrypted_backup import EncryptedBackup
from .encryption import decrypt_stream, encrypt_stream
@@ -13,6 +13,7 @@
from ..loghelper.flog import noLOG, run_cmd
from .fexceptions import FileException
from ..texthelper.diacritic_helper import remove_diacritics
from .synchelper import explore_folder

if sys.version_info[0] == 2:
from codecs import open
@@ -388,3 +389,36 @@ def un7zip_files(zipf, where_to=None, fLOG=noLOG, fvalid=None, remove_space=True
elif not info.filename.endswith("/"):
files.append(tos)
return files


def unrar_files(zipf, where_to=None, fLOG=noLOG, fvalid=None, remove_space=True):
"""
Unrar files from a rar archive compress with 7z.
Very basic. Use 7z.
@param zipf archive (or bytes or BytesIO)
@param where_to destination folder (can be None, the result is a list of tuple)
@param fLOG logging function
@param fvalid function which takes two paths (zip name, local name) and return True if the file
must be unzipped, False otherwise, if None, the default answer is True
@param remove_space remove spaces in created local path (+ ``',()``)
@return list of unzipped files
.. versionadded:: 1.5
"""
if sys.platform.startswith("win"):
exe = r"C:\Program Files\7-Zip\7z.exe"
if not os.path.exists(exe):
raise FileNotFoundError("unable to find: {0}".format(exe))
else:
exe = "7z"

if where_to is None:
where_to = os.path.abspath(".")
cmd = '"{0}" x "{1}" -o{2}'.format(exe, zipf, where_to)
out, err = run_cmd(cmd, wait=True, fLOG=fLOG)
if len(err) > 0:
raise FileException(
"Unable to unrar file '{0}'\nOUT\n{1}\nERR\n{2}".format(zipf, out, err))

return explore_folder(where_to)[1]

0 comments on commit 84a65aa

Please sign in to comment.
You can’t perform that action at this time.