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、创建解压archive

package server;

import compress.Zip7CompressCallBack;
import compress.CompressZipEntry;
import entity.Item;
import extact.Zip7ExtractCallback;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.impl.RandomAccessFileOutStream;
import org.apache.log4j.Logger;
import util.CompressOutItemStructure;
import static util.ZipUtils.*;
import java.io.*; public class SevenZipServer {
Logger logger = Logger.getLogger(SevenZipServer.class);
/**
* supper tar zip
* @param filename Compressed file name and path
* @param compressDir Wait for compressed files or folder paths
* @param format The format of the compressed
*/
public boolean compressZIP7(String compressDir ,String filename,ArchiveFormat format) {
Item[] items = CompressOutItemStructure.create(compressDir);
boolean success = false;
RandomAccessFile raf = null;
IOutCreateArchive outArchive = null;
try {
raf = new RandomAccessFile(filename, "rw");
outArchive = SevenZip.openOutArchive(format);
outArchive.createArchive(new RandomAccessFileOutStream(raf),
items.length, new Zip7CompressCallBack(items));
success = true;
} catch (SevenZipException e) {
logger.error(format.getMethodName()+"-Error occurs:");
e.printStackTraceExtended();
} catch (Exception e) {
logger.error("Error occurs: " + e);
} finally {
if (outArchive != null) {
try {
outArchive.close();
} catch (IOException e) {
logger.error("Error closing archive: " + e);
success = false;
}
}
if (raf != null) {
try {
raf.close();
} catch (IOException e) {
logger.error("Error closing file: " + e);
success = false;
}
}
}
if (success) {
System.out.println("Compression operation succeeded");
}
return success;
}
}

3、解压的回调类方法

package compress;

import entity.Item;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.OutItemFactory;
import net.sf.sevenzipjbinding.util.ByteArrayStream; /**
* Created by wangshunyao on 2017/5/4.
*/
public class Zip7CompressCallBack implements IOutCreateCallback<IOutItemBase> { private Item[] items;
public Zip7CompressCallBack(){} public Zip7CompressCallBack(Item[] items) {
this.items = items;
} public void setOperationResult(boolean operationResultOk)
throws SevenZipException {
// Track each operation result here
} public void setTotal(long total) throws SevenZipException {
// Track operation progress here
} public void setCompleted(long complete) throws SevenZipException {
// Track operation progress here
} public IOutItemBase getItemInformation(int index,
OutItemFactory<IOutItemBase> outItemFactory) {
IOutItemBase item = outItemFactory.createOutItem();
String format = item.getArchiveFormat().getMethodName(); if (items[index].getContent() == null) {
if(format.equals("Tar")){
((IOutItemTar)item).setPropertyIsDir(true);
}else if(format.equals("Zip")){
((IOutItemZip)item).setPropertyIsDir(true);
}
} else {
item.setDataSize((long) items[index].getContent().length);
}
if(format.equals("Tar")){
((IOutItemTar)item).setPropertyPath(items[index].getPath());
}else if(format.equals("Zip")){
((IOutItemZip)item).setPropertyPath(items[index].getPath());
}
return item;
} public ISequentialInStream getStream(int i) throws SevenZipException {
if (items[i].getContent() == null) {
return null;
}
return new ByteArrayStream(items[i].getContent(), true);
}
}

  4、解压文件加的目录器构造:

package util;

import entity.Item;
import java.io.File; /**
* Created by wangshunyao on 2017/5/5.
*/
public class CompressOutItemStructure {
static int fileNumber = 0;
static Item[] items = null;
static String parent = ""; /**
* file item format is fileName String,btye[] content
* folder dir+"/"+fileName,byte[] content
* @param compressDir
* @return
*/
public static Item[] create(String compressDir) {
File file = new File(compressDir);
parent = file.getParent() == null ? "":file.getParent();
fileCounter(file);
items = new Item[fileNumber];
packet(file);
return items;
} public static void fileCounter(File file){
if (!file.exists()){
throw new RuntimeException("not found file "+file.getPath());
}
if (file.isFile()){
fileNumber++;
}else{
for(File inFile : file.listFiles()){
if (inFile.isFile()){
fileNumber++;
}else{
fileCounter(inFile);
}
}
}
}
static int tempFileIndex = 0;
public static void packet(File file){
if (!file.exists()){
throw new RuntimeException("not found file "+file.getPath());
}
if (file.isFile()){
items[tempFileIndex] = readFile(file);
tempFileIndex++;
}else{
for(File file1 : file.listFiles()){
if (file1.isFile()){
items[tempFileIndex] = readFile(file1);
tempFileIndex++;
}else{
packet(file1);
}
}
}
} public static Item readFile(File file){
String path = file.getPath();
if (!("".equals(parent))){
path = file.getPath().replace(parent,"");
}
return new Item(path,ZipUtils.getBytes(file));
} }

  5、Item如下:

package entity;

/**
* Created by wangshunyao on 2017/5/5.
*/
public class Item {
private String path;
private byte[] content; public Item(String path, String content) {
this(path, content.getBytes());
} public Item(String path, byte[] content) {
this.path = path;
this.content = content;
} public String getPath() {
return path;
} public void setPath(String path) {
this.path = path;
} public byte[] getContent() {
return content;
} public void setContent(byte[] content) {
this.content = content;
}
}

  

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

zip7压缩的更多相关文章

  1. ASP.NET Core 中间件之压缩、缓存

    前言 今天给大家介绍一下在 ASP.NET Core 日常开发中用的比较多的两个中间件,它们都是出自于微软的 ASP.NET 团队,他们分别是 Microsoft.AspNetCore.Respons ...

  2. Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩

    目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...

  3. 【开源毕设】一款精美的家校互动APP分享——爱吖校推 [你关注的,我们才推](持续开源更新3)附高效动态压缩Bitmap

    一.写在前面 爱吖校推如同它的名字一样,是一款校园类信息推送交流平台,这么多的家校互动类软件,你选择了我,这是我的幸运.从第一次在博客园上写博客到现在,我一次一次地提高博文的质量和代码的可读性,都是为 ...

  4. linux 如何对文件解压或打包压缩

    tar命令用与对文件打包压缩或解压,格式: tar [选项] [文件] 打包并压缩文件: tar -czvf  压缩包名 .tar.gz 解压并展开压缩包: tar -xzvf  压缩包名 .tar. ...

  5. linux压缩和解压缩命令大全

    .tar 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName ------------------------------------- ...

  6. 快速开发Grunt插件----压缩js模板

    前言 Grunt是一款前端构建工具,帮助我们自动化搭建前端工程.它可以实现自动对js.css.html文件的合并.压缩等一些列操作.Grunt有很多插件,每一款插件实现某个功能,你可以通过npm命名去 ...

  7. H5图片压缩与上传

    接到需求,问前端是否可以压缩图片?因为有的图片太大,传到服务器上再压缩太慢了.意识里没有这么玩过,早上老大丢来一个知乎链接,一看,原来前辈们已经用canvas实现了(为自己的见识羞愧3秒钟,再马上开干 ...

  8. 压缩javascript文件方法

    写在前面的话:正式部署前端的时候,javascript文件一般需要压缩,并生成相应的sourcemap文件,对于一些小型的项目开发,这里提供一个简单的办法. ======正文开始====== 1.下载 ...

  9. HTML5网页录音和压缩,边猜边做..(附源码)

    宣传一下自己的qq群: (暗号:C#交流) 欢迎喜欢C#,热爱C#,正在学习C#,准备学习C#的朋友来这里互相学习交流,共同进步 群刚建,人不多,但是都是真正热爱C#的 我也是热爱C#的 希望大家可以 ...

随机推荐

  1. 初探JavaScript的截图实现

    最近参与了网易炉石盒子的相关页面开发,在做卡组分享页(地址:炉石盒子卡组分享),有个需求:用户可以把这个卡组以图片的形式分享给好友.最初的的做法是使用服务器把该页面转换成图片,然后把图片地址返回给前端 ...

  2. HDOJ2012-素数判定

    Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数.   I ...

  3. JS浏览器对象:window对象、History、Location对象、Screen对象

    一.JS浏览器对象-window 1.window对象 window对象是BOM的核心,window对象指当前的浏览器窗口 所有JavaScript全局对象.函数以及变量均自动成为window对象的成 ...

  4. 一个最最简易的RPC框架雏形---转载自梁飞的博客

    查阅RPC与HTTP区别的时候, 无意间发现一篇博客,内容是一个简易的RPC服务框架, 仔细一看, 不得了,博主竟然就是阿里dubbo的作者. 原文链接在此: http://javatar.iteye ...

  5. C# 接口基础学习

    什么是接口  接口,在表面上是由几个没有主体代码的方法.属性.索引器.事件,或者它们的组合的集合体,有唯一的名称,可以被类或结构或者其他接口所实现(或者也可以说继承).它在形式上可能是如下的样子: i ...

  6. ffmpeg编解码视频导致噪声增大的一种解决方法

    一.前言 ffmpeg在视音频编解码领域算是一个比较成熟的解决方案了.公司的一款视频编辑软件正是基于ffmpeg做了二次封装,并在此基础上进行音视频的编解码处理.然而,在观察编码后的视频质量时,发现图 ...

  7. 【数学】HPU--1037 一个简单的数学题

    1037: 一个简单的数学题 [数学] 时间限制: 1 Sec 内存限制: 128 MB提交: 259 解决: 41 统计 题目描述 小明想要知道$a^b$的值,但是这个值会非常的大. 所以退而求其次 ...

  8. 安徽省2016“京胜杯”程序设计大赛_H_单身晚会

    单身晚会 Time Limit: 1000 MS Memory Limit: 65536 KB Total Submissions: 53 Accepted: 16 Description ​ZJ和Z ...

  9. Java中parse()和valueOf(),toString()的区别

    1.parse()是SimpleDateFomat里面的方法,你说的应该是parseInt()或parsefloat()这种方法吧, 顾名思义 比如说parseInt()就是把String类型转化为i ...

  10. 终端管理软件tmux

    tmux - terminal multiplexer 我们在服务器上进行操作,写代码,测试,运行服务,都会用到这样的工具,以前使用GNU screen,但是在最近使用了tmux之后,我觉得tmux真 ...