Extracting .zip in python
Asked Answered
F

1

1

I got BadZipfile: Bad magic number for file header error while extracting a .zip using python2 zipfile.ZipFile

Same .zip when extracted with unzip gives file #1: bad zipfile offset (local header sig): 0 but gets extracted with exit code 2.

When using jar -xf file.zip the command completes with $? == 0 with nothing being extracted.

Using file gives:

file -i file.zip
file.zip application/octet-stream; charset=binary

This gives incorrect header for zipfile

$ hexdump -C file.zip | head -10
00000000  50 67 f0 de 1e 7a 29 e4  93 56 3f 11 a2 5f b6 97  |Pg...z)..V?.._..|

Correct header is:

00000000  50 4b 03 04 14 00 08 08  08 00 28 3e 4b 4b 00 00  |PK........(>KK..|

Why is the file listed as application/octet-stream ?

I am on

Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:    14.04
Codename:   trusty

Whats going on ? What file format is this ? Any pointers ?

Freefloating answered 28/10, 2017 at 21:19 Comment(6)
have you tried all other ways: gzip, unzip ?Ammonate
maybe archive is corrupted?Managerial
Possible duplicate of Bad magic number error with ZipFile module in PythonZeph
@Ammonate With unzip I get exit code 2 but the file gets extracted.Freefloating
file -i file.zip gives application/octet-stream. Why ?Freefloating
I added few extra details from hexdump. Which file format is this ?Freefloating
P
1

Have you tried this?

import zipfile
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()
Porky answered 28/10, 2017 at 21:40 Comment(1)
Then may be this could help [programcreek.com/python/example/3149/zipfile.BadZipfile]Porky

© 2022 - 2024 — McMap. All rights reserved.