用户工具

站点工具


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

差别

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

到此差别页面的链接

后一修订版
前一修订版
linux:python:python在内存中进行zip操作 [2020/09/16 10:54] – 创建 zhangguolinux:python:python在内存中进行zip操作 [2026/10/13 22:51] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
-====== Python: 使用zipfile+io模块在内存中进行zip操作 ======+# Python: 使用zipfile+io模块在内存中进行zip操作 
 +## - 网上参考代码 
 +转自原文:https://www.cnblogs.com/yaoyu126/p/3700593.html
  
- 
-转自原文:https://www.cnblogs.com/yaoyu126/p/3700593.html 
  
  
-<code python>+```python
 #!/usr/bin/env python #!/usr/bin/env python
 #coding=utf-8 #coding=utf-8
行 71: 行 71:
     filebytes = yy_unzip_base64zipfile(filename)     filebytes = yy_unzip_base64zipfile(filename)
     yy_file_write_bytes(filename + '.xls', filebytes)     yy_file_write_bytes(filename + '.xls', filebytes)
-</code>+``` 
 + 
 +     
 +     
 +     
 +## - 读zip文件到内存,然后解压到内存 
 + 
 +```python 
 + 
 +#!/usr/bin/python3 
 + 
 +import zipfile 
 +import io 
 + 
 + 
 +fh = open('ab.zip', 'rb') 
 +fbuf = fh.read()   # 读取zip二进制文件内容到buffer,注意buffer溢出。 
 +fh.close() 
 + 
 +fio = io.BytesIO()   #创建一个文件io 
 + 
 +fio.write(fbuf)     #将zip二进制内容写入文件io 
 + 
 + 
 + 
 +# zfile = zipfile.ZipFile('ab.zip', 'r') 
 +zfile = zipfile.ZipFile(fio, 'r')   # 直接从文件io处操作,而不是指定硬盘上的文件。 
 +files = [] 
 +for i in zfile.filelist: 
 +    files.append((i.filename, zfile.read(i.filename))) 
 + 
 +for tmp in files: 
 +    print(f'{tmp}')    # output: ('a.txt', b' a\n') 
 +    fname = tmp[0] 
 +    fh = open(fname) 
 +    _buff = fh.read() 
 +    print(f'from file {fname}, read all = {_buff}') 
 +``` 
 + 
 +     
 +     
 + 
 + 
 +## - 读zip文件然后AES加密写到文件,读加密后的文件解密到内存,然后在内存解压zip文件 
 + 
 +```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}') 
 +``` 
 + 
linux/python/python在内存中进行zip操作.1600224844.txt.gz · 最后更改: (外部编辑)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki