Last active
October 14, 2019 07:28
-
-
Save Hootrix/cf3e75b1fa6d3d404bc99787f89687f1 to your computer and use it in GitHub Desktop.
快速读取zip压缩包内的文件 支持在线url以及本地path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests,tempfile, zipfile,os | |
def read_file_for_zip(zip_url, callback=None): | |
""" | |
读取zip包内的文件 | |
:param zip_url:zip路径/url | |
:param callback:读取操作的回调函数 若函数返回false 则不会读取下一个文件 | |
:return: | |
""" | |
with tempfile.TemporaryFile('w+b') as tmpfile: # 生成临时文件 | |
# 判断是否为本地文件 | |
if os.path.isfile(zip_url): | |
#进行本地复制。没必要 | |
# with open(zip_url,'rb') as f: | |
# while True: | |
# chunk = f.read(1024) | |
# if not chunk: | |
# break | |
# tmpfile.write(chunk) | |
tmpfile = zip_url | |
else:#进行http请求 | |
r = requests.get(zip_url, stream=True) | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: | |
tmpfile.write(chunk) | |
assert zipfile.is_zipfile(tmpfile), '不是zip文件' | |
zf = zipfile.ZipFile(tmpfile) | |
for name in zf.namelist(): # list e.g. ['Brave Browser.url', 'Express VPN.url', 'ssl.txt', 'What is my IP.url'] | |
if callable(callback): | |
# zf.read(name) #读取 | |
if callback(name, zf) is False:# 函数返回false 会终止下一个文件的读取 | |
break | |
### 例子 | |
def cb(filename,context): | |
if filename.endswith('.txt'): | |
print(context.read(filename).decode('utf-8')) | |
# print( context.read(filename)) | |
return False #终止下一个文件的读取 | |
read_file_for_zip('https://cdn-01.openload.cc/S9Y7m488n8/22c3c58b-1571037628/ssl_proxies.zip',cb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment