4

How to extract 7z zip file in python .Please some one let me know is there any library for that.

I have install libarchive library in python 2.7.3 version . But i am not able to use that library.

Ibney Hasan
  • 71
  • 1
  • 1
  • 4
  • 1
    Please provide more detailed information about the problem. Why are you not able to use the library? – timo.rieber Oct 28 '14 at 07:55
  • libarchive library installed in under site-packeges. e.g.C:\Python27\Lib\site-packages\libarchive-0.4.3-py3.2.egg\libarchive Archive.reader module does not exists . How to use exact class – Ibney Hasan Oct 28 '14 at 08:25
  • Check out what I do in extractcode in ScanCode here: https://github.com/nexB/scancode-toolkit/blob/e45f8f401ca22a15fda6a78dff317e8acf94038e/src/extractcode/sevenzip.py This is a wrapper to the command line 7zip for Windows/Linux/Mac – Philippe Ombredanne May 12 '16 at 05:08

5 Answers5

5

You can use PyLZMA and py7zlib libraries to extract 7z file or try executing shell scripts to extract zip file using python subprocess module.

hamidfzm
  • 4,595
  • 8
  • 48
  • 80
  • please share me how to install PyLZMA and py7zlip library in python 2.7.3 version. – Ibney Hasan Oct 28 '14 at 08:25
  • For windows you can use http://www.lfd.uci.edu/~gohlke/pythonlibs/#pylzma libraries but for linux I think you should build it from source on your own. But take a look at http://www.joachim-bauch.de/projects/pylzma/ – hamidfzm Oct 28 '14 at 08:33
  • 1
    Thanks I have installed in windows .Please share to me any example of 7z zip extraction.That will be helpfull for me – Ibney Hasan Oct 28 '14 at 08:41
  • There is also `libarchive` which can do this: https://pypi.org/project/libarchive/ – cdabel Oct 04 '19 at 20:42
3

I use command like C:\Program Files\7-Zip\7z.exe x <filename> in my C++ project. You can run it in Python like so:

import subprocess
subprocess.call(r'"C:\Program Files\7-Zip\7z.exe" x ' + archive_name + ' -o' + folder_name_to_extract)

or 32-bit version:

subprocess.call(r'"C:\Program Files (x86)\7-Zip\7z.exe" x ' + archive_name + ' -o' + folder_name_to_extract)
KevinA
  • 619
  • 6
  • 25
Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
0

According to the Python doc (about the subprocess), you might rather to use the recommanded function run (such as in this exemple).

from subprocess import run
run('C:\\Program Files\\7-Zip\\7zG.exe x'+ archive_name + ' -o' + folder_name_to_extract)`

PS0 : One adivce, don't forget to escape the chars in the full path; it's could help a lot specially under Windows. Otherwise the OS couldn't find 7zip (or another program).

PS1 : Apparently, the comments are difficult to wrote... The display wasn't the same (like group all the text just in only one line) and via the touch enter, the post will be publish (unfinished). The system from stackoverflow.com is wrong because I wanted to just add a few lines and not publish it. And also because no, on the moment, I wasn't finish to write (this post).

Wagner_SOFC
  • 117
  • 4
  • This didn't work for me, the contents of the archive were never extracted to the output folder. and using `subprocess.call` I get `TypeError: bufsize must be an integer` – jerrythebum Aug 03 '18 at 08:45
0

This worked for me in Windows. The string you want to shoot for is something like this:

C:/Egain_ETL/7-Zip/7z.exe e "C:/Egain_ETL/EG_DATA_EXTRACT_2017-11-25 09-45-10-627.7z" -p"Dev@123" -o"C:/Egain_ETL/"

Notice the call to the exe and options are unquoted, everything else is double quoted.

Sample Code:

import subprocess

Z_Location = 'C:/Egain_ETL/7-Zip/7z.exe'
Extract_File = 'C:/Egain_ETL/EG_DATA_EXTRACT_2017-11-25 09-45-10-627.7z'
Extract_PW = 'Dev@123'
Extact_Folder = 'C:/Egain_ETL/'

Extract_Target = Z_Location + ' e ' + '"' + Extract_File + '"' + ' -p' + '"' + Extract_PW + '"' + ' -o' + '"' + Extact_Folder + '"'

subprocess.run(Extract_Target)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
-2

!apt-get install p7zip-full !p7zip -d file_name.tar.7z

Try the above steps