import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
private static final int BUFFER = 8192;
/**
* 压缩文件
* @param srcPath
* @param dstPath
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件压缩失败)
*/
public static void fileToZip(String srcPath , String dstPath, boolean KeepDirStructure) {
File srcFile = new File(srcPath);
File dstFile = new File(dstPath);
if (!srcFile.exists()) {
System.err.println(srcPath + "不存在!");
return;
}
FileOutputStream fileOutputStream = null;
CheckedOutputStream checkedOutputStream = null;
ZipOutputStream zipOutputStream = null;
try {
fileOutputStream = new FileOutputStream(dstFile);
checkedOutputStream = new CheckedOutputStream(fileOutputStream, new CRC32());
zipOutputStream = new ZipOutputStream(checkedOutputStream);
String baseDir = "";
compress(srcFile, zipOutputStream, baseDir, KeepDirStructure);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (null != zipOutputStream) {
try {
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != checkedOutputStream) {
try {
checkedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 解压压缩文件
* @param zipFilePath
* @param dstPath
*/
public static void zipToFile(String zipFilePath , String dstPath) {
File zipFile = new File(zipFilePath);
if (!zipFile.exists()) {
System.err.println(zipFilePath + "不存在!");
return;
}
File dstFile = new File(dstPath);
if (!dstFile.exists()) {
dstFile.mkdirs();
}
ZipFile zip = null;
try {
zip = new ZipFile(zipFile);
decompress(zip , dstPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != zip) {
try {
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 真正开始压缩
* @param srcFile
* @param zipOutputStream
* @param baseDir
* @param keepDirStructure
*/
private static void compress(File srcFile, ZipOutputStream zipOutputStream, String baseDir, boolean keepDirStructure) {
if (srcFile.isDirectory()) {
compressDirectory(srcFile, zipOutputStream, baseDir, keepDirStructure);
} else {
compressFile(srcFile, zipOutputStream, baseDir, keepDirStructure);
}
}
/**
* 压缩一个文件
* @param srcFile
* @param zipOutputStream
* @param baseDir
* @param keepDirStructure
*/
private static void compressFile(File srcFile, ZipOutputStream zipOutputStream, String baseDir, boolean keepDirStructure) {
if (!srcFile.exists()) {
return;
}
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
System.out.println(srcFile.getAbsolutePath());
System.out.println(baseDir + srcFile.getName());
if (keepDirStructure) {
zipOutputStream.putNextEntry(new ZipEntry(baseDir + srcFile.getName()));
} else {
zipOutputStream.putNextEntry(new ZipEntry(srcFile.getName()));
}
int count;
byte data[] = new byte[BUFFER];
while ((count = bufferedInputStream.read(data, 0, BUFFER)) != -1) {
zipOutputStream.write(data, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedInputStream) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩一个目录
* @param srcFile
* @param zipOutputStream
* @param keepDirStructure
* @param baseDir
* @param keepDirStructure
*/
private static void compressDirectory(File srcFile, ZipOutputStream zipOutputStream, String baseDir, boolean keepDirStructure) {
File[] files = srcFile.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], zipOutputStream, baseDir + srcFile.getName() + "/", keepDirStructure);
}
}
/**
* 真正开始解压
* @param zip
* @param dstPath
* @throws IOException
*/
private static void decompress(ZipFile zip , String dstPath) throws IOException {
for(Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = null;
OutputStream out = null;
try{
in = zip.getInputStream(entry);
String outPath = (dstPath+"/"+zipEntryName).replaceAll("\\*", "/");
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
} finally {
if (null != out) {
out.close();
}
if (null != in) {
in.close();
}
}
}
}
public static void main(String[] args) {
String sourceFilePath = "E:\\test\\aaaa";
String destFilePath = "E:\\test\\aaaa.zip";
String newFilePath = "E:\\test\\aa";
fileToZip(sourceFilePath,destFilePath, true);
zipToFile(destFilePath, newFilePath);
}
}
文件压缩与解压缩
最新推荐文章于 2022-10-24 00:45:00 发布