• java 代码解压7z(带密码)转载请注明出处,谢谢


    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>demo</groupId>
        <artifactId>spring</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
            <sevenzipjbinding.version>9.20-2.00beta</sevenzipjbinding.version>
            <commons.io.version>2.0.1</commons.io.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>net.sf.sevenzipjbinding</groupId>
                <artifactId>sevenzipjbinding</artifactId>
                <version>${sevenzipjbinding.version}</version>
            </dependency>
    
            <dependency>
                <groupId>net.sf.sevenzipjbinding</groupId>
                <artifactId>sevenzipjbinding-all-platforms</artifactId>
                <version>${sevenzipjbinding.version}</version>
            </dependency>
    
            <dependency>
                <artifactId>commons-io</artifactId>
                <groupId>commons-io</groupId>
                <version>${commons.io.version}</version>
            </dependency>
        </dependencies>
    
    </project>
    View Code
    package urar;
    
    import net.sf.sevenzipjbinding.*;
    import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
    import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
    import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
    import org.apache.commons.io.IOUtils;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.RandomAccessFile;
    import java.util.Arrays;
    
    public class URaR {
        /**
         * @param file7zPath(7z文件路径)
         * @param outPutPath(解压路径)
         * @param passWord(文件密码.没有可随便写,或空)
         * @return
         * @throws Exception
         * @Description (解压7z)
         */
        public static int un7z(String file7zPath, final String outPutPath, String passWord) throws Exception {
            IInArchive archive;
            RandomAccessFile randomAccessFile;
            randomAccessFile = new RandomAccessFile(file7zPath, "r");
            archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile), passWord);
            int numberOfItems = archive.getNumberOfItems();
            ISimpleInArchive simpleInArchive = archive.getSimpleInterface();
            for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
                final int[] hash = new int[]{0};
                if (!item.isFolder()) {
                    ExtractOperationResult result;
                    final long[] sizeArray = new long[1];
                    result = item.extractSlow(new ISequentialOutStream() {
                        public int write(byte[] data) throws SevenZipException {
                            try {
                                //判断压缩包内的文件是否存在
                                String parentFilePath = outPutPath + File.separator + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));
                                if (!new File(parentFilePath).exists()) {
                                    new File(parentFilePath).mkdirs();
                                }
                                IOUtils.write(data, new FileOutputStream(new File(outPutPath + File.separator + item.getPath()), true));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed
                        }
                    }, passWord);
                    if (result == ExtractOperationResult.OK) {
                        System.out.println(String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath()));
                    } else {
                        System.out.printf("Error extracting item: " + result);
                    }
                }
            }
            archive.close();
            randomAccessFile.close();
    
            return numberOfItems;
        }
    
        public static void main(String[] args) throws Exception {
    
            un7z("C:\Users\spring\Desktop\A_(3169).rar", "d:\111", "SPzfq");
        }
    }
    View Code

     解压

    http://sevenzipjbind.sourceforge.net/compression_snippets.html

  • 相关阅读:
    从零开始学正则(四),什么是正则回溯?
    JS 正则表达式^$详解,脱字符^与美元符$同时写表示什么意思?
    从零开始学正则(三),理解正则的分组与反向引用
    从零开始学正则(二),如何用正则匹配特定位置?理解正则的锚,先行断言
    从零开始学正则(一),学习正则字符组,量词表示法与正则分支
    深入了解angularjs中的$digest与$apply方法,从区别聊到使用优化
    js 从两道面试题加深理解闭包与箭头函数中的this
    js 五种绑定彻底弄懂this,默认绑定、隐式绑定、显式绑定、new绑定、箭头函数绑定详解
    【Spring深度分析】IoC/DI机制 配置文件式 基本实现
    BZOJ4311 向量(线段树分治+凸包)
  • 原文地址:https://www.cnblogs.com/sprinng/p/6150171.html
Copyright © 2020-2023  润新知