用户工具

站点工具


linux:python:python在内存中进行zip操作

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
linux:python:python在内存中进行zip操作 [2020/09/16 11:30] – [2. 读zip文件到内存,然后解压到内存] zhangguolinux:python:python在内存中进行zip操作 [2023/03/17 10:12] (当前版本) – 外部编辑 127.0.0.1
行 110: 行 110:
     print(f'from file {fname}, read all = {_buff}')     print(f'from file {fname}, read all = {_buff}')
  
 +
 +</code>
 +    
 +    
 +
 +
 +===== - 读zip文件然后AES加密写到文件,读加密后的文件解密到内存,然后在内存解压zip文件  =====
 +
 +<code python>
 +#!/usr/bin/python3
 +
 +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, key):
 +        self.key = key
 +        self.mode = AES.MODE_CBC
 +     
 +    #加密函数,如果text不是16的倍数【加密文本text必须为16的倍数!】,那就补足为16的倍数
 +    def encrypt(self, text):
 +        cryptor = AES.new(self.key, self.mode, self.key)
 +        #这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用
 +        length = 16
 +        count = len(text)
 +        # print (f'count = {count}')
 +
 +        if(count % length != 0) :
 +                add = length - (count % length)
 +        else:
 +            add = 0
 +
 +        text = text + (b'\0' * add)
 +        self.ciphertext = cryptor.encrypt(text)
 +        #因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
 +        #所以这里统一把加密后的字符串转化为16进制字符串
 +        return b2a_hex(self.ciphertext)
 +     
 +    #解密后,去掉补足的空格用strip() 去掉
 +    def decrypt(self, text):
 +        cryptor = AES.new(self.key, self.mode, self.key)
 +        plain_text = cryptor.decrypt(a2b_hex(text))
 +        return plain_text.rstrip(b'\0')
 + 
 +# if __name__ == '__main__':
 +#     pc = prpcrypt('keyskeyskeyskeys'     #初始化密钥
 +#     e = pc.encrypt("0123456789ABCDEF")
 +#     d = pc.decrypt(e)                     
 +#     print (f'e,d = {e}, {d}')
 +#     e = pc.encrypt("00000000000000000000000000")
 +#     d = pc.decrypt(e)                  
 +#     # print (e, d)
 +#     print (f'e,d = {e}, {d}')
 +
 +# exit(0)
 +
 +
 +pc = prpcrypt('keyskeyskeyskeys'     #初始化密钥
 +
 +################ read file, then encrypt ################
 +fh = open('ab.zip', 'rb')
 +fbuf = fh.read()
 +fh.close()
 +
 +fbuf_str = b2a_hex(fbuf)
 +print(f'fbuf_str = {fbuf_str}')
 +
 +e = pc.encrypt(fbuf_str)
 +print(f'e = {e}')
 +
 +fh = open('_ab.e', 'wb')
 +fh.write(a2b_hex(e))
 +fh.close()
 +
 +
 +
 +################ read the entrypted file, decrypt ################
 +fh = open('_ab.e', 'rb')
 +e = b2a_hex(fh.read())
 +fh.close()
 +
 +d = pc.decrypt(e)
 +print(f'd = {d}')
 +
 +
 +
 +fio = io.BytesIO()
 +# fio.write(fbuf)
 +fio.write(a2b_hex(d))
 +
 +
 +
 +# zfile = zipfile.ZipFile('ab.zip', 'r')
 +zfile = zipfile.ZipFile(fio, 'r')
 +files = []
 +for i in zfile.filelist:
 +    files.append((i.filename, zfile.read(i.filename)))   # 可以考虑在此处创建很多的BytesIO.
 +
 +for tmp in files:
 +    print(f'{tmp}')
 +    fname = tmp[0]
 +    fh = open(fname)   #此处读的是硬盘文件,不是内存文件。如果读内存文件,则需要首要创建BytesIO,
 +    _buff = fh.read()
 +    print(f'from file {fname}, read all = {_buff}')
  
 </code> </code>
  
linux/python/python在内存中进行zip操作.1600227028.txt.gz · 最后更改: 2023/03/17 10:12 (外部编辑)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki