7zip - python extract uncompressed data from 7z-file -
i have several csv-files, of compressed others not, in 7z archive. want read csv files , save content in database. however, whenever py7zlib attemts read data csv file not compressed, error data error during decompression
.
import os import py7zlib scr = r'y:\pathtoarchive' z7file = 'archivename.7z' open(os.path.join(scr,z7file),'rb') f: archive = py7zlib.archive7z(f) names = archive.filenames mem in names: obj = archive.getmember(mem) print obj.compressed # prints none uncompressed data try: data = obj.read() except exception er: print er # prints data error during decompression # whenever obj.compressed none
the error happens in
file "c:\anaconda\lib\site-packages\py7zlib.py", line 608, in read data = getattr(self, decoder)(coder, data, level) file "c:\anaconda\lib\site-packages\py7zlib.py", line 671, in _read_lzma return self._read_from_decompressor(coder, dec, input, level, checkremaining=true, with_cache=true) file "c:\anaconda\lib\site-packages\py7zlib.py", line 646, in _read_from_decompressor tmp = decompressor.decompress(data) valueerror: data error during decompression
so, how can extract uncompressed data 7z-archive?
though couldn't figure out problem seemed be, found workaround solved ultimate goal obtain data csv-files 7z-archive. 7-zip comes command line tool. communicating tool via subprocess module, automatically extract files wihsed extract without problems
import subprocess import py7zlib archiveman = r'c:\program files\7-zip\7z' # 7z.exe comes 7-zip archivepath = r'c:\path\to\archive.7z' open(archivepath,'rb') f: archive = py7zlib.archive7z(f) names = archive.filenames name in names: _ = subprocess.check_output([archiveman, 'e', archivepath, '-o{}'.format(r'c:\destination\of\copy'), name])
the different commands can used 7z can found here.
Comments
Post a Comment