7-zip 解压

7-zip 解压

1、引入依赖文件

sevenzipjbinding.jar
sevenzipjbinding-Allwindows.jar
<!-- https://mvnrepository.com/artifact/net.sf.sevenzipjbinding/sevenzipjbinding -->
<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding</artifactId>
    <version>9.20-2.00beta</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.sevenzipjbinding/sevenzipjbinding-all-windows -->
<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding-all-windows</artifactId>
    <version>9.20-2.00beta</version>
</dependency>

  

2、创建IInArchive

public class SevenZipServer {
Logger logger = Logger.getLogger(SevenZipServer.class);

/**
*This method USES zip7 to decompress the file, need the parameter decompression file, unzip the path
* Unpack The supported format is zip, rar,tar
* @param zipFile
* @param unpackPath
*/
public boolean extractZIP7(String zipFile,String unpackPath ){
IInArchive archive = null;
RandomAccessFile randomAccessFile = null;
boolean success = false;
try {
randomAccessFile = new RandomAccessFile(zipFile, "rw");
archive = SevenZip.openInArchive(null,
new RandomAccessFileInStream(
randomAccessFile));
int[] in = new int[archive.getNumberOfItems()];
for(int i=0;i<in.length;i++){
in[i] = i;
}
archive.extract(in, false, new Zip7ExtractCallback(archive, unpackPath));
success = true;
}catch (FileNotFoundException e){
logger.error(zipFile+"-FileNotFoundException occurs: ");
e.printStackTrace();
}catch (SevenZipException e){
logger.error("SevenZipException occurs: ");
e.printStackTrace();
}finally {
try {
archive.close();
randomAccessFile.close();
}catch (IOException e){

}
}
return success;
}

}

3、创建提取文件实现类

public class ExtractCallback implements IArchiveExtractCallback {
    private int index;
    private String packageName;
    private String unzipPath;
    private IInArchive inArchive;

    public ExtractCallback(IInArchive inArchive, String packageName,String unzipPath) {
        this.inArchive = inArchive;
        this.packageName = packageName;
        this.unzipPath = unzipPath;
    }

    public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
        this.index = index;
        final String path = (String) inArchive.getProperty(index, PropID.PATH);
        System.out.println("path === >"+ path);
        final boolean isFolder = (Boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
        return new ISequentialOutStream() {
            public int write(byte[] data) throws SevenZipException {
                try {
                    if (!isFolder) {
                        String path1 = unzipPath+path;

                        File file = buildUnPath(path1);
                        writeFiles(file, data);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return data.length;
            }
        };
    }

    public void prepareOperation(ExtractAskMode arg0) throws SevenZipException {
    }

    public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
        String path = (String) inArchive.getProperty(index, PropID.PATH);
        boolean isFolder = (Boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
        /*if (ZipUtils.checkOnlyGetDir(path) && !isFolder) {
            if (extractOperationResult != ExtractOperationResult.OK) {
                StringBuilder sb = new StringBuilder();
                sb.append("解压").append(packageName).append("包的").append(path).append("文件");
                sb.append("失败!");
            }
        }*/
        if (!isFolder) {
            if (extractOperationResult != ExtractOperationResult.OK) {
                StringBuilder sb = new StringBuilder();
                sb.append("解压").append(packageName).append("包的").append(path).append("文件");
                sb.append("失败!");
            }
        }

    }

    public void setTotal(long l) throws SevenZipException {

    }

    public void setCompleted(long l) throws SevenZipException {

    }


}

  4、工具类

public class ZipUtils {
    public static FileOutputStream fileOutputStream = null;
    public static void writeFiles(File file,byte[] data){
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(data);
            fileOutputStream.close();

        }catch (FileNotFoundException e){
            System.out.println(file.getName()+"没有找到该文件或目录");
        }catch (IOException e){
            System.out.println(file.getName()+"IO异常");
        }
    }
    public static File buildUnPath(String path){
        String[] pathDir = path.split("\\\\");
        if (pathDir.length != 1){
            new File(path.substring(0,path.lastIndexOf("\\"))).mkdirs();
        }
        File file = new File(path);
        return file;
    }

    public static byte[] getBytes(String filePath){
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
}

5、调用方式:

 1 public class Main {
 2     public static void main(String[] args) throws Exception{
 3         //解压文件
 4         String path = ""; // 压缩文件
 5         String unzipPath = ""; //解压目录
 6         SevenZipServer server = new SevenZipServer();
 7        /* System.out.println("---------------开始解压---------------------");
 8         server.unZip(path,unzipPath);
 9         System.out.println("---------------解压完成---------------------");*/
10 
11         System.out.println("---------------开始压缩---------------------");
12         server.zip(path,unzipPath, ArchiveFormat.ZIP);
13         System.out.println("---------------压缩完成---------------------");
14     }
15 }
View Code

  github 地址:https://github.com/1182632074/SevenZIP

转载于:https://www.cnblogs.com/wangshunyao/p/6811248.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
7-ZIP32.DLL 是一个供第三方软件调用进行压缩7z 或解压7z 的动态链接库。 2014/6/25 Version 9.22.00.01 新增的API。 (SevenZipGetLastError) 新增的API。 (SevenZipSfxConfigDialog) 新增的API。 (SevenZipSfxFileStoring) 支持长路径名。 (仅适用于基于NT的,在某种程度上是7-zip的本家兼容) 更改为返回FALSE,如果密码是错误指定CHECKARCHIVE_FULLCRC标志SevenZipCheckArchive。 加强错误处理。固定在没有返回现场应该返回一些错误。 定,如果缓冲区耗尽SevenZipGetArcFileName等来返回一个错误。 SevenZipGetMethod的略作修改的规范。相应的缓冲区不足的时间变化。 支持M_CHECK_FILENAME_ONLY国旗和M_CHECK_ALL_PATH在SevenZipOpenArchive。固定M_CHECK_FILENAME_ONLY的(“-R-”),是一个M_CHECK_ALL_PATH(“-R”)的默认状态。还可以支持(“-R0”)通过使用在同一时间的两个标志。 修正了无法显示的密码窗口时没有任何窗口的应用程序处理加密文件中的错误。 固定有在那里当然对话框的处理速度的项目显示100倍的情况。 修正了SevenZipGetArcAccessTimeEx必须得到更新的日期和时间的错误。 未安装-SLP到非支持的DLL,就像因此不能使用开关,一旦7-Zip文件管理器。 (您可以使用) 重建在7-ZIP9.22。 - 我可以在MF= FilterID开关指定压缩过滤器。下面的例子。 A-MF= BCJ2 a.7z a.tar A-MF=三角洲:4 a.7z a.wav A-MF= BCJ a.tar.xz a.tar - 我可以用最好的4GB内存在64位版本的Windows。 - 修正了几个错误。...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值