packageurar;import net.sf.sevenzipjbinding.*;importnet.sf.sevenzipjbinding.impl.RandomAccessFileInStream;importnet.sf.sevenzipjbinding.simple.ISimpleInArchive;importnet.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;importorg.apache.commons.io.IOUtils;importjava.io.File;importjava.io.FileOutputStream;importjava.io.RandomAccessFile;importjava.util.Arrays;public classURaR {/***@paramfile7zPath(7z文件路径)
*@paramoutPutPath(解压路径)
*@parampassWord(文件密码.没有可随便写,或空)
*@return*@throwsException
* @Description (解压7z)*/
public static int un7z(String file7zPath, final String outPutPath, String passWord) throwsException {
IInArchive archive;
RandomAccessFile randomAccessFile;
randomAccessFile= new RandomAccessFile(file7zPath, "r");
archive= SevenZip.openInArchive(null, newRandomAccessFileInStream(randomAccessFile), passWord);int numberOfItems =archive.getNumberOfItems();
ISimpleInArchive simpleInArchive=archive.getSimpleInterface();for (finalISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {final int[] hash = new int[]{0};if (!item.isFolder()) {
ExtractOperationResult result;final long[] sizeArray = new long[1];
result= item.extractSlow(newISequentialOutStream() {public int write(byte[] data) throwsSevenZipException {try{//判断压缩包内的文件是否存在
String parentFilePath = outPutPath + File.separator + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));if (!newFile(parentFilePath).exists()) {newFile(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();returnnumberOfItems;
}public static void main(String[] args) throwsException {
un7z("C:\\Users\\spring\\Desktop\\A_(3169).rar", "d:\\111", "SPzfq");
}
}