java解压全部rar压缩包

引入依赖


        
            com.github.axet
            java-unrar
            1.7.0-8
        
        
            net.sf.sevenzipjbinding
            sevenzipjbinding
            16.02-2.01
        
        
            net.sf.sevenzipjbinding
            sevenzipjbinding-all-platforms
            16.02-2.01
        

ExtractCallback 

需要首先船舰ExtractCallback工具类,再调用方法


/**
 * @Author JimmySong
 * @Date 2023/11/8 13:49
 * @Version 1.0
 */


import net.sf.sevenzipjbinding.*;

import java.io.*;

public class ExtractCallback implements IArchiveExtractCallback {

    private int index;
    private String packageName;
    private IInArchive inArchive;
    private String ourDir;

    public ExtractCallback(IInArchive inArchive, String packageName, String ourDir) {
        this.inArchive = inArchive;
        this.packageName = packageName;
        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);
        final String[] oldPath = {""};
        return new ISequentialOutStream() {
            public int write(byte[] data) throws SevenZipException {
                try {
                    if (!isFolder) {
//                        System.out.println(path);
                        File file = new File(ourDir+"\\" + path);
                        if (path.equals(oldPath[0])){
                            save2File(file, data,true);
                        }else{
                            save2File(file, data,false);
                        }
                        oldPath[0] = path;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return data.length;
            }
        };
    }

    @Override
    public void prepareOperation(ExtractAskMode extractAskMode) throws SevenZipException {

    }

    @Override
    public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {

    }


    public boolean save2File(File file, byte[] msg,boolean append) {
        OutputStream fos = null;
        try {
            File parent = file.getParentFile();
            boolean bool;
            if ((!parent.exists()) && (!parent.mkdirs())) {
                return false;
            }
//            fos = new FileOutputStream(file);
            fos = new FileOutputStream(file,append);//是否追加
            fos.write(msg);
            fos.flush();
            return true;
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            File parent;
            return false;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    }

}

RarUtils 工具类

import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @Author JimmySong
 * @Date 2023/11/8 14:38
 * @Version 1.0
 */
@Component
public class FindRar {


    public static void findRar01(String targetPath) throws IOException {
    File src = new File(targetPath);
    findDocx(src);
}
    public static void findDocx ( File src) throws IOException {

        // 1,进入文件夹
        File[] files = src.listFiles();
        //防止空指针方体!=null
        if (files!=null){
            //2,遍历数组
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith("rar")){
                    System.out.println(file);
                    String rarDir  = file.getAbsolutePath();
                    int  last = rarDir.indexOf(".");//找到文件后缀”.“,找到它的下标
                    String outDir = rarDir.substring(0,last);//从开始直到下标结束前一位,通过下标
                    unDoRar01(rarDir,outDir);
                    findRar01(outDir);//调用第一个类来循环解压
                }else {
                    findDocx(file);//递归传参一定是子文件参数
                }
            }
        }

    }

    public static void unDoRar01(String rarDir, String outDir)throws IOException {

        RandomAccessFile randomAccessFile = null;
        IInArchive inArchive = null;
// 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile
        try {
            randomAccessFile = new RandomAccessFile(rarDir, "r");
            inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));

            int[] in = new int[inArchive.getNumberOfItems()];
            for (int i = 0; i < in.length; i++) {
                in[i] = i;
            }
            inArchive.extract(in, false, new ExtractCallback(inArchive, "366", outDir));
        } catch (FileNotFoundException | SevenZipException e) {
            e.printStackTrace();
        } finally {
            if (inArchive != null) {
                try {
                    inArchive.close();
                } catch (SevenZipException e) {
                    System.err.println("Error closing archive: " + e);
                }
            }
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    System.err.println("Error closing file: " + e);
                }
            }
        }
    }
}

参考项目elezip111

你可能感兴趣的:(java,python,开发语言)