Java通用解压代码(RAR5.0以上包、Zip包、7Z包)

依赖:


net.sf.sevenzipjbinding
sevenzipjbinding
16.02-2.01


net.sf.sevenzipjbinding
sevenzipjbinding-all-platforms
16.02-2.01

方法:
public static void main(String[] args) throws Exception{

// decompressFile(“D:\testZip.rar”,“D:\aaa\testZip”);
// decompressFile(“D:\testRar.rar”,“D:\aaa\testRar”);
decompressFile(“D:\test7z.7z”,“D:\aaa\test7z”);

    //获取文件夹下所有文件名称
    File folder = new File("D:\\aaa\\testRar\\testRar");
    File[] files = folder.listFiles();
    List<String> fileNames=new ArrayList<>();
    List<String> foldernames=new ArrayList<>();
    for (File file : files) {
        if (file.isFile()) {
            fileNames.add(file.getName());
        } else {
            foldernames.add(file.getName());
        }
    }
    Map<String,List<String>> map=new HashMap();
    map.put("file",fileNames);
    map.put("folder",foldernames);

}


//zip、rar、7z三合一
public static void decompressFile(String inputFilePath,final String targetFileDir) {
    //解压7zip文件
    RandomAccessFile randomAccessFile = null;
    IInArchive inArchive = null;
    try {
        // 判断目标目录是否存在,不存在则创建
        File newdir = new File(targetFileDir);
        if (false == newdir.exists()) {
            newdir.mkdirs();
            newdir = null;
        }
        randomAccessFile = new RandomAccessFile(inputFilePath, "r");
        RandomAccessFileInStream t = new RandomAccessFileInStream(randomAccessFile);
        if (inputFilePath.endsWith("rar")) {
            inArchive = SevenZip.openInArchive(ArchiveFormat.RAR5, t);
        } else if (inputFilePath.endsWith("7z")) {
            inArchive = SevenZip.openInArchive(ArchiveFormat.SEVEN_ZIP, t);
        } else {
            inArchive = SevenZip.openInArchive(ArchiveFormat.ZIP, t);
        }
        ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
        for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
            final int[] hash = new int[]{0};
            System.out.println("item.isFolder()==" + item.isFolder()+"---1");
            if (!item.isFolder()) {
                ExtractOperationResult result;
                final long[] sizeArray = new long[1];
                result = item.extractSlow(new ISequentialOutStream() {
                    public int write(byte[] data) throws SevenZipException {
                        //写入指定文件
                        FileOutputStream fos;
                        try {
                            if (item.getPath().indexOf(File.separator) > 0) {
                                String path = targetFileDir + File.separator + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));
                                File folderExisting = new File(path);
                                if (!folderExisting.exists()) {
                                    new File(path).mkdirs();
                                }
                            }
                            fos = new FileOutputStream(targetFileDir + File.separator + item.getPath(), true);
                            fos.write(data);
                            fos.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        hash[0] ^= Arrays.hashCode(data); // Consume data
                        sizeArray[0] += data.length;
                        return data.length; // Return amount of consumed data
                    }
                });

                if (result == ExtractOperationResult.OK) {
                    System.out.println(String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath())+"---2");

                } else {
                    System.err.println("Error extracting item: " + result+"---3");
                }
            }else {
               //创建空文件夹
               String filepath=targetFileDir + File.separator + item.getPath();
               new File(filepath).mkdirs();
            }
        }
    } catch (Exception e) {
        System.err.println("Error occurs: " + e);
        e.printStackTrace();
        System.exit(1);
    } finally {
        if (inArchive != null) {
            try {
                inArchive.close();
            } catch (SevenZipException e) {
                System.err.println("Error closing archive: " + e);
            }
        }
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                System.err.println("Error closing file: " + e);
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值