[java]使用junrar解压文件时,上报解压进度的具体实现

笔者在公司项目中有一个需要解压10G的rar的压缩包的一个需求,那么我们希望能够上报整个解压的进度。google/baidu上均没有找到合适的办法。后来看了下junrar的源码,发现实现UnrarCallback接口后,可以完成进度的上报监测。

经验之谈就是,读源码才是王道。

具体代码可点击Github地址

监控类具体代码如下。

package com.zju.javastudy.unrar;

import com.github.junrar.UnrarCallback;
import com.github.junrar.Volume;
import com.github.junrar.impl.FileVolume;

import java.io.IOException;

/**
 * @author Arthur
 * @Date 11/04/2018
 * @Decription:
 */
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+"%");
    }
}

解压代码如下:

package com.zju.javastudy.unrar;

import com.github.junrar.Archive;
import com.github.junrar.exception.RarException;
import com.github.junrar.exception.RarException.RarExceptionType;
import com.github.junrar.rarfile.FileHeader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author Arthur
 * @Date 11/04/2018
 * @Decription:
 */


public class UnRarDemo {

    /**
     * @param rarFileName rar file name
     * @param outFilePath output file path
     * @return success Or Failed
     * @author zhuss
     * @throws Exception
     */
    public static boolean  unrar(String rarFileName, String outFilePath)  throws  Exception{

        try  {
            Archive archive = new  Archive(new File(rarFileName), new UnrarProcessMonitor(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();
                if(fileName != null && fileName.trim().length() > 0){
                    String saveFileName = outFilePath+File.separator+fileName;
                    File saveFile = new File(saveFileName);
                    File parent =  saveFile.getParentFile();
                    if(!parent.exists()){
                        parent.mkdirs();
                    }
                    if(!saveFile.exists()){
                        saveFile.createNewFile();
                    }
                    FileOutputStream fos = new FileOutputStream(saveFile);
                    try {
                        archive.extractFile(fh, fos);
                        fos.flush();
                        fos.close();
                    } catch (RarException e) {
                        if(e.getType().equals(RarExceptionType.notImplementedYet)){
                        }
                    }finally{
                    }
                }
            }
            return true;
        } catch  (Exception e) {
            System.out.println("failed.");
            return false;
        }
    }
}

测试代码:

 public static void main(String[] args) throws Exception {
        System.out.println("unrar result:"+unrar("/Users/arthur/Desktop/Books/193.rar","/Users/arthur/Desktop/Books/"));
    }

结果截图:
[java]使用junrar解压文件时,上报解压进度的具体实现_第1张图片

当然了,笔者这个仅仅是一个demo,这个进度应当做一下格式化处理,这里不再赘述了。

你可能感兴趣的:(JAVA,问题解决方案)