代码源地址:http://sourceforge.net/p/sevenzipjbind/discussion/757964/thread/b64a36fb
支持压缩格式:- 7z, Zip, Tar, Rar, Lzma, Iso, GZip, Cpio, BZIP2, Z, Arj, Lzh, Cab, Chm, Nsis, DEB, RPM, UDF, WIM
代码如下:
package com.neusoft;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.logging.Logger;
import net.sf.sevenzipjbinding.ExtractAskMode;
import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IArchiveExtractCallback;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.ISevenZipInArchive;
import net.sf.sevenzipjbinding.PropID;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
/**
* <p>Description: [压缩文件工具包]</p>
* @author <a href="mailto: li.bb@xxx.com">李彬彬</a>
* @version $Revision$
*/
public class ZipTools
{
private static Logger logger = Logger.getLogger(ZipTools.class.getCanonicalName());
/**
* <p>Discription:[将解压文件解压到指定目录下]</p>
* @param file 文件全路径
* @param extractPath 解压后文件存往路径 -支持压缩格式: 7Z,ZIP,TAR,RAR,LZMA,ISO,GZIP,BZIP2,CPIO,Z,ARJ,LZH,CAB,CHM,NSIS,DEB,RPM,UDF,WIM
* @throws SevenZipException
* @throws IOException
* @author:[李彬彬]
* @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
*/
public void extract(String file, String extractPath) throws SevenZipException, IOException
{
ISevenZipInArchive inArchive = null;
RandomAccessFile randomAccessFile = null;
try
{
randomAccessFile = new RandomAccessFile(new File(file), "r");
inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
inArchive.extract(null, false, new ExtractCallback(inArchive, extractPath));
}
finally
{
if (inArchive != null)
{
inArchive.close();
}
if (randomAccessFile != null)
{
randomAccessFile.close();
}
}
}
/**
* <p>Description: [提后文件后回调方法]</p>
* @author <a href="mailto: li.bb@neusoft.com">李彬彬</a>
* @version $Revision$
*/
private class ExtractCallback implements IArchiveExtractCallback
{
private final ISevenZipInArchive inArchive;
private final String extractPath;
public ExtractCallback(ISevenZipInArchive inArchive, String extractPath)
{
this.inArchive = inArchive;
if (!extractPath.endsWith("/") && !extractPath.endsWith("\\"))
{
extractPath += File.separator;
}
this.extractPath = extractPath;
}
@Override
public ISequentialOutStream getStream(final int index, ExtractAskMode extractAskMode) throws SevenZipException
{
return new ISequentialOutStream()
{
@Override
public int write(byte[] data) throws SevenZipException
{
String filePath = inArchive.getStringProperty(index, PropID.PATH);
FileOutputStream fos = null;
try
{
File path = new File(extractPath + filePath);
if (!path.getParentFile().exists())
{
path.getParentFile().mkdirs();
}
if (!path.exists())
{
path.createNewFile();
}
fos = new FileOutputStream(path, true);
fos.write(data);
}
catch (IOException e)
{
logger.log(null, "IOException while extracting " + filePath);
}
finally
{
try
{
if (fos != null)
{
fos.flush();
fos.close();
}
}
catch (IOException e)
{
logger.log(null, "Could not close FileOutputStream", e);
}
}
return data.length;
}
};
}
@Override
public void setCompleted(long arg0) throws SevenZipException
{
}
@Override
public void setTotal(long arg0) throws SevenZipException
{
}
@Override
public void prepareOperation(ExtractAskMode arg0) throws SevenZipException
{
}
@Override
public void setOperationResult(ExtractOperationResult arg0) throws SevenZipException
{
}
}
}