{"id":1135841,"date":"2025-01-08T21:29:43","date_gmt":"2025-01-08T13:29:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135841.html"},"modified":"2025-01-08T21:29:45","modified_gmt":"2025-01-08T13:29:45","slug":"python%e4%bb%a3%e7%a0%81%e5%a6%82%e4%bd%95%e8%a7%a3%e5%8e%8b7z%e5%8e%8b%e7%bc%a9%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135841.html","title":{"rendered":"python\u4ee3\u7801\u5982\u4f55\u89e3\u538b7z\u538b\u7f29\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104404\/2da9c58b-7efc-4401-9845-bbc19fdddc62.webp\" alt=\"python\u4ee3\u7801\u5982\u4f55\u89e3\u538b7z\u538b\u7f29\u5305\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u89e3\u538b7z\u538b\u7f29\u5305\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4f7f\u7528<code>py7zr<\/code>\u5e93\u3001<code>pylzma<\/code>\u5e93\u3001\u4ee5\u53ca\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/strong>\u5176\u4e2d\uff0c<strong><code>py7zr<\/code>\u5e93\u662f\u4e00\u79cd\u975e\u5e38\u65b9\u4fbf\u4e14\u6613\u7528\u7684\u9009\u62e9<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e13\u95e8\u7528\u4e8e\u5904\u74067z\u683c\u5f0f\u7684\u538b\u7f29\u5305\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u5f3a\u5927\u7684\u63a5\u53e3\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u89e3\u538b7z\u538b\u7f29\u5305\uff0c\u5e76\u63a2\u8ba8\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>py7zr<\/code>\u5e93\u89e3\u538b7z\u538b\u7f29\u5305<\/h3>\n<\/p>\n<p><p><code>py7zr<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5904\u74067z\u538b\u7f29\u5305\u7684Python\u5e93\uff0c\u652f\u6301\u538b\u7f29\u548c\u89e3\u538b\u7f29\u64cd\u4f5c\u3002\u5b83\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u5e76\u4e14\u4e0d\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u76847z\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5<code>py7zr<\/code><\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>py7zr<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py7zr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u538b7z\u6587\u4ef6\u7684\u57fa\u672c\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>py7zr<\/code>\u89e3\u538b7z\u6587\u4ef6\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import py7zr<\/p>\n<p>def extract_7z(file_path, destination):<\/p>\n<p>    with py7zr.SevenZipFile(file_path, mode=&#39;r&#39;) as z:<\/p>\n<p>        z.extractall(path=destination)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>extract_7z(&#39;example.7z&#39;, &#39;output_directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u8be6\u89e3<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165<code>py7zr<\/code>\u5e93<\/strong>\uff1a\u5728\u4f7f\u7528\u524d\u9700\u8981\u5bfc\u5165<code>py7zr<\/code>\u5e93\u3002<\/li>\n<li><strong>\u521b\u5efa<code>SevenZipFile<\/code>\u5bf9\u8c61<\/strong>\uff1a\u4f7f\u7528<code>py7zr.SevenZipFile<\/code>\u7c7b\u6253\u5f007z\u6587\u4ef6\u3002<\/li>\n<li><strong>\u8c03\u7528<code>extractall<\/code>\u65b9\u6cd5<\/strong>\uff1a\u5c06\u6587\u4ef6\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>pylzma<\/code>\u5e93\u89e3\u538b7z\u538b\u7f29\u5305<\/h3>\n<\/p>\n<p><p><code>pylzma<\/code>\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5904\u74067z\u683c\u5f0f\u7684Python\u5e93\uff0c\u4f46\u5b83\u4e3b\u8981\u7528\u4e8e\u5904\u7406LZMA\u538b\u7f29\u7684\u6570\u636e\u6d41\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5<code>pylzma<\/code><\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>pylzma<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pylzma<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u538b7z\u6587\u4ef6\u7684\u57fa\u672c\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>pylzma<\/code>\u89e3\u538b7z\u6587\u4ef6\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pylzma<\/p>\n<p>import os<\/p>\n<p>def extract_7z(file_path, destination):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        data = file.read()<\/p>\n<p>        decompressed_data = pylzma.decompress(data)<\/p>\n<p>        # \u4fdd\u5b58\u89e3\u538b\u540e\u7684\u6587\u4ef6<\/p>\n<p>        output_path = os.path.join(destination, &#39;decompressed_file&#39;)<\/p>\n<p>        with open(output_path, &#39;wb&#39;) as output_file:<\/p>\n<p>            output_file.write(decompressed_data)<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>extract_7z(&#39;example.7z&#39;, &#39;output_directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u8be6\u89e3<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165<code>pylzma<\/code>\u5e93<\/strong>\uff1a\u5728\u4f7f\u7528\u524d\u9700\u8981\u5bfc\u5165<code>pylzma<\/code>\u5e93\u3002<\/li>\n<li><strong>\u8bfb\u53d67z\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u8bfb\u53d67z\u6587\u4ef6\u3002<\/li>\n<li><strong>\u89e3\u538b\u7f29\u6570\u636e<\/strong>\uff1a\u4f7f\u7528<code>pylzma.decompress<\/code>\u65b9\u6cd5\u89e3\u538b\u6570\u636e\u3002<\/li>\n<li><strong>\u4fdd\u5b58\u89e3\u538b\u540e\u7684\u6587\u4ef6<\/strong>\uff1a\u5c06\u89e3\u538b\u540e\u7684\u6570\u636e\u5199\u5165\u6307\u5b9a\u76ee\u5f55\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u884c\u5de5\u5177\u89e3\u538b7z\u538b\u7f29\u5305<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u7cfb\u7edf\u5df2\u7ecf\u5b89\u88c5\u4e867z\u547d\u4ee4\u884c\u5de5\u5177\uff08\u59827-Zip\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u7684<code>subprocess<\/code>\u6a21\u5757\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u884c\u5de5\u5177\u6765\u89e3\u538b7z\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c57z\u547d\u4ee4\u884c\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4ece<a href=\"https:\/\/www.7-zip.org\/\">7-Zip\u5b98\u7f51<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c57-Zip\u3002\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c57z\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install p7zip-full<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u8c03\u75287z\u547d\u4ee4\u884c\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u8c03\u75287z\u547d\u4ee4\u884c\u5de5\u5177\u89e3\u538b7z\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def extract_7z(file_path, destination):<\/p>\n<p>    subprocess.run([&#39;7z&#39;, &#39;x&#39;, file_path, f&#39;-o{destination}&#39;])<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>extract_7z(&#39;example.7z&#39;, &#39;output_directory&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ee3\u7801\u8be6\u89e3<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165<code>subprocess<\/code>\u6a21\u5757<\/strong>\uff1a\u5728\u4f7f\u7528\u524d\u9700\u8981\u5bfc\u5165<code>subprocess<\/code>\u6a21\u5757\u3002<\/li>\n<li><strong>\u8c03\u7528<code>subprocess.run<\/code>\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528<code>subprocess.run<\/code>\u65b9\u6cd5\u6267\u884c7z\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u89e3\u538b\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728Python\u4e2d\u89e3\u538b7z\u538b\u7f29\u5305\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong><code>py7zr<\/code>\u5e93<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u4e0d\u4f9d\u8d56\u5916\u90e8\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8de8\u5e73\u53f0\u5e94\u7528\u7684\u573a\u666f\u3002<\/li>\n<li><strong><code>pylzma<\/code>\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406LZMA\u538b\u7f29\u7684\u6570\u636e\u6d41\uff0c\u4f46\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u5904\u7406\u6587\u4ef6\u6d41\u3002<\/li>\n<li><strong>\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u884c\u5de5\u5177<\/strong>\uff1a\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u5b89\u88c5\u76847z\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u7cfb\u7edf\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\u4e867z\u5de5\u5177\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u5728\u591a\u5e73\u53f0\u4e0a\u8fd0\u884c\u4e14\u4e0d\u4f9d\u8d56\u5916\u90e8\u5de5\u5177\uff0c<code>py7zr<\/code>\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5982\u679c\u5df2\u7ecf\u6709\u7cfb\u7edf\u73af\u5883\u652f\u63017z\u5de5\u5177\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u884c\u5de5\u5177\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7ec4\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u89e3\u538b7z\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u89e3\u538b7z\u538b\u7f29\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528<code>py7zr<\/code>\u5e93\u6216<code>pylzma<\/code>\u5e93\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u4f7f\u7528\u547d\u4ee4<code>pip install py7zr<\/code>\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8f7b\u677e\u89e3\u538b7z\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">import py7zr\n\nwith py7zr.SevenZipFile(&#39;yourfile.7z&#39;, mode=&#39;r&#39;) as archive:\n    archive.extractall(path=&#39;output_directory&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>yourfile.7z<\/code>\u66ff\u6362\u4e3a\u4f60\u7684\u6587\u4ef6\u540d\uff0c\u5e76\u6307\u5b9a\u5408\u9002\u7684\u8f93\u51fa\u76ee\u5f55\u3002<\/p>\n<p><strong>\u5728\u89e3\u538b7z\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u89e3\u538b7z\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u76ee\u6807\u8def\u5f84\u6709\u8db3\u591f\u7684\u5b58\u50a8\u7a7a\u95f4\u3002\u6b64\u5916\uff0c\u67d0\u4e9b7z\u6587\u4ef6\u53ef\u80fd\u4f1a\u52a0\u5bc6\uff0c\u56e0\u6b64\u5728\u89e3\u538b\u524d\u9700\u8981\u63d0\u4f9b\u6b63\u786e\u7684\u5bc6\u7801\u3002\u786e\u4fdd\u4f7f\u7528\u7684\u5e93\u652f\u6301\u6240\u9700\u7684\u529f\u80fd\uff0c\u5982\u89e3\u538b\u7f29\u52a0\u5bc6\u6587\u4ef6\u3002<\/p>\n<p><strong>\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u7528\u4e8e\u5904\u74067z\u538b\u7f29\u5305\uff1f<\/strong><br \/>\u9664\u4e86<code>py7zr<\/code>\uff0c\u5176\u4ed6\u4e00\u4e9b\u53ef\u4ee5\u5904\u74067z\u6587\u4ef6\u7684\u5e93\u5305\u62ec<code>pylzma<\/code>\u548c<code>libarchive<\/code>\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u65f6\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u652f\u6301\u7684\u538b\u7f29\u683c\u5f0f\u3001\u64cd\u4f5c\u7b80\u5355\u6027\u4ee5\u53ca\u6587\u6863\u7684\u5b8c\u6574\u6027\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u89e3\u538b7z\u538b\u7f29\u5305\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4f7f\u7528py7zr\u5e93\u3001pylzma\u5e93\u3001\u4ee5\u53ca\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u884c\u5de5\u5177\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1135845,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135841"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1135841"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135841\/revisions"}],"predecessor-version":[{"id":1135848,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135841\/revisions\/1135848"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135845"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}