zip7压缩

时间:2021-07-07 22:24:13

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