当前位置:   article > 正文

java工具类解压缩zip和rar_com.github.junrar.archive

com.github.junrar.archive

解压缩java工具类

import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import com.ramostear.unaboot.common.UnaBootConst;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import java.io.*;
import java.util.Enumeration;

/**
 * 压缩包工具类
 */
public class ArchiveUtils {

    public static void unzip(String source,String target) throws IOException {
        unzip(new File(source),target);
    }

    public static void unzip(File source,String target) throws IOException {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        File targetFile = new File(target);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        ZipFile zipFile = new ZipFile(source);
        for(Enumeration entries = zipFile.getEntries();entries.hasMoreElements();){
            ZipEntry zipEntry = (ZipEntry) entries.nextElement();
            String entryName = zipEntry.getName();
            inputStream = zipFile.getInputStream(zipEntry);
            String outPath = (target+"/"+entryName).replaceAll("\\*","/");
            File file = new File(outPath.substring(0,outPath.lastIndexOf("/")));
            if(!file.exists()){
                file.mkdirs();
            }
            if(new File(outPath).isDirectory()){
                continue;
            }
            outputStream = new FileOutputStream(outPath);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = inputStream.read(bytes))>0){
                outputStream.write(bytes,0,length);
            }
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
        zipFile.close();
        System.gc();
    }

    public static void unRar(String source,String target) throws Exception{
        File targetFile = new File(target);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        Archive archive = new Archive(new File(source));
        if(archive != null){
            FileHeader header = archive.nextFileHeader();
            while(header != null){
                if(header.isDirectory()){
                    File file = new File(target+ File.separator+header.getFileNameString());
                    file.mkdirs();
                }else{
                    File file = new File(target+File.separator+header.getFileNameString());
                    try{
                        if(!file.exists()){
                            if(!file.getParentFile().exists()){
                                file.getParentFile().mkdirs();
                            }
                            file.createNewFile();
                        }
                        FileOutputStream outputStream = new FileOutputStream(file);
                        archive.extractFile(header,outputStream);
                        outputStream.close();
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
                header = archive.nextFileHeader();
            }
        }
        archive.close();
    }
    
    public static void main(String[] args) throws Exception {
		//unRar("F:/web.rar", "F:/test"); 注意需要是rar4压缩
    	unzip("F:/web.zip", "F:/test"); 
    	
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

依赖

<dependency>
   <groupId>com.github.junrar</groupId>
    <artifactId>junrar</artifactId>
    <version>1.0.1</version>
</dependency>
<dependency>
 <groupId>ant</groupId>
    <artifactId>ant</artifactId>
    <version>1.6.5</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号