Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read contents of 7z file using python

How can I read and save the contents of 7z. I use Python 2.7.9, I can extract or Archive like this, but I can't read contents in python, I only listing the file's contents in CMD

import subprocess import os  source = 'filename.7z' directory = 'C:\Directory' pw = '123456' subprocess.call(r'"C:\Program Files (x86)\7-Zip\7z.exe" x '+source +' -o'+directory+' -p'+pw) 
like image 695
Ken Kem Avatar asked Sep 26 '15 13:09

Ken Kem


People also ask

How do I load a 7z file in Python?

You can use either libarchive or pylzma. If you can upgrade to python3. 3+ you can use lzma, which is in the standard library. Note that lzma doesn't work with 7z archives, only single files.

Is 7z better than zip?

In 2011, TopTenReviews found that the 7z compression was at least 17% better than ZIP, and 7-Zip's own site has since 2002 reported that while compression ratio results are very dependent upon the data used for the tests, "Usually, 7-Zip compresses to 7z format 30–70% better than to zip format, and 7-Zip compresses to ...

How do I unzip a 7z file without WinZip?

Press Ctrl + A to select all of the files and folders in the 7z file. Click the button that says "1-click Unzip". Select "Unzip to PC or Cloud," then select a destination folder. The default option is to create a new folder with the name of the 7z file, which should suffice.


2 Answers

If you can use python 3, there is a useful library, py7zr, which supports 7zip archive compression, decompression, encryption and decryption.

import py7zr with py7zr.SevenZipFile('sample.7z', mode='r') as z:     z.extractall() 
like image 54
Zhou Hongbo Avatar answered Sep 19 '22 18:09

Zhou Hongbo


I ended up in this situation where I was forced to use 7z, and also needed to know exactly which files were extracted from each zip archive. To deal with this, you can check the output of the call to 7z and look for the filenames. Here's what the output of 7z looks like:

$ 7z l sample.zip  7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21 p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs x64)  Scanning the drive for archives: 1 file, 472 bytes (1 KiB)  Listing archive: sample.zip  -- Path = sample.zip Type = zip Physical Size = 472     Date      Time    Attr         Size   Compressed  Name ------------------- ----- ------------ ------------  ------------------------ 2018-12-01 17:09:59 .....            0            0  sample1.txt 2018-12-01 17:10:01 .....            0            0  sample2.txt 2018-12-01 17:10:03 .....            0            0  sample3.txt ------------------- ----- ------------ ------------  ------------------------ 2018-12-01 17:10:03                  0            0  3 files 

and how to parse that output with python:

import subprocess  def find_header(split_line):     return 'Name' in split_line and 'Date' in split_line  def all_hyphens(line):     return set(line) == set('-')  def parse_lines(lines):     found_header = False     found_first_hyphens = False     files = []     for line in lines:          # After the header is a row of hyphens         # and the data ends with a row of hyphens         if found_header:             is_hyphen = all_hyphens(''.join(line.split()))              if not found_first_hyphens:                 found_first_hyphens = True                 # now the data starts                 continue              # Finding a second row of hyphens means we're done             if found_first_hyphens and is_hyphen:                 return files          split_line = line.split()          # Check for the column headers         if find_header(split_line):             found_header=True             continue          if found_header and found_first_hyphens:             files.append(split_line[-1])             continue      raise ValueError("We parsed this zipfile without finding a second row of hyphens")    byte_result=subprocess.check_output('7z l sample.zip', shell=True) str_result = byte_result.decode('utf-8') line_result = str_result.splitlines() files = parse_lines(line_result) 
like image 39
Kyle Heuton Avatar answered Sep 19 '22 18:09

Kyle Heuton