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


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 type
					new 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 file
							FileOutputStream 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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值