1

I'm using Python 2.7. I have the following code

file = ZipFile("D:\\Project\\text.evt.gz")

Its giving the following error

**"BadZipfile: File is not a zip file"**

While trying to extract the file manually,i am getting the following error

**"Unexpected end of data"**

Is it possible to extract the evt file irrespective of this error in Python? Basically i want to extract the compressed evt file to a specific destination folder.

Dharman
  • 30,962
  • 25
  • 85
  • 135
user3138864
  • 63
  • 1
  • 10

1 Answers1

1

The file extension "gz" suggests this is a gzip file, which is distinct from a zip file. Try the gzip module.

#!/usr/bin/env python3

import gzip
import sys


with gzip.open('/Some/file/somewhere.gz', 'rb') as f:
    file_content = f.read()

for i in file_content:
    print(chr(i),file=sys.stdout, end='')

Though it might make more sense to look into a system call to 7zip if you need this to be at all universal, and you don't have to manipulate the contents.

Hack Saw
  • 2,741
  • 1
  • 18
  • 33
  • gzip does not offer the extract method to copy the extracted file to different location.Any idea how to copy the extracted evt file to different location using gzip? – user3138864 Jul 05 '18 at 08:39
  • 1
    @user3138864, the gzip module gives you a generic file-like object; you can use `shutil.copyfileobj()` with it to copy from that to any other file object on Python (without requiring enough memory to store the whole content in RAM, as the answer currently does). – Charles Duffy Jul 05 '18 at 19:14
  • @HackSaw, I'd advise against 7zip. It's not widely available on Linux, and security bugs are... not unheard of (particularly in the rar module -- sure, the OP isn't *intending* to use that module here, but that doesn't mean that it can't be selected via a crafted input file). – Charles Duffy Jul 05 '18 at 19:15
  • OP is on Windows, note the drive letter. – Hack Saw Jul 05 '18 at 19:16
  • Sure. The security note remains pertinent. – Charles Duffy Jul 05 '18 at 19:16
  • Security notes aside, but not unloved, I also note the existence of p7zip in the Ubuntu archive. – Hack Saw Jul 05 '18 at 19:18
  • still iam not able to read the gz content using the gzip.. Iam getting the following error Traceback (most recent call last): File "", line 2, in file_content = f.read() File "C:\Python27\lib\gzip.py", line 261, in read self._read(readsize) File "C:\Python27\lib\gzip.py", line 303, in _read self._read_gzip_header() File "C:\Python27\lib\gzip.py", line 197, in _read_gzip_header raise IOError, 'Not a gzipped file' IOError: Not a gzipped file. Same file iam able to open using Zipfile – user3138864 Jul 12 '18 at 10:23
  • Could you run this: python -c "with open('D:\Project\text.evt.gz', 'rb') as f: print(f.read1(2))" – Hack Saw Jul 12 '18 at 21:54
  • I expect this as output: b'\x1f\x8b', thought other things are possible. – Hack Saw Jul 12 '18 at 21:55
  • python -c "with open('D:\Project\text.evt.gz', 'rb') as f: print(f.read1(2)). output is pk – user3138864 Jul 16 '18 at 10:00
  • I think you don't have a gzip file, and you possibly don't have a compressed file at all. Looking at the "magic" sequences, a start of "pk" isn't among them. Maybe look at it with Notepad and see if it's just a text file? – Hack Saw Jul 18 '18 at 18:58
  • Wait, is the output "pk" or "PK"? Try this: rename the file to text.evt.zip, and try your ZipFile version. – Hack Saw Jul 19 '18 at 02:44