python2:(包含压缩选项,如果只打包,可以调整zipfile.ZIP_DEFLATED)

  1. import zipfile
  2. import StringIO
  3. class InMemoryZip(object):
  4. def __init__(self):
  5. # Create the in-memory file-like object
  6. self.in_memory_zip = StringIO.StringIO()
  7. def append(self, filename_in_zip, file_contents):
  8. '''Appends a file with name filename_in_zip and contents of
  9. file_contents to the in-memory zip.'''
  10. # Get a handle to the in-memory zip in append mode
  11. zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)
  12. # Write the file to the in-memory zip
  13. zf.writestr(filename_in_zip, file_contents)
  14. # Mark the files as having been created on Windows so that
  15. # Unix permissions are not inferred as 0000
  16. for zfile in zf.filelist:
  17. zfile.create_system = 0
  18. return self
  19. def read(self):
  20. '''Returns a string with the contents of the in-memory zip.'''
  21. self.in_memory_zip.seek(0)
  22. return self.in_memory_zip.read()
  23. def writetofile(self, filename):
  24. '''Writes the in-memory zip to a file.'''
  25. f = file(filename, "w")
  26. f.write(self.read())
  27. f.close()
  28. if __name__ == "__main__":
  29. # Run a test
  30. imz = InMemoryZip()
  31. imz.append("/home/test/1.jpg",imagebuf)
  32. imz.writetofile("test.zip")

  python3:(包含打包选项,不压缩)

  1. import zipfile
  2. import io
  3. class InMemoryZip(object):
  4. def __init__(self):
  5. self.in_memory_zip = io.BytesIO()
  6. def append(self, filename_in_zip, file_contents):
  7. zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_STORED, False)
  8. zf.writestr(filename_in_zip, file_contents)
  9. for zfile in zf.filelist:
  10. zfile.create_system = 0
  11. return self
  12. def read1(self):
  13. self.in_memory_zip.seek(0)
  14. return self.in_memory_zip.read()
  15. def writetofile(self, filename):
  16. f = open(filename, "wb")
  17. f.write(self.read1())
  18. f.close()
  19. if __name__ == "__main__":
  20.  
  21. imz = InMemoryZip()
  22. f1 = open('/home/yangbing/jpg/1.jpg','rb').read()
  23. imz.append("1.jpg", f1)
  24. f2 = open('/home/yangbing/jpg/2.jpg','rb').read()
  25. imz.append("2.jpg",f2)
  26. imz.writetofile("test.zip")

  

python2/python3 内存中打包/压缩文件的更多相关文章

  1. IDA远程调试 在内存中dump Dex文件

    1. 首先使用调试JNI_OnLoad函数的方法,先将apk以调试状态挂起,使用IDA附加上去. 2. 然后在libdvm.so中的dvmDexFileOpenPartial函数上下一个断点 3. 然 ...

  2. net 编译报错:编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件

    1,报错提示: 编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件. 在生成过程中保存文件是危险的,这可能会在将来导致不正确的生成输出. 是否仍然继续签出? 2,原因:licenses.l ...

  3. Python中zipfile压缩文件模块的使用

    目录 zipfile 压缩一个文件 解压文件 高级应用 利用 zipfile 模块破解压缩文件口令:Python脚本破解压缩文件口令 zipfile Python 中 zipfile 模块提供了对 z ...

  4. java打包压缩文件

    package com.it.simple.util; import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream ...

  5. CentOS 打包压缩文件 zip 命令详解

    我们再linux中常见的压缩文件有.tar.gz,.zip,.gz,在linux中,你要习惯没有.rar的日子. 一下为tar,zip命令详解 tar -zcvf /home/files.tar.gz ...

  6. Linux就该这么学--命令集合6(打包压缩文件、文件查询搜索命令)

    1.tar命令用于对文件打包压缩或解压:(tar [选项] [文件]) 打包并压缩文件:tar -czvf 压缩包名.tar.gz 文件名 解压并展开压缩包:tar -xzvf 压缩包名.tar.gz ...

  7. php 如何在有限的内存中读取大文件

    突然遇到了一个要读取超过80M文件的需求,很悲剧的,不管是file_get_content还是file什么的,都会将读取的文件一次性加载到内存中. 正常情况下,我们可以使用fseek来读取,好处就是不 ...

  8. Linux中各种压缩文件

    .gz格式 压缩: gzip 文件名 解压: gzip -d 欲解压文件名 gunzip 欲解压文件名 说明: 1.只能压缩文件,不能压缩目录 2.压缩和解压的时候不保留原文件 .bz2格式 压缩: ...

  9. python打包压缩文件夹zip+组装文件夹

    无意间想到的一个需求,然后就顺手写了写,留下来,方便以后用 列表版:(基本没用,仅提供思路,字典版稍微改动可以直接用) 大体需求: 把重复的文件名进行改名,达到浏览器下载相同文件的效果 下载完成后再把 ...

随机推荐

  1. JSON数据表示格式简介(JavaScript对象表示法)

    [1] JSON简介    > JSON全称 JavaScript Object Notation    > 类似于JS中对象的创建的方法    > JSON和XML一样,都是一种表 ...

  2. struts2整合JFreechart 饼图、折线图、柱形图

    struts2整合JFreechart 饼图.折线图.柱形图 上效果图: 当然可以将数据导出图片格式存储.具体下的链接里的文件有保存成图片的操作. 因为是strust2整合JFreechart,所以s ...

  3. .net Path 类

    检索文件扩展名.   GetExtension 检索文件的完全限定路径. GetFullPath 检索路径中的文件名和扩展名. GetFileName 只检索路径中的文件名. GetFileNameW ...

  4. FBI Warning

    FBI Warning... --------------------------- 电影中的片头部分:<正宗的FBI Warning>: ========== 翻译: fbi warni ...

  5. Andrew Ng机器学习课程笔记--week9(下)(推荐系统&协同过滤)

    本周内容较多,故分为上下两篇文章. 本文为下篇. 一.内容概要 1. Anomaly Detection Density Estimation Problem Motivation Gaussian ...

  6. latex 生成pdf

    我个人还是比较推崇传统的方法:先生成dvi,在生成pdf. 直接在winEdt中点击最下方的Windows Command Prompt, 否则从cmd进入命令行的话,还要进入tex文件夹,好麻烦. ...

  7. AC Analysis

    1.从Options---sheet properties—circuit—show all调出来节点编号 :

  8. SQL查询每个部门工资前三名的员工信息

    --通用sql select deptno, ename, sal from emp e1 where ( ) from emp e2 where e2.deptno=e1.deptno and e2 ...

  9. SpringMVC知识点小结

    SpringMVC: 1.SpringMVC和Spring的关系: 软件开发的三层架构: web层[表示层.表现层]---->Service层---->Dao[DataBase Acces ...

  10. 容器在 Weave 中如何通信和隔离?- 每天5分钟玩转 Docker 容器技术(65)

    上一节我们分析了 Weave 的网络结构,今天讨论 Weave 的连通和隔离特性. 首先在host2 执行如下命令: weave launch 192.168.56.104 这里必须指定 host1 ...