在python中提取.zip [英] Extracting .zip in python

查看:118
本文介绍了在python中提取.zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python2 zipfile.ZipFile

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

使用.zip解压缩时使用相同的.zip会给出file #1: bad zipfile offset (local header sig): 0,但是会使用退出代码2进行提取.

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

使用jar -xf file.zip时,该命令以$? == 0结束,未提取任何内容.

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

使用文件即可:

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

这为zipfile提供了错误的标题

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?.._..|

正确的标题是:

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

为什么文件被列为application/octet-stream?

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

我在

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

怎么回事?这是什么文件格式?有指针吗?

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

推荐答案

您尝试过吗?

import zipfile
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()

这篇关于在python中提取.zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆