pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>spring</artifactId> <version>1.0-SNAPSHOT</version> <properties> <sevenzipjbinding.version>9.20-2.00beta</sevenzipjbinding.version> <commons.io.version>2.0.1</commons.io.version> </properties> <dependencies> <dependency> <groupId>net.sf.sevenzipjbinding</groupId> <artifactId>sevenzipjbinding</artifactId> <version>${sevenzipjbinding.version}</version> </dependency> <dependency> <groupId>net.sf.sevenzipjbinding</groupId> <artifactId>sevenzipjbinding-all-platforms</artifactId> <version>${sevenzipjbinding.version}</version> </dependency> <dependency> <artifactId>commons-io</artifactId> <groupId>commons-io</groupId> <version>${commons.io.version}</version> </dependency> </dependencies> </project>
package urar; import net.sf.sevenzipjbinding.*; import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; import net.sf.sevenzipjbinding.simple.ISimpleInArchive; import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem; import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileOutputStream; import java.io.RandomAccessFile; import java.util.Arrays; public class URaR { /** * @param file7zPath(7z文件路径) * @param outPutPath(解压路径) * @param passWord(文件密码.没有可随便写,或空) * @return * @throws Exception * @Description (解压7z) */ public static int un7z(String file7zPath, final String outPutPath, String passWord) throws Exception { IInArchive archive; RandomAccessFile randomAccessFile; randomAccessFile = new RandomAccessFile(file7zPath, "r"); archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile), passWord); int numberOfItems = archive.getNumberOfItems(); ISimpleInArchive simpleInArchive = archive.getSimpleInterface(); for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { final int[] hash = new int[]{0}; if (!item.isFolder()) { ExtractOperationResult result; final long[] sizeArray = new long[1]; result = item.extractSlow(new ISequentialOutStream() { public int write(byte[] data) throws SevenZipException { try { //判断压缩包内的文件是否存在 String parentFilePath = outPutPath + File.separator + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator)); if (!new File(parentFilePath).exists()) { new File(parentFilePath).mkdirs(); } IOUtils.write(data, new FileOutputStream(new File(outPutPath + File.separator + item.getPath()), true)); } catch (Exception e) { e.printStackTrace(); } hash[0] ^= Arrays.hashCode(data); // Consume data sizeArray[0] += data.length; return data.length; // Return amount of consumed } }, passWord); if (result == ExtractOperationResult.OK) { System.out.println(String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath())); } else { System.out.printf("Error extracting item: " + result); } } } archive.close(); randomAccessFile.close(); return numberOfItems; } public static void main(String[] args) throws Exception { un7z("C:\Users\spring\Desktop\A_(3169).rar", "d:\111", "SPzfq"); } }
解压
http://sevenzipjbind.sourceforge.net/compression_snippets.html