Introduce dependencies
<!-- rar--> <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>
ExtractCallback
You need to first ship the ExtractCallback tool class and then call the method
/** * @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()) & amp; & amp; (!parent.mkdirs())) { return false; } // fos = new FileOutputStream(file); fos = new FileOutputStream(file,append);//Whether to 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 tool class
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. Enter the folder File[] files = src.listFiles(); //Prevent null pointer cube! =null if (files!=null){ //2, traverse the array for (File file : files) { if (file.isFile() & amp; & amp; file.getName().endsWith("rar")){ System.out.println(file); String rarDir = file.getAbsolutePath(); int last = rarDir.indexOf(".");//Find the file suffix "." and find its subscript String outDir = rarDir.substring(0,last);//From the beginning until the last digit before the end of the subscript, through the subscript unDoRar01(rarDir,outDir); findRar01(outDir);//Call the first class to decompress in a loop }else { findDocx(file);//The recursive parameter must be a sub-file parameter } } } } public static void unDoRar01(String rarDir, String outDir)throws IOException { RandomAccessFile randomAccessFile = null; IInArchive inArchive = null; // The first parameter is the path of the compressed package that needs to be decompressed. The second parameter refers to RandomAccessFile in the JdkAPI document. 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); } } } } }
Reference project elezip111
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139319 people are learning the system