java 7zip解压_java解压7-zip压缩文件

本文介绍了一个Java类UnZip7,用于解压7-zip格式的压缩文件。通过RandomAccessFile和SevenZip库,实现读取压缩文件并逐个提取文件到指定目录,同时显示文件的哈希值、大小和路径。如果解压过程中出现错误,程序会捕获并打印异常信息。
摘要由CSDN通过智能技术生成

public class UnZip7 {

public static void main(String args[]){

UnZip7 unzip=new UnZip7();

boolean flag=unzip.extractile("D://item.zip");

if(flag){

System.out.println("解压成功。");

}

}

boolean extractile(String filepath){

RandomAccessFile randomAccessFile = null;

ISevenZipInArchive inArchive = null;

boolean flag=true;

try {

randomAccessFile = new RandomAccessFile(filepath, "r");

inArchive = SevenZip.openInArchive(null,new RandomAccessFileInStream(randomAccessFile));

ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

System.out.println("  Hash    |     Size   | Filename");

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 {

//Write to file

FileOutputStream fos;

try {

File file = new File("D://7zip//"+item.getPath());

fos = new FileOutputStream(file);

fos.write(data);

fos.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

hash[0] ^= Arrays.hashCode(data);

sizeArray[0] += data.length;

return data.length;

}

});

if (result != ExtractOperationResult.OK) {

flag=false;

System.err.println("Error extracting item: " + result);

} else {

System.out.println(String.format("%9X |%10s | %s",

hash[0], sizeArray[0], item.getPath()));

}

}

}

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

}

}

}

return flag;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值