package compress;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class Compress {
private String path;
private String name;
/**
* path文件或文件夹压缩后保存的路径
* name文件或文件夹压缩后保存的名字
* is是否创建path(如果path路径不存在)
* path、name这两个参数都可以为空,即默认保存在要压缩文件或文件夹所在的目录,压缩后的文件名为要压缩的文件或文件夹的名称
* @param path
* @param name
* @param is
*/
public Compress(String path, String name, boolean is) {
if(null != path && !"".equals(path)) {
File file = new File(path);
if(!file.exists() && is) {
file.mkdirs();
}
}
this.path = path;
this.name = name;
}
/**
* 压缩功能
* file要压缩的文件或文件夹
* @param file
*/
public void compress(File file) {
if(!file.exists()) {
throw new RuntimeException("压缩路径文件或文件夹不存在:" + file.getAbsolutePath());
}
path = null == path || "".equals(path) ? file.getParent() : path;
name = (null == name || "".equals(name) ? file.getName().split("[.]")[0] : name) + ".zip";
try {
FileOutputStream out = new FileOutputStream(path + "//" + name);
CheckedOutputStream checked = new CheckedOutputStream(out, new CRC32());
ZipOutputStream write = new ZipOutputStream(checked);
compress(file, write, "");
write.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 判断是目录还是文件
* @param file被压缩的文件或文件夹
* @param write写流
* @param parent路径
*/
private void compress(File file, ZipOutputStream write, String parent) {
if (file.isDirectory()) {
System.out.println("压缩目录:" + file.getName());
compressDirectory(file, write, parent);
} else {
System.out.println("压缩文件:" + file.getName());
compressFile(file, write, parent);
}
}
/**
* 压缩一个目录
* @param dir
* @param write
* @param parent
*/
private void compressDirectory(File dir, ZipOutputStream write, String parent) {
if (!dir.exists()) {
return;
}
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], write, parent + dir.getName() + "/");
}
}
/**
* 压缩一个文件
* @param file
* @param write
* @param parent
*/
private void compressFile(File file, ZipOutputStream write, String parent) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
String name = new String((parent + file.getName()).getBytes(), "UTF-8");
//此处保存文件名如果含有中文,文件名会出现乱码(使用加压缩工具打开的时候),但是使用程序解压一切正常
ZipEntry entry = new ZipEntry(name);
write.putNextEntry(entry);
int count;
byte data[] = new byte[1024];
while ((count = bis.read(data, 0, 1024)) != -1) {
write.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 解压缩功能,类似与压缩工具中的解压到当前文件夹
* @param file被解压的zip文件
*/
public static void unCompress(File file) {
if(!file.exists()) {
throw new RuntimeException("解压缩路径文件不存在:" + file.getAbsolutePath());
}
try {
FileInputStream in = new FileInputStream(file);
CheckedInputStream check = new CheckedInputStream(in, new CRC32());
ZipInputStream read = new ZipInputStream(check);
unCompress(read, file.getParent());
read.close();
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 路径创建
* @param read
* @param path
* @throws Exception
*/
private static void unCompress(ZipInputStream read, String path) throws Exception{
ZipEntry entry = null;
while(null != (entry = read.getNextEntry())) {
String name = entry.getName();
File file = new File(path + "//" + name);
file.getParentFile().mkdirs();
unCompressFile(read, file);
System.out.println("解压缩文件:" + name);
read.closeEntry();
}
}
/**
* 解压文件
* @param read
* @param file
*/
private static void unCompressFile(ZipInputStream read, File file) {
try{
BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(file));
int count;
byte data[] = new byte[1024];
while ((count = read.read(data, 0, 1024)) != -1) {
write.write(data, 0, count);
}
write.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}