简体   繁体   English

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

[英]Opening password protected RAR files in Java

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). 我一直在搜索Google一段时间,但似乎找不到任何库可以让我使用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,但据我所知,它们都不支持密码保护的文件。

WinRAR is shipped with two utility programs (unrar.exe and rar.exe). WinRAR附带了两个实用程序(unrar.exe和rar.exe)。 From Powershell, you can unrar an archive by calling: unrar e .\\my-archive.rar -p[your-password] 在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. 我不知道如何为Windows JVM实现exec() I think there is a risk the password ends up in an unsafe place (log file?) where it does not belong. 我认为密码有可能在不安全的不安全地方(日志文件?)结束。

  • 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 Stackoverflow.com, you probably stumbled over the Junrar library. 注意:在Stackoverflow.com上搜索时,您可能偶然发现了Junrar库。 It cannot be used to extract encrypted archives (see line 122 of this file ). 它不能用于提取加密的归档文件 (请参阅此文件的第122行)。

SevenZip library could extract many types of archive files including RAR SevenZip库可以提取许多类型的存档文件,包括RAR

        randomAccessFile= new RandomAccessFile(sourceZipFile, "r");
    inArchive = SevenZip.openInArchive(null, // autodetect archive type
            new RandomAccessFileInStream(randomAccessFile));
    simpleInArchive = inArchive.getSimpleInterface();

    for (int i = 0; i < inArchive.getNumberOfItems(); i++) {
        ISimpleInArchiveItem archiveItem = simpleInArchive.getArchiveItem(i);

        final File outFile = new File(destFolder,archiveItem.getPath());
        outFile.getParentFile().mkdirs();
        logger.debug(String.format("extract(%s) in progress: %s",sourceZipFile.getName(),archiveItem.getPath()));
        final BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(outFile));
        ExtractOperationResult result = archiveItem.extractSlow(new ISequentialOutStream() {
            public int write(byte[] data) throws SevenZipException {
                try {
                    out.write(data);
                } catch (IOException e) {
                    throw new SevenZipException(String.format("error in writing extracted data from:%s to:%s ",sourceZipFile.getName(),outFile.getName()),e);
                }finally{
                    try{out.close();}catch(Exception e){}
                }
                return data.length; // return amount of consumed data
            }
        });
        if(result!=ExtractOperationResult.OK){
            throw new SevenZipException(String.format(" %s error occured in extracting : %s item of file : %s ",result.name(),archiveItem.getPath(),sourceZipFile.getName()));
        }
    }

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

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