zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }
    
    
  • 相关阅读:
    WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别
    改善C#程序的建议1:非用ICloneable不可的理由
    WPF快速指导5:验证
    改善C#程序的建议4:C#中标准Dispose模式的实现
    我所入选的微软技术社区电子报
    C#中new, override, virtual的具体用法
    C#中FCL迭代器模式的一点问题
    WPF快速指导3:数据绑定
    WPF快速指导2:模板
    C#高效编程话题集2(每期10话题)
  • 原文地址:https://www.cnblogs.com/zhufanfan/p/15016717.html
Copyright © 2011-2022 走看看