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 ...
随机推荐
- 【Mysql】Mysql关键字
ADD ALL ALTER ANALYZE AND AS ASC ASENSITIVE BEFORE BETWEEN BIGINT BINARY BLOB BOTH BY CALL CASCADE C ...
- Python:使用Kivy将python程序打包为apk文件
1.概述 Kivy是一套Python下的跨平台开源应用开发框架,官网,我们可以用 它来将Python程序打包为安卓的apk安装文件.以下是在windows环境中使用. 安装和配置的过程中会下载很多东西 ...
- python教程6-2:字符串标识符
标识符合法性检查. 1.字母或者下划线开始. 2.后面是字母.下划线或者数字. 3.检查长度大于等于1. 4.可以识别关键字. python35 idcheck.py idcheck.py impo ...
- 40. leetcode 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 25. leetcode 217. Contains Duplicate
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- 8. leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- Jersey实现Restful服务
jersey 是基于Java的一个轻量级RESTful风格的Web Services框架.以下我基于IDEA实现Restful完整Demo. 1.创建maven-web工程,后面就是正常的maven工 ...
- RabbitMQ入门-Topic模式
上篇<RabbitMQ入门-Routing直连模式>我们介绍了可以定向发送消息,并可以根据自定义规则派发消息.看起来,这个Routing模式已经算灵活的了,但是,这还不够,我们还有更加多样 ...
- Spring 并发访问的线程安全性问题
首先对于spring的IOC来说,对象是由Spring来帮我们管理,也就是在Spring启动的时候,在Spring容器中,由Spring给我们创建的,Spring会帮我们维护,一般都是单例的,也就是一 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...