很久以前写的python对文件的压缩操作,可以参考参考,下面是代码。
'''
Created on 2011-8-7
@author: WGY
'''
import zipfile
import os.path
import os
class ZFile(object):
def __init__(self, filename, mode='r', basedir=''):
self.filename = filename
self.mode = mode
if self.mode in ('w', 'a'):
self.zfile = zipfile.ZipFile(filename, self.mode, compression=zipfile.ZIP_DEFLATED)
else:
self.zfile = zipfile.ZipFile(filename, self.mode)
self.basedir = basedir
if not self.basedir:
self.basedir = os.path.dirname(filename)
def addfile(self, path, arcname=None):
path = path.replace('\', '/')
if not arcname:
if path.startswith(self.basedir):
arcname = path[len(self.basedir):]
else:
arcname = ''
self.zfile.write(path, arcname)
def addfiles(self, paths):
for path in paths:
if isinstance(path, tuple):
self.addfile(*path)
else:
self.addfile(path)
def close(self):
self.zfile.close()
def extract_to(self, path):
for p in self.zfile.namelist():
self.extract(p, path)
def extract(self, filename, path):
if not filename.endswith('/'):
f = os.path.join(path, filename)
dir = os.path.dirname(f)
if not os.path.exists(dir):
os.makedirs(dir)
file(f, 'wb').write(self.zfile.read(filename))
def create(zfile, files):
z = ZFile(zfile, 'w')
z.addfiles(files)
z.close()
def extract(zfile, path):
z = ZFile(zfile)
z.extract_to(path)
z.close()
create('e:/js/a.zip',('e:/js/test.txt'))
转
http://wgyblog.com/html/artcode/195.html
分享到:
相关推荐
利用Python操作对文件进行压缩。
python压缩文件与解压缩文件资料收集,课外练习需要
Python压缩文件的脚本
python 文件夹压缩(包含空文件夹)、ini文件读写(解决大小写不敏感)
c++调用python接口实现压缩拆分文件功能,压缩和拆分的功能都是python实现,有pythton源码,直接运行c++就可以。
Python应用实战案例:Python压缩NC格式(附代码).zip
使用请看我的文章
将压缩文件隐藏到图片的python实现,更改筛选标准可以推广到融合各种文件
PDF压缩工具,目前只针对纯PDF图片文件,可批量压缩文件,压缩后的文件默认生成在D:/smallPDF
python3 多线程压缩文件
使用python自带的zipfile和tarfile模块做压缩文件夹/解压缩zip文件功能
Python压缩解压zip文件.doc Python压缩解压zip文件.doc Python压缩解压zip文件.doc Python压缩解压zip文件.doc Python压缩解压zip文件.doc Python压缩解压zip文件.doc Python压缩解压zip文件.doc Python压缩解压zip...
使用Python来压缩文件和解压缩文件
主要介绍了Python实现多级目录压缩与解压文件的方法,涉及Python针对文件路径的遍历、判断以及文件压缩、解压缩等相关操作技巧,需要的朋友可以参考下
6. 将编码后的位串转换为字节序列写入压缩文件 解压原理: 1. 从压缩文件读取编码后的位串 2. 去除填充,提取实际的编码文本 3. 对编码文本进行解码,恢复原始的像素值序列 4. 将解码得到的一维像素值序列 reshape ...
python 的 zipfile 提供了非常便捷的方法来压缩和解压 zip 文件。 例如,在py脚本所在目录中,有如下文件: readability/readability.js readability/readability.txt readability/readability-print.css ...
python实现各种算法的文档 Data Structures and Algorithms with Object-Oriented Design Patterns in Python
python Windows 压缩备份文件(支持多文件夹)
本文实例讲述了python压缩文件夹内所有文件为zip文件的方法。分享给大家供大家参考。具体如下: 用这段代码可以用来打包自己的文件夹为zip,我就用这段代码来备份 import zipfile z = zipfile.ZipFile('my-...