用Java打开受密码保护的RAR文件

时间:2021-07-05 06:25:42

I've been searching Google for some time now but can't seem to find any library that allows me to open password protected RAR files using Java (compressed files).

我一直在谷歌搜索一段时间,但似乎找不到任何允许我使用Java(压缩文件)打开受密码保护的RAR文件的库。

If anyone knows of one please share it with me (if possible one including a maven dependency).

如果有人知道,请与我分享(如果可能的话,包括maven依赖)。

I've been looking at JUnRar and java-UnRar, but both do not support password protected files for as far as I could discover.

我一直在看JUnRar和java-UnRar,但据我所知,两者都不支持受密码保护的文件。

1 个解决方案

#1


2  

WinRAR is shipped with two utility programs (unrar.exe and rar.exe). From Powershell, you can unrar an archive by calling: unrar e .\my-archive.rar -p[your-password]

WinRAR附带两个实用程序(unrar.exe和rar.exe)。从Powershell,您可以通过以下方式取消存档:unrar e。\ my-archive.rar -p [your-password]

Now, you could place this call using the exec() method of Java's Runtime class:

现在,您可以使用Java的Runtime类的exec()方法进行此调用:

public class UnArchiver {

    public static void main(String[] args) {        
        try {   
            String command = "unrar.exe e .\my-archive.rar -pQWERT";
            Runtime.getRuntime().exec(command); 
        } catch (Exception e) {
            e.printStackTrace();
        }   
    }
}
// Code not tested

However, this option has some drawbacks:

但是,此选项有一些缺点:

  • Password is handled as string (bad practice when handling password)

    密码作为字符串处理(处理密码时的不良做法)

  • I do not know how exec() is implemented for Windows JVMs. I think there is a risk the password ends up in an unsafe place (log file?) where it does not belong.

    我不知道如何为Windows JVM实现exec()。我认为密码最终存在于不属于的不安全的地方(日志文件?)中存在风险。

  • For me, exec() always has a smell to it (because it introduces coupling to the environment - in this case unrar.exe that is not visible on first glance for later maintainers of your code)

    对我来说,exec()总是有一股气味(因为它引入了与环境的耦合 - 在这种情况下,unrar.exe乍一看是不可见的代码的后续维护者)

  • You introduce a platform dependency (in this case to Windows) as unrar.exe can run only on Windows (thanks @SapuSeven)

    您引入了平台依赖关系(在本例中为Windows),因为unrar.exe只能在Windows上运行(感谢@SapuSeven)

Note: When searching on *.com, you probably stumbled over the Junrar library. It cannot be used to extract encrypted archives (see line 122 of this file).

注意:在*.com上搜索时,您可能偶然发现了Junrar库。它不能用于提取加密档案(请参阅此文件的第122行)。

#1


2  

WinRAR is shipped with two utility programs (unrar.exe and rar.exe). From Powershell, you can unrar an archive by calling: unrar e .\my-archive.rar -p[your-password]

WinRAR附带两个实用程序(unrar.exe和rar.exe)。从Powershell,您可以通过以下方式取消存档:unrar e。\ my-archive.rar -p [your-password]

Now, you could place this call using the exec() method of Java's Runtime class:

现在,您可以使用Java的Runtime类的exec()方法进行此调用:

public class UnArchiver {

    public static void main(String[] args) {        
        try {   
            String command = "unrar.exe e .\my-archive.rar -pQWERT";
            Runtime.getRuntime().exec(command); 
        } catch (Exception e) {
            e.printStackTrace();
        }   
    }
}
// Code not tested

However, this option has some drawbacks:

但是,此选项有一些缺点:

  • Password is handled as string (bad practice when handling password)

    密码作为字符串处理(处理密码时的不良做法)

  • I do not know how exec() is implemented for Windows JVMs. I think there is a risk the password ends up in an unsafe place (log file?) where it does not belong.

    我不知道如何为Windows JVM实现exec()。我认为密码最终存在于不属于的不安全的地方(日志文件?)中存在风险。

  • For me, exec() always has a smell to it (because it introduces coupling to the environment - in this case unrar.exe that is not visible on first glance for later maintainers of your code)

    对我来说,exec()总是有一股气味(因为它引入了与环境的耦合 - 在这种情况下,unrar.exe乍一看是不可见的代码的后续维护者)

  • You introduce a platform dependency (in this case to Windows) as unrar.exe can run only on Windows (thanks @SapuSeven)

    您引入了平台依赖关系(在本例中为Windows),因为unrar.exe只能在Windows上运行(感谢@SapuSeven)

Note: When searching on *.com, you probably stumbled over the Junrar library. It cannot be used to extract encrypted archives (see line 122 of this file).

注意:在*.com上搜索时,您可能偶然发现了Junrar库。它不能用于提取加密档案(请参阅此文件的第122行)。