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 ...
随机推荐
- (转)log4j(一)——为什么要用log4j?
1 试验环境 OS:win7 JDK:jdk7 Log4j:1.2.17(好尴尬,原本是想试验下log4j2的,结果阴差阳错用了这个版本,不过幸好,试验也不白试验,试验的作用是一样的) 2 先看两个简 ...
- (转)Centos7 Nginx安装
场景:工作中使用的suse,因为系统可可查资料太少,且系统中一些功能的确实,导致很多集群中功能无法顺利测试通过,在Centos上面进行测试,能够更快的熟悉项目的架构过程! 1 安装准备 首先由于ngi ...
- Objectiv-C UIKit基础 NSLayoutConstraint的使用(VFL实现)
利用VFL可视化语言 (简单的抛砖引玉) 构建3个View 橙色和绿色左中右间隔20 上间隔40 高为200 蓝色在橙色内(0,0)处 宽高为橙色的一半 实现效果如下 由于atutosize和auto ...
- java线程数过高原因分析
作者:鹿丸不会多项式 出处:http://www.cnblogs.com/hechao123 转载请先与我联系. 一.问题描述 前阵子我们因为B机房故障,将所有的流量切到了A机房,在经历了推送+ ...
- C语言编译过程(转)
内容摘要 : C语言编译的整个过程是非常复杂的,里面涉及到的编译器知识.硬件知识.工具链知识都是非常多的,深入了解整个编译过程对工程师理解应用程序的编写是有很大帮助的,希望大家可以多了解一些,在遇到问 ...
- iptables中DNAT的配置方法
1.一对一流量完全DNAT 首先说一下网络环境,普通主机一台做防火墙用,网卡两块 eth0 192.168.0.1 内网 eth1 202.202.202.1 外网 内网中一台主机 192.168. ...
- mybatis 详解(一)------JDBC
1.什么是MyBatis? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且 ...
- JS基础学习篇(一)
近来一直在学习js和jquery.刚刚进入前端工作还没有多久,虽然大学里学习的是编程自认为也学的还可以,但前端接触的不多,一直认为前端十分简单.其实不然,特别是工作的时候要自己设计一个完整的项目前端, ...
- 关于无法使用python执行进入百度页面的代码修改
前几天听了个坑爹的视频教学,按照你们的方法做了,但尼玛,执行下来各种问题啊: 首先进入页面,总是提示开发者模式,删了下次执行又挂了,于是乎我就找网上帖子解决问题,果然被我解决了 先装这两个文件,把浏览 ...
- Spring+SpringMVC+MyBatis+easyUI整合
进阶篇 Spring+SpringMVC+MyBatis+easyUI整合进阶篇(一)设计一套好的RESTful API 优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化 ...