java 解压7z_java 代码解压7z(带密码)转载请注明出处,谢谢

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");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值