java 在windows环境 android环境 linux环境对7z包的解压缩

来源:互联网 发布:轻便婴儿车推荐 知乎 编辑:程序博客网 时间:2024/06/06 00:36


1.7z的官网 https://sourceforge.net/projects/sevenzipjbind/ 提供有关java的SDK

2.WINDOWS环境添加jar包为:

sevenzipjbinding-AllWindows.jar 

sevenzipjbinding.jar

3.以带密码解压7z包为例,代码如下

package main;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.RandomAccessFile;import java.util.Arrays;import net.sf.sevenzipjbinding.ArchiveFormat;import net.sf.sevenzipjbinding.ExtractOperationResult;import net.sf.sevenzipjbinding.IInArchive;import net.sf.sevenzipjbinding.ISequentialOutStream;import net.sf.sevenzipjbinding.SevenZip;import net.sf.sevenzipjbinding.SevenZipException;import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;import net.sf.sevenzipjbinding.simple.ISimpleInArchive;import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;public class UnZip {/** * @param filepath * @param distpath */void extractile(String filepath,final String distpath) {RandomAccessFile randomAccessFile = null;IInArchive inArchive = null;try {randomAccessFile = new RandomAccessFile(filepath, "rw");inArchive = SevenZip.openInArchive(ArchiveFormat.SEVEN_ZIP, // autodetect archive typenew RandomAccessFileInStream(randomAccessFile));ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {if (!item.isFolder()) {ExtractOperationResult result;final long[] sizeArray = new long[1];result = item.extractSlow(new ISequentialOutStream() {public int write(byte[] data) throws SevenZipException {// Write to fileFileOutputStream fos;try {System.out.println(distpath+"=="+item.getPath());File file = new File(distpath+item.getPath());File parentFile = file.getParentFile();if(!parentFile.exists()){parentFile.mkdirs();}fos = new FileOutputStream(file);fos.write(data);fos.close();} catch (Exception e) {e.printStackTrace();}sizeArray[0] += data.length;return data.length;}},"MbEdTXM4");if (result == ExtractOperationResult.OK) {System.out.println(item.getPath());} else {System.err.println("Error extracting item: " + result);}}}} catch (Exception e) {System.err.println("Error occurs: " + e);e.printStackTrace();System.exit(1);} 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);}}}}public static void main(String[] args) {UnZip unzip = new UnZip();//unzip.extractile("H:/1884fa85aedc4eb4a84ff03f99adbb9c/index.html.7z","H:/");long startTime = System.currentTimeMillis();unzip.extractile("H:/1884fa85aedc4eb4a84ff03f99adbb9c/rt5ctrNews.swf.7z","H:/1884fa85aedc4eb4a84ff03f99adbb9c/");/*unzip.extractile("/root/1884fa85aedc4eb4a84ff03f99adbb9c/rt5ctrNews.swf.7z","/root/1884fa85aedc4eb4a84ff03f99adbb9c/");*/long endTime = System.currentTimeMillis();System.out.println((endTime - startTime)/1000);}}


linux没有环境测试,android从官网下载包进行测试,发现不可以了,官方留言说暂时不支持andorid环境!!!



0 0
原创粉丝点击