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. Java之集合初探(一)

    一.集合概述.区别 集合是一种容器,数组也是一种容器 在Java编程中,装各种各样的对象(引用类型)的叫做容器. 为什么出现集合类? 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的 ...

  2. 安装cocoaPods第三方类库

    *1 检测gem 镜像文件     输入指令: gem sources -l     回车后得到镜像地址.可能是一个,也可能有好几个,常见两个如下 https://rubygems.org/     ...

  3. 【MYSQL】SQL 的join 区别

    ----------------------INNER JOIN---------------------------  1. 三表联合查询 select XX,XX  from a , b , c  ...

  4. hexo博客MathJax公式渲染问题

    这个问题自己很早以前便碰到了,用MathJax语法写的一些公式,在本地Markdown编译器上渲染是没问题的,可是部署到hexo博客中就出现问题了,之前我是使用图片代替公式应付过去了,今天从网上找了一 ...

  5. 从equals和==的区别开始

    今天我要谈的,不是具体的一个小程序的设计,也不是什么高大上的遗传算法群体智能什么什么的理念.今天我想就非常简单的一个问题,深入地解释一些东西,把自己的一些看法传达给大家.我相信,认真看下去,大家会有所 ...

  6. css3鼠标悬停图片抖动效果

    提供一个参考的链接 http://demo.lanrenzhijia.com/2015/pic0113/

  7. 使用Mapper专用的MyBatis Generator插件

    使用Maven执行MBG 这里有一个完整的例子,Mybatis-Spring,下面讲解的内容出自这个例子. 使用Maven插件的一个好处是可以将Maven中的属性使用${property}形式在gen ...

  8. Python爬虫从入门到放弃(二十)之 Scrapy分布式原理

    关于Scrapy工作流程回顾 Scrapy单机架构 上图的架构其实就是一种单机架构,只在本机维护一个爬取队列,Scheduler进行调度,而要实现多态服务器共同爬取数据关键就是共享爬取队列. 分布式架 ...

  9. .net Ajax使用

    function CheckUsername() { var strName = $("#txtUserName").val(); $.ajax({ type: "GET ...

  10. jQuery相关知识总结一

    1day-jquery 1. 1 jQuery 1概念 * JavaScript(ECMA/DOM/BOM)在实际开发中,使用比较麻烦,有浏览器兼容问题. * JavaScript类库(JS库) 的目 ...