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. swift3.0 基础练习-实现99乘法表

    用的方法比较笨 大致效果是这样 思路: 第一行 拼接一次 第二行 拼接二次 ... 第九行 拼接九次 //num控制个数 var num = 1 //result为结果 var result = &q ...

  2. 前端程序员经常忽视的一个JavaScript面试题

    在网上找到一个有关JavaScript的面试题,特整理如下: function Foo() { getName = function () { alert (1); }; return this; } ...

  3. PHPsthdy+xdebug

    PHPsthdy下载后查看phpinfo后会发现没有xdebug这一项: 1.phpStudy集成了XDebug扩展,所以不用单独下载XDebug. 2.打开XDebug扩展:右击PHPstudy的图 ...

  4. Hello ReactJS

    前言 React学习前先搭好环境,官网的例子看着比较分散.结合webpack就可以体验完整的es6开发流程了. 源码:https://github.com/Ryan-Miao/hello-react- ...

  5. 【分享】01. Eclipse for PHP + phpStudy 搭建php开发环境

    配置php编译器 配置phpStudy服务器项目发布目录 修改hosts文件127.0.0.1      www.350zx.cn 新建项目 启动的你的phpStudy  

  6. 你可能需要为你的APP适配iOS11

    WeTest 导读  iOS 11 为整个生态系统的 UI 元素带来了一种更加大胆.动态的新风格. 本文介绍了iOS11在UI方面做了哪些更新,有些更新可以为用户提供更加完美的体验,但也有的可能会给目 ...

  7. nopCommerce 3.9 大波浪系列 之 可退款的支付宝插件(上)

    一.简介 nop通过插件机制可以支持更多的支付扩展,我们通过编写支持退款的支付宝插件来更好的理解支付插件的扩展. 先分享下支付宝插件源码点击下载,由于时间原因,本篇只介绍使用该插件,下一篇结合插件进行 ...

  8. 40. leetcode 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  9. 移动端https抓包那些事--进阶篇

    上一次和大家介绍了手机端https抓包的初级篇,即在手机未root或者未越狱的情况下如何抓取https流量,但是当时分析应用时会发现,好多应用的https的流量还是无法抓取到,这是为什么呢? 主要原因 ...

  10. Unity 坐标 转换 详解 World世界坐标 Screen屏幕坐标 View视口坐标 GUI坐标 NGUI坐标 localPosition相对父级坐标

    在制作游戏中我们经常会遇到这样一个需求: 在人物模型的上面显示 名字.称号 一类的文字或者图片 如下图 人物模型属于是Camera1   UI Title信息属于NGUI Camera2 如下图 这时 ...