pythongzip压缩文件
⑴ python 中如何压缩文件,并指定文件的压缩之后的大小。
这个简单啊。你先压缩成一个ZIP文件。比如 example.zip
然后用python将它分割成,5个文件。e1,e2,e3,e4,e5
邮件发出去后,对方收到邮件,另存附件,然后在目录下运行
e1+e2+e3+e4+e5 example.zip
此时windows就将依次将5个文件复制到同一个文件里去。
⑵ 如何通过Python压缩解压缩zip文件
解压缩
importzipfile
importos
defun_zip(file_name):
"""unzipzipfile"""
zip_file=zipfile.ZipFile(file_name)
ifos.path.isdir(file_name+"_files"):
pass
else:
os.mkdir(file_name+"_files")
fornamesinzip_file.namelist():
zip_file.extract(names,file_name+"_files/")
zip_file.close()
打包
zipfile.ZipFile('xxx.zip','a/w/x').write('xxx.txt')
'w'以截断并写入新文件'a'以附加到现有文件,或'x'以专门创建和写入新文件。
⑶ python如何判断一个文件是否为gzip文件
本文实例讲述了Python实现压缩与解压gzip大文件的方法。分享给大家供大家参考,具体如下:
#encoding=utf-8
#author: walker
#date: 2015-10-26
#summary: 测试gzip压缩/解压文件
import gzip
BufSize = 1024*8
def gZipFile(src, dst):
fin = open(src, 'rb')
fout = gzip.open(dst, 'wb')
in2out(fin, fout)
def gunZipFile(gzFile, dst):
fin = gzip.open(gzFile, 'rb')
fout = open(dst, 'wb')
in2out(fin, fout)
def in2out(fin, fout):
while True:
buf = fin.read(BufSize)
if len(buf) < 1:
break
fout.write(buf)
fin.close()
fout.close()
if __name__ == '__main__':
src = r'D:\tmp\src.txt'
dst = r'D:\tmp\src.txt.gz'
ori = r'D:\tmp\ori.txt'
gZipFile(src, dst)
print('gZipFile over!')
gunZipFile(dst, ori)
print('gunZipFile over!')
也可以简单地封装成一个类:
class GZipTool:
def __init__(self, bufSize):
self.bufSize = bufSize
self.fin = None
self.fout = None
def compress(self, src, dst):
self.fin = open(src, 'rb')
self.fout = gzip.open(dst, 'wb')
self.__in2out()
def decompress(self, gzFile, dst):
self.fin = gzip.open(gzFile, 'rb')
self.fout = open(dst, 'wb')
self.__in2out()
def __in2out(self,):
while True:
buf = self.fin.read(self.bufSize)
if len(buf) < 1:
break
self.fout.write(buf)
self.fin.close()
self.fout.close()
⑷ python怎样压缩和解压缩ZIP文件
1、python使用zipfile模块压缩和解压ZIP文件
2、读取zip文件
首先,通过zipfile模块打开指定zip文件,如:
zpfd
=
zipfile.ZipFile(path,
mode='r')
对于zipfile,其标志与open所用的打开文件标志有所不同,不能识别
'rb'。
然后,读取zip文件中的内容,zipfile对象提供一个read(name)的方法,name为zip文件中的一个文件入口,执行完成之后,将返回读出的内容,如:
for
filename
in
zpfd.namelist():
tmpcont
=
zpfd.read(filename)
print
'len(tmpcont)',
'tmpcont'
需要注意的是,读取zip文件时,只能读取内容!
⑸ python如何读取通过gzip压缩的字符串
import osimport gzip # 那是因为你调用了read方法,而这个方法会把文件一股脑儿读取出来的# 为了便于你迭代,你可以在这里使用一个生成器def read_gz_file(path): if os.path.exists(path): with gzip.open(path, 'rt') as pf: for line in pf: yield line else: print('the path [{}] is not exist!'.format(path)) con = read_gz_file('abc.gz')if getattr(con, '__iter__', None): for line in con: print(line, end = '')
⑹ python压缩成tar
Python压缩文件为tar、gzip的方源码。需要应用到os、tarfile、gzip、string、shutil这几个Python类库中的方法。不同于Python Gzip压缩与解压模块,今天我们要用自己的方法实现压...
⑺ java gzip 和 python gzip 有什么区别
一个zip可以内藏多个文件 狭义的gzip仅对单个文件压缩,不能打包多个文件。 tar.gzip或tgz可以打包多个文件,属于固实压缩,压缩比较高,但随机存取单个文件的效率不如zip..
⑻ python 解压缩tar.gz文件 判断文件是否正常
Python自带的tarfile模块可以方便读取tar归档文件,牛b的是可以处理使用gzip和bz2压缩归档文件tar.gz和tar.bz2。
对于gz格式gz文件一般不加密常见的是这种.tar.gz格式的 对于tar格式 在python标准库我没找到关于检测加密的方法
⑼ 用python写一个程序实现字符串压缩的方法
import
StringIOimport
gzipcompresseddata
=
gzip方式压缩的字符串(html)compressedstream
=
StringIO.StringIO(compresseddata)gzipper
=
gzip.GzipFile(fileobj=compressedstream)data
=
gzipper.read()
#
data就是解压后的数据一个简单的例子1
import
urllib2
2
from
StringIO
import
StringIO
3
import
gzip
4
5
def
loadData(url):
6
request
=
urllib2.Request(url)
7
request.add_header('Accept-encoding',
'gzip')
8
response
=
urllib2.urlopen(request)
9
if
response.info().get('Content-Encoding')
==
'gzip':10
print
'gzip
enabled'11
buf
=
StringIO(response.read())12
f
=
gzip.GzipFile(fileobj=buf)13
data
=
f.read()14
else:15
data
=
response.read()16
return
data