首页 技术 正文
技术 2022年11月9日
0 收藏 369 点赞 3,917 浏览 1623 个字
#!/usr/bin/env python
#coding=utf-8
'''
版权所有 (c) 2014 yao_yu (http://blog.csdn.net/yao_yu_126)
本代码采用MIT许可
使用io.BytesIO()在内存中压缩,解压缩zip文件
2014-04-30 yaoyu 创建
'''import zipfile
import os
import io
from base64 import standard_b64decode
try:
from .yy_file import *
except:
from yy_file import *__all__ = ['yy_zip_files2buffer', 'yy_zip_files', 'yy_unzip_buffer2files', 'yy_unzip_base64zipfile']def yy_zip_files2buffer(files):
'''文件压缩到内存中'''
buffer = io.BytesIO()
zfile = zipfile.ZipFile(buffer, 'w', zipfile.ZIP_DEFLATED, allowZip64=False)
for i in files:
if os.path.isfile(i):
zfile.write(i, os.path.basename(i))
zfile.close()
buffer.seek(0)
return bufferdef yy_zip_files(zipfilename, files):
'''压缩文件列表到zip文件'''
yy_file_write_bytes(zipfilename, yy_zip_files2buffer(files).read())def yy_unzip_buffer2files(buffer):
'''从内存压缩文件中读取文件信息'''
zfile = zipfile.ZipFile(buffer, 'r')
files = []
for i in zfile.filelist:
files.append((i.filename, zfile.read(i.filename)))
return filesdef yy_unzip_base64zipfile(szFile):
'''从base64压缩的zip文件中读取第一个文件二进制内容'''
bytescontent = yy_file_read_bytes(szFile)
for (find, replace) in ((b' ', b'\n'),
(b' ', b'\r')):
if find in bytescontent:
bytescontent = bytescontent.replace(find, replace)
filebytes = standard_b64decode(bytescontent)
fileinfos = yy_unzip_buffer2files(io.BytesIO(filebytes))
if fileinfos:
return fileinfos[0][1]if __name__ == '__main__':
sourcedir = '/Volumes/Data/Document/百度云同步盘/Develop/Python/Calculator'
files = (os.path.join(sourcedir, i) for i in os.listdir(sourcedir))
yy_zip_files(os.path.join(sourcedir, 'dist', 'r.zip'), files)
#print(yy_unzip_buffer2files(yy_zip_files2buffer(files))) filename = '/Volumes/Data/Download/aaa'
filebytes = yy_unzip_base64zipfile(filename)
yy_file_write_bytes(filename + '.xls', filebytes)
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899