linux:python:python在内存中进行zip操作
差别
这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
linux:python:python在内存中进行zip操作 [2020/09/16 10:54] – 创建 zhangguo | linux:python:python在内存中进行zip操作 [2023/03/17 10:12] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
====== Python: 使用zipfile+io模块在内存中进行zip操作 ====== | ====== Python: 使用zipfile+io模块在内存中进行zip操作 ====== | ||
+ | |||
+ | ===== - 网上参考代码 ===== | ||
行 72: | 行 74: | ||
yy_file_write_bytes(filename + ' | yy_file_write_bytes(filename + ' | ||
</ | </ | ||
+ | | ||
+ | | ||
+ | | ||
+ | ===== - 读zip文件到内存,然后解压到内存 ===== | ||
+ | |||
+ | <code python> | ||
+ | |||
+ | # | ||
+ | |||
+ | import zipfile | ||
+ | import io | ||
+ | |||
+ | |||
+ | fh = open(' | ||
+ | fbuf = fh.read() | ||
+ | fh.close() | ||
+ | |||
+ | fio = io.BytesIO() | ||
+ | |||
+ | fio.write(fbuf) | ||
+ | |||
+ | |||
+ | |||
+ | # zfile = zipfile.ZipFile(' | ||
+ | zfile = zipfile.ZipFile(fio, | ||
+ | files = [] | ||
+ | for i in zfile.filelist: | ||
+ | files.append((i.filename, | ||
+ | |||
+ | for tmp in files: | ||
+ | print(f' | ||
+ | fname = tmp[0] | ||
+ | fh = open(fname) | ||
+ | _buff = fh.read() | ||
+ | print(f' | ||
+ | |||
+ | |||
+ | </ | ||
+ | | ||
+ | | ||
+ | |||
+ | |||
+ | ===== - 读zip文件然后AES加密写到文件,读加密后的文件解密到内存,然后在内存解压zip文件 | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | |||
+ | import zipfile | ||
+ | import io | ||
+ | |||
+ | #coding: utf8 | ||
+ | from Crypto import Random | ||
+ | |||
+ | import sys | ||
+ | from Crypto.Cipher import AES | ||
+ | from binascii import b2a_hex, a2b_hex | ||
+ | |||
+ | class prpcrypt(): | ||
+ | def __init__(self, | ||
+ | self.key = key | ||
+ | self.mode = AES.MODE_CBC | ||
+ | |||
+ | # | ||
+ | def encrypt(self, | ||
+ | cryptor = AES.new(self.key, | ||
+ | # | ||
+ | length = 16 | ||
+ | count = len(text) | ||
+ | # print (f' | ||
+ | |||
+ | if(count % length != 0) : | ||
+ | add = length - (count % length) | ||
+ | else: | ||
+ | add = 0 | ||
+ | |||
+ | text = text + (b' | ||
+ | self.ciphertext = cryptor.encrypt(text) | ||
+ | # | ||
+ | # | ||
+ | return b2a_hex(self.ciphertext) | ||
+ | |||
+ | # | ||
+ | def decrypt(self, | ||
+ | cryptor = AES.new(self.key, | ||
+ | plain_text = cryptor.decrypt(a2b_hex(text)) | ||
+ | return plain_text.rstrip(b' | ||
+ | |||
+ | # if __name__ == ' | ||
+ | # pc = prpcrypt(' | ||
+ | # e = pc.encrypt(" | ||
+ | # d = pc.decrypt(e) | ||
+ | # print (f'e,d = {e}, {d}') | ||
+ | # e = pc.encrypt(" | ||
+ | # d = pc.decrypt(e) | ||
+ | # # print (e, d) | ||
+ | # print (f'e,d = {e}, {d}') | ||
+ | # | ||
+ | # exit(0) | ||
+ | |||
+ | |||
+ | pc = prpcrypt(' | ||
+ | |||
+ | ################ | ||
+ | fh = open(' | ||
+ | fbuf = fh.read() | ||
+ | fh.close() | ||
+ | |||
+ | fbuf_str = b2a_hex(fbuf) | ||
+ | print(f' | ||
+ | |||
+ | e = pc.encrypt(fbuf_str) | ||
+ | print(f' | ||
+ | |||
+ | fh = open(' | ||
+ | fh.write(a2b_hex(e)) | ||
+ | fh.close() | ||
+ | |||
+ | |||
+ | |||
+ | ################ | ||
+ | fh = open(' | ||
+ | e = b2a_hex(fh.read()) | ||
+ | fh.close() | ||
+ | |||
+ | d = pc.decrypt(e) | ||
+ | print(f' | ||
+ | |||
+ | |||
+ | |||
+ | fio = io.BytesIO() | ||
+ | # fio.write(fbuf) | ||
+ | fio.write(a2b_hex(d)) | ||
+ | |||
+ | |||
+ | |||
+ | # zfile = zipfile.ZipFile(' | ||
+ | zfile = zipfile.ZipFile(fio, | ||
+ | files = [] | ||
+ | for i in zfile.filelist: | ||
+ | files.append((i.filename, | ||
+ | |||
+ | for tmp in files: | ||
+ | print(f' | ||
+ | fname = tmp[0] | ||
+ | fh = open(fname) | ||
+ | _buff = fh.read() | ||
+ | print(f' | ||
+ | |||
+ | </ | ||
+ |
linux/python/python在内存中进行zip操作.1600224844.txt.gz · 最后更改: 2023/03/17 10:12 (外部编辑)