2
3 final File file = newFile(targetFilePath);4 if (!file.exists()) {5 file.mkdirs();6 }7 RandomAccessFile randomAccessFile = null;8 IInArchive inArchive = null;9
10 try{11 randomAccessFile = new RandomAccessFile(filepath, "r");12 inArchive = SevenZip.openInArchive(null,13 newRandomAccessFileInStream(randomAccessFile));14
15 ISimpleInArchive simpleInArchive =inArchive.getSimpleInterface();16
17 for (finalISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {18 final int[] hash = new int[]{0};19 if (!item.isFolder()) {20 ExtractOperationResult result;21
22 final long[] sizeArray = new long[1];23 result = item.extractSlow(newISequentialOutStream() {24 public int write(byte[] data) throwsSevenZipException {25
26 FileOutputStream fos = null;27 try{28 String fileName =item.getPath();29 String[] split = rename.split("\r\n");30 for (int i = 0; i < split.length; i++) {31 fileName = fileName.replace(split[i], "");32 }33
34 File tarFile = new File(file + File.separator +fileName);35
36 if (!tarFile.getParentFile().exists()) {37 tarFile.getParentFile().mkdirs();38 }39 tarFile.createNewFile();40 fos = newFileOutputStream(tarFile.getAbsolutePath());41 fos.write(data);42 fos.close();43
44 } catch(FileNotFoundException e) {45
46 } catch(IOException e) {47
48 }49
50 hash[0] ^=Arrays.hashCode(data);51 sizeArray[0] +=data.length;52 returndata.length;53 }54 });55 if (result ==ExtractOperationResult.OK) {56 //System.out.println(String.format("%9X | %10s | %s",//
57 //hash[0], sizeArray[0], item.getPath()));
58 } else{59 //System.err.println("Error extracting item: " + result);
60 }61 }62 }63 } catch(Exception e) {64 e.printStackTrace();65 System.exit(1);66 } finally{67 if (inArchive != null) {68 try{69 inArchive.close();70 } catch(SevenZipException e) {71 e.printStackTrace();72 }73 }74 if (randomAccessFile != null) {75 try{76 randomAccessFile.close();77 } catch(IOException e) {78 e.printStackTrace();79 }80 }81 }82 }
commons-logging
commons-logging
1.1.1
com.github.junrar
junrar
3.0.0
public static boolean unrar(String rarFileName, String outFilePath, String rename) throwsException {try{
Archive archive= new Archive(new File(rarFileName), newUnrarProcessMonitor(rarFileName));if (archive == null) {throw new FileNotFoundException(rarFileName + " NOT FOUND!");
}if(archive.isEncrypted()) {throw new Exception(rarFileName + " IS ENCRYPTED!");
}
List files =archive.getFileHeaders();for(FileHeader fh : files) {if(fh.isEncrypted()) {throw new Exception(rarFileName + " IS ENCRYPTED!");
}
String fileName= fh.getFileNameW().isEmpty() ?fh.getFileNameString() : fh.getFileNameW();
String[] split= rename.split("\n");for (int i = 0; i < split.length; i++) {
fileName= fileName.replace(split[i], ""); //解压重命名
}if (fileName != null && fileName.trim().length() > 0) {
String saveFileName= outFilePath + File.separator +fileName;
File saveFile= newFile(saveFileName);
File parent=saveFile.getParentFile();if (!parent.exists()) {
parent.mkdirs();
}if (!saveFile.exists()) {
saveFile.createNewFile();
}
FileOutputStream fos= newFileOutputStream(saveFile);try{
archive.extractFile(fh, fos);
fos.flush();
fos.close();
}catch(RarException e) {
}finally{
}
}
}return true;
}catch(Exception e) {
System.out.println("failed.");return false;
}
}
//对解压rar文件进度的监控
public class UnrarProcessMonitor implements UnrarCallback {
private String fileName;
public UnrarProcessMonitor(String fileName) {
this.fileName = fileName;
}
/**
* 返回false的话,对于某些分包的rar是没办法解压正确的
* */
@Override
public boolean isNextVolumeReady(Volume volume) {
try {
fileName = ((FileVolume) volume).getFile().getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public void volumeProgressChanged(long l, long l1) {
//输出进度
System.out.println("Unrar "+fileName+" rate: "+(double)l/l1*100+"%");
}
}
最后就是如果三种方法都无法解压我们就应该调用cmd来用WinRar进行解压
public static boolean unfile(String zipFile,String outFilePath,intmode){boolean flag=false;try{
File file= newFile(zipFile);
String fileName=file.getName();if(mode == 1)
{
outFilePath+= File.separator; //文件当前路径下
}else{
outFilePath+= File.separator+fileName.substring(0,fileName.length()-4)+File.separator;
}
File tmpFileDir= newFile(outFilePath);
tmpFileDir.mkdirs();
String unrarCmd= "C:\\Program Files\\WinRAR\\WinRar e ";
unrarCmd+= zipFile + " " +outFilePath;try{
Runtime rt=Runtime.getRuntime();
Process p=rt.exec(unrarCmd);
InputStream inputStream=p.getInputStream();
BufferedReader br= new BufferedReader(newInputStreamReader(inputStream));while (br.readLine()!=null){
}
p.waitFor();
br.close();
inputStream.close();
p.getErrorStream().close();
p.getOutputStream().close();
flag=true;
}catch(Exception e) {
System.out.println(e.getMessage());
}
}catch(Exception e){
}returnflag;
}
以上就是解压的方法,总体坐下来感觉还是调用cmd最简单直接,然后压缩的话基本上大部分都可以压缩,就不写上压缩的代码了