#!/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 buffer def 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 files def 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: 使用zipfile+io模块在内存中进行zip操作的更多相关文章

  1. C# 图像处理:复制屏幕到内存中,拷屏操作

    /// <summary> /// 复制屏幕到内存中 /// </summary> /// <returns>返回内存流</returns> publi ...

  2. python的异步IO模块

    asyncio模块:示例一 import asyncio @asyncio.coroutine def func1(): print('before...func1......') yield fro ...

  3. python学习之io模块

    class io.BytesIO([initial_bytes]) 他是一个_io.BytesIO对象. 用这个类的实例可以操作内存缓冲区中的字节流. >>> s = 'hello' ...

  4. python深浅拷贝以及数据在内存中储存方法

    要搞懂深浅拷贝,首先要明白数据在内存里的储存方法. 一个变量的储存,首先是变量名加上储存内容的ID,通过ID去找到变量名所对应的内容, 当我们对数据进行赋值时,其实是把内容的整体地址赋给别的变量名(相 ...

  5. python基础学习笔记——模块

    自定义模块 我们今天来学习一下自定义模块(也就是私人订制),我们要自定义模块,首先就要知道什么是模块啊 一个函数封装一个功能,比如现在有一个软件,不可能将所有程序都写入一个文件,所以咱们应该分文件,组 ...

  6. python多线程与_thread模块

    进程与线程 1.进程:计算机程序只是存储在磁盘中的可执行二进制(或其他类型)的文件.只有把他们加载到内存中并被操作系统调用,才具有其生命周期.进程则是一个执行中的程序.每个进程都拥有自己的地址空间,内 ...

  7. SQL Server 内存中OLTP内部机制概述(三)

    ----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...

  8. Python从内存中使用编译后的模块

    在Windows编程的时候,有些时候,我们经常会要使用一些非常规的方法,比如说从内存中加载DLL,然后使用DLL中的函数.于是就思索在用Python的时候是否能够将几个编译好的Pyc合并成一个,然后使 ...

  9. {Python之线程} 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Threading模块 九 锁 十 信号量 十一 事件Event 十二 条件Condition(了解) 十三 定时器

    Python之线程 线程 本节目录 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Thr ...

随机推荐

  1. jmeter http get 登录

      登录页url:http://www.ablesky.com/login.do?fromurl=http%3A%2F%2Fwww.ablesky.com%2Findex.do 登录参数用firebu ...

  2. SQL Server Reporting Services 自定义数据处理扩展DPE(Data Processing Extension)

    最近在做SSRS项目时,遇到这么一个情形:该项目有多个数据库,每个数据库都在不同的服务器,但每个数据库所拥有的数据库对象(table/view/SPs/functions)都是一模一样的,后来结合网络 ...

  3. STL源码剖析之_allocate函数

    SGI STL提供的标准std::allocator中的_allocate函数代码如下: template<class T> inline T* _allocate(ptrdiff_t s ...

  4. 如何判断Linux是否适合你

    如果你厌烦了windows,或者想延长旧pc的寿命,也或者想给小孩使用一款os,linux都能满足你. 开源操作系统linux的关键优势: - 免费的. - 大部分linux软件也是免费的. - li ...

  5. Excel文件上传

    *&---------------------------------------------------------------------* *& FORM FRM_UPDATA_ ...

  6. 【学习】ABAP OLE 对EXCEL的处理

    原文:http://blog.sina.com.cn/s/blog_7229b9c00100opx2.html -------------------------------------------- ...

  7. Least Common Multiple

    地址:http://www.codewars.com/kata/5259acb16021e9d8a60010af/train/python 题目: Write a function that calc ...

  8. Windows 下启动Apache服务(转)

    Win下apache出现问题:“No services installed”安装完apache之后(不是按照默认路径安装的,我的是 D:\ )右下方那个小羽毛图标是没有启动的,左键不好使,而且提示“N ...

  9. Cocos2d-x中常用粒子编辑器ParticleDesigner测试例子

    打开 ParticleDesigner 随意选择一种效果 选择save. 类型选择 cocosd(plist)类型保存至桌面 命名为myplist导出至桌面 选择右侧Emitter Config可设置 ...

  10. 3行3列表格 table实现,div+css实现

    table实现: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...