I have a small script that I wrote with a help of * community to unzip the archive.
我有一个小脚本,我在*社区的帮助下编写了这个脚本,以解压存档。
The strange issue that I'm currently facing is that large zip files (for example, 1GB or more) before unpacking are downloaded(?) locally to the computer and only after that starting to unpack.
我目前面临的一个奇怪的问题是,在卸载之前,大的zip文件(例如,1GB或以上)在本地下载(?),然后才开始打开包。
My script is:
我的脚本:
#!/usr/bin/env python2.7
# coding=utf-8
import os
import sys
def unpack_zip(zip_file, to_dir):
if sys.platform in ('darwin', 'linux2'):
unpack = os.system('unzip %s -d %s' % (zip_file, to_dir))
if unpack != 0:
return False
return to_dir
elif 'win32' in sys.platform:
import zipfile
zf = zipfile.ZipFile(zip_file, "r")
if zf.testzip() is not None:
return False
try:
os.mkdir(to_dir)
except OSError:
pass
def get_members(zip_archive):
parts = []
for name in zip_archive.namelist():
if not name.endswith('/'):
parts.append(name.split('/')[:-1])
prefix = os.path.commonprefix(parts) or ''
if prefix:
prefix = '/'.join(prefix) + '/'
offset = len(prefix)
for zipinfo in zip_archive.infolist():
name = zipinfo.filename
if len(name) > offset:
zipinfo.filename = name[offset:]
print "Extracting: %s" % name
yield zipinfo
zf.extractall(to_dir, get_members(zf))
zf.close()
return to_dir
if __name__ == "__main__":
archive = os.path.join(os.getcwd(), "zip_file.zip")
unzip_to = os.path.join(os.getcwd(), "test_unzip")
unpack_zip(archive, unzip_to)
If you start this script it will wait for couple of minutes and only after that will begin extraction. Important note: zip file should be located at the network drive.
如果您启动这个脚本,它将等待几分钟,然后才开始提取。重要提示:zip文件应该位于网络驱动器。
My goal is to start exctracion process immediately (similar to unzip tool in Linux / Mac). Is that possible to achieve without 3rd party dependencies (only with a help of ZipFile and Python)?
我的目标是立即启动exctracion进程(类似于Linux / Mac中的unzip工具)。如果没有第三方依赖(只有在ZipFile和Python的帮助下),这可能实现吗?
Thanks!
谢谢!
1 个解决方案
#1
2
You are testing your zipped files before unpacking. The docstring to the testzip
-method is clear: 'Read all the files and check the CRC.' Delete this line, and unpacking should start immediately.
你正在测试你的压缩文件,然后开箱。testzip方法的docstring很清楚:“读取所有文件并检查CRC。”删除这条线,立即开始拆包。
#1
2
You are testing your zipped files before unpacking. The docstring to the testzip
-method is clear: 'Read all the files and check the CRC.' Delete this line, and unpacking should start immediately.
你正在测试你的压缩文件,然后开箱。testzip方法的docstring很清楚:“读取所有文件并检查CRC。”删除这条线,立即开始拆包。