一、压缩cab文件
import com.ms.util.cab.CabCreator;
import com.ms.util.cab.CabFileEntry;
import com.ms.util.cab.CabFolderEntry;
import com.ms.util.cab.CabProgressInterface;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
/**
* @description 压缩cab文件
* @author sun
*/
public class CreatorTool implements CabProgressInterface {
public static void main(String[] args) {
try {
//输出目录
String outCabPath = "D:\\test888.cab";
//输入文件目录
String filePath = "D:\\file\\";
//创建CabProgressInterface实现类
CreatorTool test = new CreatorTool();
CabCreator cab = new CabCreator(test);
CabFolderEntry cabFolderEntry = new CabFolderEntry();
cabFolderEntry.setCompression(1, 0);
FileOutputStream fileOutputStream = new FileOutputStream(outCabPath);
cab.create(fileOutputStream);
cab.newFolder(cabFolderEntry);
FileInputStream fileInputStream = null;
//用来接受遍历出来的文件
ArrayList<String> list = new ArrayList<>();
CreatorTool.getAllFile(filePath, list);
for (String s : list) {
File file = new File(s);
fileInputStream = new FileInputStream(file);
CabFileEntry cabFileEntry = new CabFileEntry();
cabFileEntry.setDate(new Date());
if (!filePath.equals(file.getParent())) {
//操作多级目录
File file1=new File(filePath);
String absolutePath = file.getAbsolutePath();
String absoluteNewPath = absolutePath.replace(file1.getAbsolutePath(), "");
if(absoluteNewPath.substring(0,1).equals("\\")){
absoluteNewPath=absoluteNewPath.replaceFirst("\\\\","");
}
cabFileEntry.setName(absoluteNewPath);
} else {
cabFileEntry.setName(file.getName());
}
cabFileEntry.setSize(file.length());
cab.addStream(fileInputStream, cabFileEntry);
}
//完成cab文件压缩包完成 一定调用complete 不然是0字节
cab.complete();
fileOutputStream.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//这个类 可以加一些进度显示什么的
@Override
public Object progress(int i, long l, long l1, Object[] objects) {
return null;
}
/**
* @description 获取文件夹下的所有文件
* @param path 输入路径
* @param list 存文件AbsolutePath
*/
public static void getAllFile(String path, ArrayList<String> list) {
File file = new File(path);
//获取全部File
//返回目录名加文件名
//添加过滤器
//这些路径名表示此抽象路径名所表示目录中的文件。
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return true;
}
});
for (int i = 0; i < files.length; i++) {
//判断是否是目录,是的话继续递归
if (files[i].isDirectory()) {
getAllFile(files[i].getAbsolutePath(), list);
} else {
//否则添加到list
//获取全部包+文件名
list.add(files[i].getAbsolutePath());
}
}
}
}
二、解压cab文件
import com.ms.util.cab.CabDecoder;
import com.ms.util.cab.CabDecoderInterface;
import com.ms.util.cab.CabFileEntry;
import java.io.*;
/**
* @description 解压cab文件
* @author sun
*/
public class DecoderTool implements CabDecoderInterface {
public static void main(String[] args) {
try {
//输入cab压缩文件
FileInputStream fin = new FileInputStream("D:\\test.cab");
DecoderTool test = new DecoderTool();
//创建CabDecoder 传人CabDecoderInterface实现类
CabDecoder cab = new CabDecoder(fin, test);
cab.extract();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Object progress(int arg0, long arg1, long arg2, Object[] arg3) {
return null;
}
@Override
public boolean closeOutputStream(OutputStream out, CabFileEntry cfe, boolean arg2) {
//获取输出的数据结果out
FileOutputStream fos = null;
ByteArrayOutputStream bos = (ByteArrayOutputStream) out;
byte[] bytes = bos.toByteArray();
try {
//将流输出到文件中
InputStream in = new ByteArrayInputStream(bytes);
String path = "D:\\test888\\"+cfe.getName();
File file = new File(path);
if (!file.exists()) {
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
file.createNewFile();
}
fos = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(bytes.length);
return false;
}
@Override
public InputStream openCabinet(String arg0, String arg1) {
return null;
}
@Override
public OutputStream openOutputStream(CabFileEntry cfe) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
return bos;
}
@Override
public boolean reservedAreaData(int arg0, byte[] arg1, int arg2, byte[] arg3, int arg4) {
return false;
}
}
jar包
https://download.youkuaiyun.com/download/qq_21101587/12853992