苹果手机解压rar文件

发布时间: 2023-02-24 01:34 阅读: 文章来源:转载

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

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

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

上代码:

net.sf.sevenzipjbindingsevenzipjbinding16.02-2.01net.sf.sevenzipjbindingsevenzipjbinding-all-platforms16.02-2.01
//随机 通行 文件,较多用于大文件处理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 index.	 * 详细请看接口说明	 */	@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 *

•••展开全文
相关文章