使用Python解压zip、rar文件
<h3>解压 zip 文件</h3><p><strong><span style="color: rgba(255, 0, 0, 1)">基本解压操作</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> zipfile
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]])
mode:可选 r,w,a 代表不同的打开文件的方式;r 只读;w 重写;a 添加
compression:指出这个 zipfile 用什么压缩方法,默认是 ZIP_STORED,另一种选择是 ZIP_DEFLATED;
allowZip64:bool型变量,当设置为True时可以创建大于 2G 的 zip 文件,默认值 True;
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
zip_file </span>=<span style="color: rgba(0, 0, 0, 1)"> zipfile.ZipFile(path)
zip_list </span>= zip_file.namelist() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 得到压缩包里所有文件</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> f <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> zip_list:
zip_file.extract(f, folder_abs) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 循环解压文件到指定目录</span>
<span style="color: rgba(0, 0, 0, 1)">
zip_file.close() </span><span style="color: rgba(0, 128, 0, 1)"># 关闭文件,必须有,释放内存</span></pre>
</div>
<p> </p>
<p><strong><span style="color: rgba(255, 0, 0, 1)">其他方法</span></strong></p>
<div class="cnblogs_code">
<pre>zipfile.is_zipfile(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx.zip</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 判断文件是否是个有效的zipfile</span>
zipfile.namelist(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx.zip</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 列表,存储zip文件中所有子文件的path(相对于zip文件包而言的)</span>
zipfile.infolist(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx.zip</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 列表,存储每个zip文件中子文件的ZipInfo对象</span>
zipfile.printdir() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 打印输出zip文件的目录结构,包括每个文件的path,修改时间和大小</span>
zipfile.open(name[,mode[,pwd]]) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取一个子文件的文件对象,可以对其进行read,readline,write等操作</span>
<span style="color: rgba(0, 0, 0, 1)">zipfile.setpassword(psw),为zip文件设置默认密码
zipfile.testzip() </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 读取zip中的所有文件,验证他们的CRC校验和。返回第一个损坏文件的名称,如果所有文件都是完整的就返回None</span>
zipfile.write(filename[,arcname[,compression_type]]) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 将zip外的文件filename写入到名为arcname的子文件中(当然arcname也是带有相对zip包的路径的),打开方式为w或a</span>
zipfile.extract(member, path=None, pwd=None) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 解压一个zip中的文件,path为解压存储路径,pwd为密码</span>
zipfile.extractall(path[,pwd]) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 解压zip中的所有文件,path为解压存储路径,pwd为密码</span></pre>
</div>
<p> </p>
<h3><span style="font-size: 14px"><strong style="font-size: 1.17em">解压 rar 文件</strong></span></h3>
<p> Python 本身不支持 rar 文件的解压,需要先安装相关依赖才可使用</p>
<ul>
<li>安装 unrar 模块:pip install unrar</li>
<li>下载安装 unrar library,网址:<em>http://www.rarlab.com/rar/UnRARDLL.exe</em> 按照默认安装路径安装</li>
<li>将安装后文件夹中的 X64 文件夹加入环境变量(默认路径为 C:\Program Files (x86)\UnrarDLL\x64)</li>
<li>系统变量中新建变量,变量名输入 UNRAR_LIB_PATH,变量值为 C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll(32位系统下的变量值为C:\Program Files (x86)\UnrarDLL\UnRAR.dll)</li>
<li>将 winrar 的目录下的 unrar.exe 复制到 Python 路径的 Scripts 文件夹下</li>
<li>重启PyCharm</li>
</ul>
<p><strong><span style="color: rgba(255, 0, 0, 1)">基本操作</span></strong></p>
<div class="cnblogs_code">
<pre>rf = rarfile.RarFile(_rarfile, mode=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">r</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> mode的值只能为'r'</span>
rf_list = rf.namelist() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 得到压缩包里所有的文件</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">rar文件内容</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, rf_list)
</span><span style="color: rgba(0, 0, 255, 1)">for</span> f <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> rf_list:
rf.extract(f, folder_abs)</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 循环解压,将文件解压到指定路径</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 一次性解压所有文件到指定目录</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> rf.extractall(path) # 不传path,默认为当前目录</span></pre>
</div>
<p> </p>
<h3>小案例</h3>
<p> 已知一个有密码的压缩包,但是忘了密码是多少,只记得密码是纯数字,用脚本轻松找回密码。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> rarfile,zipfile
jy_path</span>=r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\test\忘记密码的压缩包.zip</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
pwd_l </span>= <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 事先准备好密码,zipfile的pwd需要用bytes的数据类型</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 针对rar文件</span>
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> rar_attack(f):
file_handle </span>=<span style="color: rgba(0, 0, 0, 1)"> rarfile.RarFile(f)
</span><span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> pwd_l:
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
file_handle.extractall(path</span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">ttt</span><span style="color: rgba(128, 0, 0, 1)">'</span>,pwd=<span style="color: rgba(0, 0, 0, 1)">i)
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Yes!,pwd=</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,i)
</span><span style="color: rgba(0, 0, 255, 1)">break</span>
<span style="color: rgba(0, 0, 255, 1)">except</span><span style="color: rgba(0, 0, 0, 1)"> Exception as e:
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">No,</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,e,i)
file_handle.close()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 针对zip文件</span>
<span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> zip_attack(f):
file_handle </span>=<span style="color: rgba(0, 0, 0, 1)"> zipfile.ZipFile(f)
</span><span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> pwd_l:
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(i)
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
file_handle.extractall(path</span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">ttt</span><span style="color: rgba(128, 0, 0, 1)">'</span>,pwd=i)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> path用于给定解压后文件放置的路径</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Yes!,pwd=</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, i)
</span><span style="color: rgba(0, 0, 255, 1)">break</span>
<span style="color: rgba(0, 0, 255, 1)">except</span><span style="color: rgba(0, 0, 0, 1)"> Exception as e:
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">No,</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, e)
file_handle.close()
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
zip_attack(jy_path)</span><span style="color: rgba(0, 128, 0, 1)"><br></span></pre>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<p>本文来自博客园,作者:''竹先森゜,转载请注明原文链接:https://www.cnblogs.com/zhuminghui/p/11699313.html</p><br><br>
来源:https://www.cnblogs.com/zhuminghui/p/11699313.html
頁:
[1]