opening corrupted .tgz in python - python

I should just unzip RESULT folder inside of output.tgz archive
#!/usr/bin/python
import os, sys, tarfile, gzip
def unZip(self,file, filename,dire):
tar = tarfile.open(filename,"r:gz")
for file in tar.getmembers():
if file.name.startswith("RESULT"):
tar.extract(file,dire)
print 'Done.'
def main():
utilities = ZipUtil()
filename = 'output.tgz'
directory = str(sys.argv[1]);
path = os.path.join(directory, filename)
utilities.unZip(directory,path,directory)
main()
and I'm getting this error
File "/usr/lib64/python2.7/tarfile.py", line 1805, in getmembers
self._load() # all members, we first have to
File "/usr/lib64/python2.7/tarfile.py", line 2380, in _load
tarinfo = self.next()
File "/usr/lib64/python2.7/tarfile.py", line 2315, in next
self.fileobj.seek(self.offset)
File "/usr/lib64/python2.7/gzip.py", line 429, in seek
self.read(1024)
File "/usr/lib64/python2.7/gzip.py", line 256, in read
self._read(readsize)
File "/usr/lib64/python2.7/gzip.py", line 303, in _read
self._read_eof()
File "/usr/lib64/python2.7/gzip.py", line 342, in _read_eof
hex(self.crc)))
IOError: CRC check failed 0xccf8af55 != 0x43cb4d43L
There are many questions like this but still now answer for this proble. The file output.tgz is downloaded and it might be corrupted this is why I guess its not working. But is there anyway around to tell tar to open it. THnak a lot!

Related

Getting error 'NotImplementedError("That compression method is not supported")' when extracting zipfile in python3.9

I have read through the Python documentation about zip files and watched a couple of videos, but everything didn't work. I'm using Kali Linux, so that the password has to be encoded in bytes.
Here is my code, with which I have tried:
import zipfile
import string
import traceback
def try_function(zip, pwd):
try:
zip.extractall(pwd=pwd.encode())
print("Yes")
except TypeError:
print("No")
z = zipfile.ZipFile("test.txt.zip")
pwd_local = "abc"
if __name__ == '__main__':
try_function(z, pwd_local)
But I always get the same error:
Traceback (most recent call last):
File "ZipWorker.py", line 22, in <module>
try_function(z, pwd_list)
File "ZipWorker.py", line 11, in crack
zip.extractall(pwd.encode())
File "/usr/lib/python3.9/zipfile.py", line 1633, in extractall
self._extract_member(zipinfo, path, pwd)
File "/usr/lib/python3.9/zipfile.py", line 1686, in _
extract_member
with self.open(member, pwd=pwd) as source, \
File "/usr/lib/python3.9/zipfile.py", line 1559, in open
return ZipExtFile(zef_file, mode, zinfo, pwd, True)
File "/usr/lib/python3.9/zipfile.py", line 797, in __init__
self._decompressor = _get_decompressor(self._compress_type)
File "/usr/lib/python3.9/zipfile.py", line 698, in
_get_decompressor
_check_compression(compress_type)
File "/usr/lib/python3.9/zipfile.py", line 678, in
_check_compression
raise NotImplementedError("That compression method is not
supported")
NotImplementedError: That compression method is not supported
Does anyone know how to do this? I'm using python3.9.
So I finally find out, why the code above doesn't work.
When you are creating a zipfile with for example 7zip, this zip file will be encrypted.
But the encryption isn't in bytes, it's encrypted in the hashes: AES-256 or ZipCrypto.

Python hashing folder contents results in memory error

Context
In an automated test case I download a folder containing
txt files, and
encrypted files with various 3 digit extensions(i.e. .001, .004)
and hash every one of these files in the following manner:
def hash_directory(path):
hashed_files = {}
if os.path.isfile(path):
md5 = hashlib.md5()
with open(path, 'rb') as f:
while True:
buf = f.read(65536)
if not buf : break
md5.update(buf)
return {os.path.basename(path):md5.hexdigest()}
else:
for dir_name, dir_names, file_names in os.walk(path):
for filename in file_names:
md5 = hashlib.md5()
file_path = os.path.join(dir_name.replace(path, '').strip('\\'),filename)
with open(os.path.join(dir_name, filename), 'rb') as f:
part = 0
while True:
buf = f.read(65536)
if not buf: break
md5.update(buf)
#if the md5 object gets larger than a quarter MB, then digest a part of the file.
if sys.getsizeof(md5)>262144:
hashed_files[file_path+'part'+str(part)] = md5.hexdigest()
part+=1
hashed_files[file_path] = md5.hexdigest()
return hashed_files
I do the same for a folder being held on a server on my network(this is the "expected"), and compare the hashed_files to determine if there are missing files in the downloaded folder, or if any files are corrupted.
Problem
I get this:
Traceback (most recent call last):
File "c:\python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "c:\python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "c:\python27\lib\site-packages\robot\run.py", line 550, in <module>
run_cli(sys.argv[1:])
File "c:\python27\lib\site-packages\robot\run.py", line 489, in run_cli
return RobotFramework().execute_cli(arguments, exit=exit)
File "c:\python27\lib\site-packages\robot\utils\application.py", line 46, in execute_cli
rc = self._execute(arguments, options)
File "c:\python27\lib\site-packages\robot\utils\application.py", line 90, in _execute
error, details = get_error_details(exclude_robot_traces=False)
File "c:\python27\lib\site-packages\robot\utils\error.py", line 47, in get_error_details
details = ErrorDetails(exclude_robot_traces=exclude_robot_traces)
File "c:\python27\lib\site-packages\robot\utils\error.py", line 60, in ErrorDetails
raise exc_value
MemoryError
We use robot framework for writing the test cases, python27, and this occurs both on my win10 16gb machine, and on remote servers running the code.
Would anyone have a sniff of what this could be ?
Does anyone know of a python tool to monitor memory as code is run ?

Tablib xlsx file badZip file issue

I am getting error on opening xlsx extension file in windows 8 using tablib library.
python version - 2.7.14
error is as follows:
python suit_simple_sheet_product.py
Traceback (most recent call last):
File "suit_simple_sheet_product.py", line 19, in <module>
data = tablib.Dataset().load(open(BASE_PATH).read())
File "C:\Python27\lib\site-packages\tablib\core.py", line 446, in load
format = detect_format(in_stream)
File "C:\Python27\lib\site-packages\tablib\core.py", line 1157, in detect_format
if fmt.detect(stream):
File "C:\Python27\lib\site-packages\tablib\formats\_xls.py", line 25, in detect
xlrd.open_workbook(file_contents=stream)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 120, in open_workbook
zf = zipfile.ZipFile(timemachine.BYTES_IO(file_contents))
File "C:\Python27\lib\zipfile.py", line 770, in __init__
self._RealGetContents()
File "C:\Python27\lib\zipfile.py", line 811, in _RealGetContents
raise BadZipfile, "File is not a zip file"
zipfile.BadZipfile: File is not a zip file
path location is as follows =
BASE_PATH = 'C:\Users\anju\Downloads\automate\catalog-5090 fabric detail and price list.xlsx'
Excel .xlsx files are actually zip files. In order for the unzip to work correctly, the file must be opened in binary mode, as such your need to open the file using:
import tablib
BASE_PATH = r'c:\my folder\my_test.xlsx'
data = tablib.Dataset().load(open(BASE_PATH, 'rb').read())
print data
Add r before your string to stop Python from trying to interpret the backslash characters in your path.

zipfile.BadZipfile: Bad CRC-32 for file | Read only file

Got a read-only file within a zip file which are password protected and I need to extract it to the /tmp directory.
I get a CRC-32 error which suggests that the file would be corrupted yet I know it isn't and is in fact a read-only file. Any Suggestions?
Error:
Traceback (most recent call last):
File "/tmp/usercode.py", line 45, in <module>
zip.extractall('/tmp',pwd = "piso")
File "/usr/lib64/python2.7/zipfile.py", line 1040, in extractall
self.extract(zipinfo, path, pwd)
File "/usr/lib64/python2.7/zipfile.py", line 1028, in extract
return self._extract_member(member, path, pwd)
File "/usr/lib64/python2.7/zipfile.py", line 1084, in _extract_member
shutil.copyfileobj(source, target)
File "/usr/lib64/python2.7/shutil.py", line 49, in copyfileobj
buf = fsrc.read(length)
File "/usr/lib64/python2.7/zipfile.py", line 632, in read
data = self.read1(n - len(buf))
File "/usr/lib64/python2.7/zipfile.py", line 672, in read1
self._update_crc(data, eof=(self._compress_left==0))
File "/usr/lib64/python2.7/zipfile.py", line 647, in _update_crc
raise BadZipfile("Bad CRC-32 for file %r" % self.name)
zipfile.BadZipfile: Bad CRC-32 for file 'alien-12.txt'
Code:
# importing required modules
from zipfile import ZipFile
# specifying the zip file name
file_name = "/tmp/alien-12.zip"
# opening the zip file in READ mode
with ZipFile(file_name, 'r') as zip:
# printing all the contents of the zip file
zip.printdir()
# extracting all the files
print('Extracting all the files now...')
zip.extractall('/tmp',pwd = "piso")
print('Done!')
If I change the line of:
zip.extractall('/tmp',pwd = "piso")
then I get the error of:
IOError: [Errno 30] Read-only file system:
Then go on to try and fix it first by trying to output what is in the zip file.
zipfile.testzip() returns which then errors
Error:
RuntimeError: File alien-12.txt is encrypted, password required for extraction

why won't recursive ftp work in this directory?

Python newb...beware!
I am trying to recursively ftp some files. In the script below, I get an error:
Traceback (most recent call last):
File "dump.py", line 7, in <module>
for root,dirs,files in recursive:
File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 880, in walk
if self.path.isdir(self.path.join(top, name)):
File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_path.py", line 133, in isdir
path, _exception_for_missing_path=False)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 860, in stat
return self._stat._stat(path, _exception_for_missing_path)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 624, in _stat
_exception_for_missing_path)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 578, in __call_with_parser_retry
result = method(*args, **kwargs)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 543, in _real_stat
lstat_result = self._real_lstat(path, _exception_for_missing_path)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 502, in _real_lstat
for stat_result in self._stat_results_from_dir(dirname):
File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 419, in _stat_results_from_dir
lines = self._host_dir(path)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_stat.py", line 411, in _host_dir
return self._host._dir(path)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 811, in _dir
descend_deeply=True)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 578, in _robust_ftp_command
self.chdir(path)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftputil.py", line 603, in chdir
ftp_error._try_with_oserror(self._session.cwd, path)
File "/usr/local/lib/python2.6/site-packages/ftputil/ftp_error.py", line 146, in _try_with_oserror
raise PermanentError(*exc.args)
ftputil.ftp_error.PermanentError: 550 /usr/local/web: No such file or directory
Debugging info: ftputil 2.6, Python 2.6.5 (linux2)
Exited: 256
I have no idea what that means. However, when I change the directory to "/path/dir2" it works and prints out "file.txt".
Here's my directory structures:
/path/dir1/another/file.txt # I get the above error with this path
/path/dir2/another/file.txt # this works fine, even though the directories have the same structure
My script:
import ftputil
ftp = ftputil.FTPHost('ftp.site.com','user','pass')
recursive = ftp.walk("/path/dir1",topdown=True,onerror=None)
for root,dirs,files in recursive:
for name in files:
print name
ftp.close
Is there a symblolic link in /path/dir1/ that points back to /usr/local/web?
If you want to step over this error you could put a try catch block in and log...
ftp = ftputil.FTPHost('ftp.site.com','user','pass')
try:
recursive = ftp.walk("/path/dir1",topdown=True,onerror=None)
for root,dirs,files in recursive:
for name in files:
print name
except Error e:
print "Error: %s occurred" % (e)
ftp.close
the above will catch all errors. you can also import the specific error your getting and do this...
import ftputil.ftp_error.PermanentError as PermanentError
ftp = ftputil.FTPHost('ftp.site.com','user','pass')
try:
recursive = ftp.walk("/path/dir1",topdown=True,onerror=None)
for root,dirs,files in recursive:
for name in files:
print name
except PermanentError e:
print "Permanent Error: %s occurred" % (e)
ftp.close

Categories

Resources