您的位置 首页 java

JAVA解压RAR5文件

项目开发中,遇到许多文件操作,特别是压缩或解压。大多使用开源的 ZIP 7Z 方案,已有大量第三方库可用,在这不多重复。

如果项目中,需要解压rar文件,相信你会在心里骂千万遍SB,然后熟练的cmd调用winrar…

本文另辟蹊径,使用开源的sevenzipjbinding进行rar5文件解压。(注意:是现流行的rar5,非rar4)

上代码:

 <dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding</artifactId>
    <version>16.02-2.01</version>
</dependency>
<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding-all-platforms</artifactId>
    <version>16.02-2.01</version>
</dependency>  
 //随机 通行 文件,较多用于大文件处理
 RandomAccessFile  randomAccessFile = null;
IInArchive inArchive = null;
try {
    //只读打开文件
    randomAccessFile = new RandomAccessFile(file, "r");
	inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
	final int[] in = new int[inArchive.getNumberOfItems()];
	for (int i = 0; i < in.length; i++) {
		in[i] = i;
	}
   //savePath输出路径
	inArchive.extract(in, false, new MyArchiveExtractCallback(inArchive, savePath));
 } catch (Exception e) {
     //记录或抛出错误
 } finally {
	//别忘了closeable
}  

MyArchiveExtractCallback:

 public class MyArchiveExtractCallback implements IArchiveExtractCallback {
	private int index;
	private IInArchive inArchive;
	 private  String ourDir;

	public MyArchiveExtractCallback(final IInArchive newinArchive, final String newourDir) {
		this.inArchive = newinArchive;
		this.ourDir = newourDir;
	}

	/**
	 * Return sequential output stream for the file with index <code>index</code>.
	 * 详细请看接口说明
	 */
	@ Override 
	public ISequentialOutStream getStream(final int newindex, final ExtractAskMode newextractAskMode)
			throws SevenZipException {
		this.index = newindex;
		final String path = (String) inArchive.getProperty(index, PropID.PATH);
		final boolean isFolder = (Boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
		return new ISequentialOutStream() {
			public int write(final byte[] data) throws SevenZipException {
				try {
					if (!isFolder) {
						final File file = new File(ourDir + File.separator + path);
						FileUtils.writeByteArrayToFile(file, data);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return data.length;
			}
		};
	}
 
 	@Override
	public void setCompleted(final long arg0) throws SevenZipException {
	}

	@Override
	public void setTotal(final long arg0) throws SevenZipException {
	}

	@Override
	public void prepareOperation(final ExtractAskMode arg0) throws SevenZipException {
	}

	@Override
	public void setOperationResult(final ExtractOperationResult extractOperationResult) throws SevenZipException {
	}
}  

但是很可惜,目前还不支持RAR5的压缩。官网功能介绍如下:

Extract

7-Zip Zip Rar Tar Split Lzma Iso HFS GZip

Cpio BZip2 Z Arj Chm Lhz Cab Nsis

Ar/A/Lib/Deb Rpm Wim Udf Fat Ntfs

Create/update

7-Zip Zip GZip Tar BZip2

It’s cross-platform. Binaries are available for

› MS-Windows 32/64 › Darwin Mac OS X

› Linux:

Intel 32/64

ARMv5 (armel) *

ARMv6 (RaspberryPi 2) *

ARMv7 (armhf) *

ARM64 *

文章来源:智云一二三科技

文章标题:JAVA解压RAR5文件

文章地址:https://www.zhihuclub.com/195390.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

网站地图