python2/python3 内存中打包/压缩文件
python2:(包含压缩选项,如果只打包,可以调整zipfile.ZIP_DEFLATED)
import zipfile
import StringIO
class InMemoryZip(object):
def __init__(self):
# Create the in-memory file-like object
self.in_memory_zip = StringIO.StringIO()
def append(self, filename_in_zip, file_contents):
'''Appends a file with name filename_in_zip and contents of
file_contents to the in-memory zip.'''
# Get a handle to the in-memory zip in append mode
zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)
# Write the file to the in-memory zip
zf.writestr(filename_in_zip, file_contents)
# Mark the files as having been created on Windows so that
# Unix permissions are not inferred as 0000
for zfile in zf.filelist:
zfile.create_system = 0
return self
def read(self):
'''Returns a string with the contents of the in-memory zip.'''
self.in_memory_zip.seek(0)
return self.in_memory_zip.read()
def writetofile(self, filename):
'''Writes the in-memory zip to a file.'''
f = file(filename, "w")
f.write(self.read())
f.close()
if __name__ == "__main__":
# Run a test
imz = InMemoryZip()
imz.append("/home/test/1.jpg",imagebuf)
imz.writetofile("test.zip")
python3:(包含打包选项,不压缩)
import zipfile
import io
class InMemoryZip(object):
def __init__(self):
self.in_memory_zip = io.BytesIO()
def append(self, filename_in_zip, file_contents):
zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_STORED, False)
zf.writestr(filename_in_zip, file_contents)
for zfile in zf.filelist:
zfile.create_system = 0
return self
def read1(self):
self.in_memory_zip.seek(0)
return self.in_memory_zip.read()
def writetofile(self, filename):
f = open(filename, "wb")
f.write(self.read1())
f.close()
if __name__ == "__main__": imz = InMemoryZip()
f1 = open('/home/yangbing/jpg/1.jpg','rb').read()
imz.append("1.jpg", f1)
f2 = open('/home/yangbing/jpg/2.jpg','rb').read()
imz.append("2.jpg",f2)
imz.writetofile("test.zip")
python2/python3 内存中打包/压缩文件的更多相关文章
- IDA远程调试 在内存中dump Dex文件
1. 首先使用调试JNI_OnLoad函数的方法,先将apk以调试状态挂起,使用IDA附加上去. 2. 然后在libdvm.so中的dvmDexFileOpenPartial函数上下一个断点 3. 然 ...
- net 编译报错:编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件
1,报错提示: 编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件. 在生成过程中保存文件是危险的,这可能会在将来导致不正确的生成输出. 是否仍然继续签出? 2,原因:licenses.l ...
- Python中zipfile压缩文件模块的使用
目录 zipfile 压缩一个文件 解压文件 高级应用 利用 zipfile 模块破解压缩文件口令:Python脚本破解压缩文件口令 zipfile Python 中 zipfile 模块提供了对 z ...
- java打包压缩文件
package com.it.simple.util; import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream ...
- CentOS 打包压缩文件 zip 命令详解
我们再linux中常见的压缩文件有.tar.gz,.zip,.gz,在linux中,你要习惯没有.rar的日子. 一下为tar,zip命令详解 tar -zcvf /home/files.tar.gz ...
- Linux就该这么学--命令集合6(打包压缩文件、文件查询搜索命令)
1.tar命令用于对文件打包压缩或解压:(tar [选项] [文件]) 打包并压缩文件:tar -czvf 压缩包名.tar.gz 文件名 解压并展开压缩包:tar -xzvf 压缩包名.tar.gz ...
- php 如何在有限的内存中读取大文件
突然遇到了一个要读取超过80M文件的需求,很悲剧的,不管是file_get_content还是file什么的,都会将读取的文件一次性加载到内存中. 正常情况下,我们可以使用fseek来读取,好处就是不 ...
- Linux中各种压缩文件
.gz格式 压缩: gzip 文件名 解压: gzip -d 欲解压文件名 gunzip 欲解压文件名 说明: 1.只能压缩文件,不能压缩目录 2.压缩和解压的时候不保留原文件 .bz2格式 压缩: ...
- python打包压缩文件夹zip+组装文件夹
无意间想到的一个需求,然后就顺手写了写,留下来,方便以后用 列表版:(基本没用,仅提供思路,字典版稍微改动可以直接用) 大体需求: 把重复的文件名进行改名,达到浏览器下载相同文件的效果 下载完成后再把 ...
随机推荐
- js中callback.call()和callback()的区别
js中callback.call()和callback()的区别在js中callback.call()和callback() 有什么区别,举个例子:function a(){alert('hello! ...
- Python 序列的修改、散列和切片
Vector类:用户定义的序列类型 我们将使用组合模式实现 Vector 类,而不使用继承.向量的分量存储在浮点数数组中,而且还将实现不可变扁平序列所需的方法. p.p1 { margin: 0.0p ...
- 轻量级ORM框架 QX_Frame.Bantina(二、框架使用方式介绍)
轻量级ORM框架QX_Frame.Bantina系列讲解(开源) 一.框架简介 http://www.cnblogs.com/qixiaoyizhan/p/7417467.html 二.框架使用方式介 ...
- Mongodb 监测
原文地址:伍仪洲的博客 介绍 为什么要进行监控状态,因为在实际的情况中可能会发生一下无法预计的情况,比如阻塞的问题,阻塞的原因会有很多种情况造成,如果当我们查询文档的时候发生了阻塞,那么就会影响到后面 ...
- Intel VT-X
VT-x是intel运用Virtualization虚拟化技术中的一个指令集,是CPU的硬件虚拟化技术,VT可以同时提升虚拟化效率和虚拟机的安全性,在x86平台上的VT技术,一般称之为VT-x,而在I ...
- 一起来学Go --- (go的变量)
变量 变量是几乎所有编程语言中最基本的组成元素,从根本上说,变量相当于是一块数据存储空间的命名,程序可以通过定义一个变量来申请一块数据存储空间,之后可以通过引用变量名来使用这块存储空间.go语言中的变 ...
- 部署和使用kibana
背景 上一篇介绍了在阿里云上部署ES,本文将主要介绍ELK的可视化工具Kibana的部署和使用.主要分为三个步骤来实现最终呈现: 1.导入数据到ES: 2.部署kibana并完成配置: 3.使用kib ...
- Java线程池之ThreadPoolExecutor
前言 线程池可以提高程序的并发性能(当然是合适的情况下),因为对于没有线程的情况下,我们每一次提交任务都新建一个线程,这种方法存在不少缺陷: 1. 线程的创建和销毁的开销非常高,线程的创建需要时间, ...
- hdu1695 GCD(莫比乌斯入门题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意: 给出n.m.k ,求出1<=x<=n, 1<=y<=m 且gcd ...
- java集合1