python使用zipfile递归压缩和解压缩文件
import shutil,zipfile,os
class ToolModel(object):
def dfs_get_zip_file(self,input_path, result, ignore=[]):
'''
递归目录
:param input_path: 输入路径
:param result: 列表
:param ignore: 忽略文件或目录名
:return:
'''
files = os.listdir(input_path)
for file in files:
filePath = input_path + '/' + file
if file in ignore:
continue
if os.path.isdir(filePath):
self.dfs_get_zip_file(filePath, result, ignore)
else:
result.append(filePath)
def zip_path(self,input_path, output_path, ignore=[]):
'''
:param input_path: 输入路径 /app/adminkit
:param output_path: 输出路径 /app/adminkit.zip
:param ignore: 忽略文件或目录名
:return:
'''
outdir = os.path.dirname(output_path)
if not os.path.isdir(outdir):
os.makedirs(outdir)
f = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
filelists = []
self.dfs_get_zip_file(input_path, filelists, ignore)
for file in filelists:
file = file.replace('\\', '/')
input_path = input_path.replace('\\', '/')
f.write(file, file.replace(input_path, ''))
f.close()
return output_path
def unzip(self,filename: str,dirname):
'''
解压缩
:param filename: 压缩文件名
:param dirname: 解压缩输出目录
:return:
'''
try:
file = zipfile.ZipFile(filename)
file.extractall(dirname)
file.close()
# 递归修复编码
self.rename(dirname)
except:
print(f'{filename} unzip fail')
def rename(self,pwd: str, filename=''):
"""压缩包内部文件有中文名, 解压后出现乱码,进行恢复"""
path = f'{pwd}/{filename}'
if os.path.isdir(path):
for i in os.scandir(path):
self.rename(path, i.name)
newname = filename.encode('cp437').decode('gbk')
os.rename(path, f'{pwd}/{newname}')
def del_file(self,filepath):
"""
删除指定路径下的所有文件和文件夹
:param filepath: 路径
:return:
"""
del_list = os.listdir(filepath)
for f in del_list:
file_path = os.path.join(filepath, f)
if os.path.isfile(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
python使用zipfile递归压缩和解压缩文件的更多相关文章
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- C# - WinFrm应用程序调用SharpZipLib实现文件的压缩和解压缩
前言 本篇主要记录:VS2019 WinFrm桌面应用程序调用SharpZipLib,实现文件的简单压缩和解压缩功能. SharpZipLib 开源地址戳这里. 准备工作 搭建WinFrm前台界面 添 ...
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- 在C#中利用SharpZipLib进行文件的压缩和解压缩收藏
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net(http://www.icsharpcode.net/OpenSource/SharpZipL ...
- iOS中使用ZipArchive压缩和解压缩文件-备
为什么我需要解压缩文件 有许多原因能解释为什么我要在工程中使用压缩和解压缩功能,下面是几个常见的原因: 苹果App Store的50M下载限制 苹 果公司出于流量的考虑,规定在非WIFI环境下,限制用 ...
- IO操作之使用zip包压缩和解压缩文件
转自:http://www.cdtarena.com/java.htmlJava API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件
我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...
- C#利用SharpZipLib进行文件的压缩和解压缩
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...
- C#压缩和解压文件
这里用两种方法实现C#压缩和解压文件 1.使用System.IO.Compression名称空间下的相关类(需引用 System.IO.Compression.FileSystem和System.IO ...
随机推荐
- Python Ethical Hacking - Intercepting and Modifying Packets
INTERCEPTING & MODIFYING PACKETS Scapy can be used to: Create packets. Analyze packets. Send/rec ...
- Docker部署LNMP完整教程
在Docker中部署LNMP环境可以分为以下几个步骤: 安装Docker 创建镜像 创建Dockerfile build Docerfile 复制/修改配置文件 运行镜像,并映射端口 为了方便分布式部 ...
- 014.Nginx跨域配置
一 跨域概述 1.1 同源策略 同源策略是一个安全策略.同源,指的是协议,域名,端口相同.浏览器处于安全方面的考虑,只允许本域名下的接口交互,不同源的客户端脚本,在没有明确授权的情况下,不能读写对方的 ...
- MySQL数据库的安装方法
- 瀑布流的实现纯CSS实现Jquery实现
瀑布流的实现 注:本文部分图片自百度下载,如有侵权,联系删图. 首先,选择几张图片布局到HTML内容中.HTML如下所示. <div class="wrapper"> ...
- 京东阅读(web)体验优化
京东有电子书可以购买,可以多端阅读.比如PC客户端,移动端,以及本文提到的PC网站端. 先换个镜头,读书要记笔记(电子版本), 方便以后查阅. 镜头换回来,但是,我们为了方便肯定是想复制,下载啊,分享 ...
- 硬核干货:5W字17张高清图理解同步器框架AbstractQueuedSynchronizer
前提 并发编程大师Doug Lea在编写JUC(java.util.concurrent)包的时候引入了java.util.concurrent.locks.AbstractQueuedSynchro ...
- 前端学习(五):body标签(三)
进击のpython ***** 前端学习--body标签 接下来的内容就比较多了,各位看官且听我慢慢道来... ... 使用a标签,链接到另一个页面 网页中<a>标签,全称:anchor. ...
- Vue脚手架创建项目出现 (Failed to download repo vuejs-templates/webpack: Response code 404)
搭建好(脚手架2.X版本)环境像往常一样使用vue init webpack xxxx 创建项目可以是没多久就开始报错了 报错结果就是:vue-cli · Failed to download rep ...
- List<Activity> lists的关闭finish()
public class App extends Application { private static List<Activity> lists = new ArrayList< ...