• Java解压RAR5


    Java 解压RAR5

    RAR5加密算法并未公布,所以很多开源工具包都只支持rar4,在解压rar5格式时,会报出不支持rar5格式的错误,比如常用的junara

    经过仔细的翻阅Google,找到了解决方案

    1.添加pom文件依赖
     <dependency>
                <groupId>com.github.axet</groupId>
                <artifactId>java-unrar</artifactId>
                <version>1.7.0-8</version>
            </dependency>
            <dependency>
                <groupId>net.sf.sevenzipjbinding</groupId>
                <artifactId>sevenzipjbinding</artifactId>
                <version>16.02-2.01</version>
            </dependency>
            <dependency>
                <groupId>net.sf.sevenzipjbinding</groupId>
                <artifactId>sevenzipjbinding-all-platforms</artifactId>
                <version>16.02-2.01</version>
            </dependency>
    
    2.例子
    package rar5;
     
    import java.io.IOException;
    import java.io.RandomAccessFile;
     
    import net.sf.sevenzipjbinding.IInArchive;
    import net.sf.sevenzipjbinding.SevenZip;
    import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
     
    public class RAR5Test {
    	public static void main(String[] args) throws IOException {
    	    String rarDir = "D:\rar5.rar";
    	    String outDir = "D:\rar5";
    	    IInArchive archive;
                RandomAccessFile randomAccessFile;
                // 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile
                //r代表以只读的方式打开文本,也就意味着不能用write来操作文件
                randomAccessFile = new RandomAccessFile(rarDir, "r");
                archive = SevenZip.openInArchive(null, // null - autodetect
                        new RandomAccessFileInStream(randomAccessFile));
                int[] in = new int[archive.getNumberOfItems()];
                for (int i = 0; i < in.length; i++) {
                    in[i] = i;
                }
                archive.extract(in, false, new ExtractCallback(archive,unRarDir.getAbsolutePath() + "/"));
                archive.close();
                randomAccessFile.close();
     
    	}
    }
    
    3.编写回调ExtractCallback
    public class ExtractCallback implements IArchiveExtractCallback {
    
        private int index;
        private IInArchive inArchive;
        private String ourDir;
    
        public ExtractCallback(IInArchive inArchive, String ourDir) {
            this.inArchive = inArchive;
            this.ourDir = ourDir;
        }
    
        @Override
        public void setCompleted(long arg0) throws SevenZipException {
        }
    
        @Override
        public void setTotal(long arg0) throws SevenZipException {
        }
    
        @Override
        public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
            this.index = index;
            final String path = (String) inArchive.getProperty(index, PropID.PATH);
            final boolean isFolder = (boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
            return data -> {
                try {
                    if (!isFolder) {
                        File file = new File(ourDir + path);
                        save2File(file, data);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return data.length;
            };
        }
    
        @Override
        public void prepareOperation(ExtractAskMode arg0) throws SevenZipException {
        }
    
        @Override
        public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
    
        }
    
        public static boolean save2File(File file, byte[] msg) {
            OutputStream fos = null;
            try {
                File parent = file.getParentFile();
                if ((!parent.exists()) && (!parent.mkdirs())) {
                    return false;
                }
                fos = new FileOutputStream(file, true);
                fos.write(msg);
                fos.flush();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
  • 相关阅读:
    [leetcode]算法题目
    JQuery功能查询页
    [C语言]一个很实用的服务端和客户端进行TCP通信的实例
    Siege——多线程编程最佳实例
    CodeIgniter框架中关于URL(index.php)的那些事
    web压测工具http_load原理分析
    【JAVA】文件各行打乱
    【JAVA】HashMap的原理及多线程下死循环的原因
    【JAVA】高并发优化细节点
    【Linux】日志分析工具grep sed sort
  • 原文地址:https://www.cnblogs.com/zhufanfan/p/15016717.html
Copyright © 2020-2023  润新知