简体   繁体   English

如何解压文件

[英]How to unrar files

I am trying to unrar files uploaded by users in django web application.我正在尝试解压缩用户在 django web 应用程序中上传的文件。 I have gone through different approaches but none of them are working for me.我经历了不同的方法,但没有一个对我有用。 The below methods work fine in my local OS (Ubuntu 18.04), but n't working in the server.以下方法在我的本地操作系统(Ubuntu 18.04)中运行良好,但在服务器中不起作用。 I am using Python 3.6.我正在使用 Python 3.6。

  1. import rarfile - here 'rarfile' is imported from python3.6/site-packages/rarfile. import rarfile - 这里的“rarfile”是从 python3.6/site-packages/rarfile 导入的。 It is giving this error -它给出了这个错误 -

    bsdtar: Error opening archive: Failed to open '--'

    Following the steps suggested here , I installed 'unrar'.按照此处建议的步骤,我安装了“unrar”。

  2. from unrar import rarfile - here it's being imported from python3.6/site-packages/unrar/ from unrar import rarfile - 这里是从 python3.6/site-packages/unrar/ 导入的

    But this gave the error - LookupError: Couldn't find path to unrar library.但这给出了错误 - LookupError: Couldn't find path to unrar library.

    So to solve the issue, I followed the steps mentioned here , and created UnRaR executable, and also set the path in my environment.所以为了解决这个问题,我按照这里提到的步骤,创建了 UnRaR 可执行文件,并在我的环境中设置了路径。 This works fine in local, but unable to understand how to do the same in 'production environment'.这在本地工作正常,但无法理解如何在“生产环境”中做同样的事情。

  3. import patoolib - importing from patool import patoolib - 从 patool 导入

    I also tried implementing patool, which worked fine in local but not in production and I am getting this error - patoolib.util.PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),我还尝试实现 patool,它在本地运行良好,但在生产环境中运行良好,我收到此错误 - patoolib.util.PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z), patoolib.util.PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),

This has become a big blocker in my application, can anyone please explain how to solve the problem.这已成为我的应用程序中的一个大障碍,任何人都可以解释如何解决这个问题。

Code snippet for rarfile - rarfile 的代码片段 -

import rarfile
.....        
rf = rarfile.RarFile(file_path)
rf.extractall(extract_to_path)

Code snippet for patool - patool 的代码片段 -

import patoolib
...
patoolib.extract_archive(file_path, outdir=extract_to_path)

Patool is the good python library it is simple and easy. Patool 是很好的 python 库,它简单易用。 A simple example of it is here:一个简单的例子在这里:

pip install patool

Use pip install patool to install the library使用pip install patool安装库

import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")

Now, in your case the problem is probably that you don't have your unraring tool in the path, indicating it can not be called from the command line (which is exactly what patool does).现在,在您的情况下,问题可能是您的路径中没有您的 unraring 工具,这表明它不能从命令行调用(这正是 patool 所做的)。 Just put your rar file folder to the path and you're fine or give the absolute path to the rar file.只需将您的 rar 文件夹放到路径中,就可以了,或者提供 rar 文件的绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM