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 ...
随机推荐
- (转)IntelliJ IDEA 破解方法
1.下载破解包http://pan.baidu.com/s/1gf9fXx5 2.解压并打开选中的文件 3.如图 4.IDEA选择License Server输入 http://127.0.0.1:1 ...
- 微信小程序(一)基本知识初识别
最近微信圈里小程序很火的样子,以前小程序刚开始的时候研究了一下,多日没关注发现一些东西都淡忘了,最后决定还是记录下来的好. 毕竟好记星比不上烂笔头嘛~ 另外有想学习小程序的同学,也可以参考下,当然如果 ...
- Ubuntu16.04配置Mac主题
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7072878.html 觉得有帮助?欢迎来打赏 Ubuntu配置Mac主题 下 ...
- ReentrantLock实现原理及源码分析
ReentrantLock是Java并发包中提供的一个可重入的互斥锁.ReentrantLock和synchronized在基本用法,行为语义上都是类似的,同样都具有可重入性.只不过相比原生的Sync ...
- PHPsthdy+xdebug
PHPsthdy下载后查看phpinfo后会发现没有xdebug这一项: 1.phpStudy集成了XDebug扩展,所以不用单独下载XDebug. 2.打开XDebug扩展:右击PHPstudy的图 ...
- (转)fiddler实现手机抓包的基础设置问题
电脑最好是笔记本,这样能和手机保持统一局域网内:其他不多说,直接说步骤了. 一.对PC(笔记本)参数进行配置 1. 配置fiddler允许监听到https(fiddler默认只抓取http格式的 ...
- UnityShader之遮挡透明
好久没写博客了,最近在学shader,不得不说,shader真的非常美妙,我沉迷其中无法自拔= = 之前做过一个遮挡透明的功能,当物体遮挡住主角时,该物体会变成半透明显示出主角.这次同样是遮挡透明的功 ...
- Modular javascript(javascript模块化编程)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛
"波导杯"安徽科技学院第七届程序设计大赛 原文章网页 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time: ...
- html5 mdn一些精彩的案例
https://developer.mozilla.org/zh_CN/docs/Games/Examples