zip7压缩
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压缩的更多相关文章
- ASP.NET Core 中间件之压缩、缓存
前言 今天给大家介绍一下在 ASP.NET Core 日常开发中用的比较多的两个中间件,它们都是出自于微软的 ASP.NET 团队,他们分别是 Microsoft.AspNetCore.Respons ...
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...
- 【开源毕设】一款精美的家校互动APP分享——爱吖校推 [你关注的,我们才推](持续开源更新3)附高效动态压缩Bitmap
一.写在前面 爱吖校推如同它的名字一样,是一款校园类信息推送交流平台,这么多的家校互动类软件,你选择了我,这是我的幸运.从第一次在博客园上写博客到现在,我一次一次地提高博文的质量和代码的可读性,都是为 ...
- linux 如何对文件解压或打包压缩
tar命令用与对文件打包压缩或解压,格式: tar [选项] [文件] 打包并压缩文件: tar -czvf 压缩包名 .tar.gz 解压并展开压缩包: tar -xzvf 压缩包名 .tar. ...
- linux压缩和解压缩命令大全
.tar 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName ------------------------------------- ...
- 快速开发Grunt插件----压缩js模板
前言 Grunt是一款前端构建工具,帮助我们自动化搭建前端工程.它可以实现自动对js.css.html文件的合并.压缩等一些列操作.Grunt有很多插件,每一款插件实现某个功能,你可以通过npm命名去 ...
- H5图片压缩与上传
接到需求,问前端是否可以压缩图片?因为有的图片太大,传到服务器上再压缩太慢了.意识里没有这么玩过,早上老大丢来一个知乎链接,一看,原来前辈们已经用canvas实现了(为自己的见识羞愧3秒钟,再马上开干 ...
- 压缩javascript文件方法
写在前面的话:正式部署前端的时候,javascript文件一般需要压缩,并生成相应的sourcemap文件,对于一些小型的项目开发,这里提供一个简单的办法. ======正文开始====== 1.下载 ...
- HTML5网页录音和压缩,边猜边做..(附源码)
宣传一下自己的qq群: (暗号:C#交流) 欢迎喜欢C#,热爱C#,正在学习C#,准备学习C#的朋友来这里互相学习交流,共同进步 群刚建,人不多,但是都是真正热爱C#的 我也是热爱C#的 希望大家可以 ...
随机推荐
- Spring源码情操陶冶-AbstractApplicationContext#postProcessBeanFactory
阅读源码有利于陶冶情操,承接前文Spring源码情操陶冶-AbstractApplicationContext#prepareBeanFactory 约定:web.xml中配置的contextClas ...
- python-继承类执行的流程
在读python数据机构与算法, 发现了下面这个例子, 之前没有碰到过, 记录下来 In [17]: class A: def f(self): self.g() def g(self): print ...
- css 实现三角形、圆形
.div { width:0px; height:0px; border:100px solid red; border-color:red red transparent transparent; ...
- /etc/shadow,/etc/passwd,/etc/shadow,/etc/passwd文件的内容解释
1.1 /etc/passwd文件内容格式 该目录存储的是操作系统用户信息,该文件为所有用户可见 用户名: 密码 : uid : gid :用户描述:主目录:登陆shell 举个 ...
- Node.js 入门简介
Node.js简介 1.1 简介 V8引擎本身就是用于Chrome浏览器的JS解释部分,但是Ryan Dahl鬼才般的把这个V8搬到了服务器上,用于做服务器的软件. Node.js是一个专注于实现高性 ...
- fiddler - 测试手机端软件
在执行"Web测试"的时候,需要在PC配置HOSTS,那么当我们在执行"手机端测试"的时候需要如何配置HOSTS呢? iOS越狱后可以配置HOSTS,不过可能会引入bugs: Android使用ROO ...
- shell 变量的使用
变量定义 name="xiaoming"; age=12: 变量名和等号之间不能有空格,否则会报错,同时变量名的命名和其他语言的命名规则基本一样 首个字符必须为字母(a-z,A-Z ...
- Oracle用户操作
1.超级管理员的身份登录oracle:sqlplus / as sysdba 2.显示当前连接用户SQL> show user 3.新建用户并授权SQL> create user a ...
- iOS的消息转发机制详解
iOS开发过程中,有一类的错误会经常遇到,就是找不到所调用的方法,当然这类问题比较好解决,给当前对象或其父类对象添加该方法即可,使得编译器在编译时能正确找到该方法:或者,还有另外的方法,由于Objec ...
- 关于error C2872: 'LONG_PTR' : ambiguous symbol
今天用VS2010创建了一个add-in工程,使用常量INVALID_HANDLE_VALUE时,编译器就提示如下错误: error C2872: 'LONG_PTR' : ambiguous sym ...