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、调用方式:
public class Main {
public static void main(String[] args) throws Exception{
//解压文件
String path = ""; // 压缩文件
String unzipPath = ""; //解压目录
SevenZipServer server = new SevenZipServer();
/* System.out.println("---------------开始解压---------------------");
server.unZip(path,unzipPath);
System.out.println("---------------解压完成---------------------");*/
System.out.println("---------------开始压缩---------------------");
server.zip(path,unzipPath, ArchiveFormat.ZIP);
System.out.println("---------------压缩完成---------------------");
}
}
github 地址:https://github.com/1182632074/SevenZIP
7-zip 解压的更多相关文章
- CentOS7下zip解压和unzip压缩文件
1.安装zip.unzip应用. yum install zip unzip
- MySQL For Windows Zip解压版安装
前言 Windows 下 MySQL 有msi和zip解压安装版两种,而zip版只需解压并做简单配置后就能使用,我个人比较喜欢这种方式. 注意我们这里说的MySQL是指MySQL服务器,有很多初学的同 ...
- 解决ubuntu中zip解压的中文乱码问题
转自解决ubuntu中zip解压的中文乱码问题 在我的ubuntu12.10中,发现显示中文基本都是正常的,只有在解压windows传过来的zip文件时,才会出现乱码.所以,我用另一个方法解决中文乱码 ...
- JAVA zip解压 MALFORMED 错误
最近在在使用zip 解压时,使用JDK1.7及以上版本在解压时,某些文件会报异常 Exception in thread "main" java.lang.IllegalArgum ...
- windows下tomcat zip解压版安装方法
下面记录一下在win7(32位)系统下,安装zip解压版的方法: 一.下载zip压缩包 地址:http://tomcat.apache.org/download-80.cgi 二.解压 我把解压包解压 ...
- 【转载】在linux下别用zip 用tar来压缩文件 zip解压后还是utf-8 window10是GBK
3.2 使用 unzip 命令解压缩 zip 文件 将 shiyanlou.zip 解压到当前目录: $ unzip shiyanlou.zip 使用安静模式,将文件解压到指定目录: $ un ...
- Java zip解压,并遍历zip中的配置文件 .cfg或.properties
1.解析cfg或properties配置文件 讲配置文件,读取,并封装成为map类型数据 /** * 解析cfg文件 * * @param cfgFile * @return */ public st ...
- ubuntu zip解压
您好,zip xx.zip压缩,unzip xx.zip 解压,tar zcvf xx.tar.gz压缩tar zxvf xx.tar.gz解压
- zip 解压脚本
zip 解压脚本 gpk-unzip.py #!/usr/bin/env python # -*- coding: utf-8 -*- # unzip-gbk.py import os import ...
- mysql5.5.x.zip 解压版安装教程
一,前言 记一次安装解压版的mysql 5.5的经过,参考了一些文章,也遇到了一些错误,最终都安装成功了.在这里记录一下安装的过程,一方面自己做一个记录,领一方面给大家提供一份参考. 二,环境 1,w ...
随机推荐
- (转)sql通配符
背景:一次搞清sql查询中的通配符问题. 1 sql通配符 通配符主要以下几种:%._.[].[^] . 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符.SQL 通配符必须与 LIKE ...
- (转)TCP和UDP之间的区别
TCP和UDP区别 TCP UDP 是否连接 面向连接 面向非连接 传输可靠性 可靠的 不可靠的 应用场合 传输大量的数据 少量数据 速度 慢 快 OSI 和 TCP/IP 模型在传输层 ...
- C语言基础 - 实现单向链表
回归C基础 实现一个单向链表,并有逆序功能 (大学数据结构经常是这么入门的) //定义单链表结构体 typedef struct Node{ int value; struct Node *next; ...
- 学习jdbc学习笔记
1.jdbc: java database connection jdbc规范:是sun公司制定的一套连接操作数据库的接口. 我们必须要用具体的驱动类去连接操作数据库. 每个 ...
- testbench中$display查看例化model里面信号方法以及$realtime用法
前言 此为测试语法,不可综合: 流程: 1.在tb中可以这么写,检测clk_t_en的高电平,输出仿真时间位置,想查看的cnt_t是底层模块中的.这么会使得时间延迟一个周期: always @(pos ...
- WPF个人助手更新
大家好,这次更新主要是去除一些无关的功能,界面做了很大的调整,以前都是自己写的 UI ,最近也引入了 WPF-UI ,挺不错的,特此表示感谢,也希望大家会喜欢,别的也就不多说了,本软件以实用性为主,采 ...
- 小球自由落体动态模拟(Position Based Simulation)
在过去的几十年中,基于物理的三维物体动态模拟成为了计算机图形学的研究热点,其中最常见的方法是基于力(force-based)的模拟方法,比如弹簧质点模型,它把物体抽象成一系列质点以及连接这些质点的弹簧 ...
- opencv 基本绘图函数
opencv 常用的数据结构和函数 颜色空间转换函数 cvtColor 函数 cvtColor 函数是opencv 中的颜色空间转换函数.可以实现rgb向hsv hsi等颜色空间的转换,也可以转换成灰 ...
- sns社交系统ThinkSNS+ 更新至V0.8.2,新增圈子功能
sns社交系统"ThinkSNS+"于7月15日发布了V0.8.0,含开源版本web+H5,及Android APP和iOS APP客户端. V0.8.2版本将于7月29日(本周六 ...
- python----------装饰器应用练习
1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码注意:从文件中读出字符串形式的字典,可以用eval('{"name& ...