java原生的zip压缩和解压缩
缺点:含中文名的文件和文件夹名称会变乱码,但是通过这个类压缩和解压缩还是能能还原回去
第一次写博客,希望有什么不足之处,请大家提出
zip压缩工具:
package com.wqj.file.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;
/**
* ZIP压缩工具
*
* @author wqj
* @since 1.0
*/
public class ZipUtils {
private static final String EXT = ".zip";
private static final String ZIPSEPARATOR = "/";
/**
* 压缩文件或文件夹
* @param destPath 压缩文件保存路径
* @param srcFile 需要解压的文件或文件夹
* @throws IOException
*/
public static void zipFile(String destPath,File srcFile) throws IOException{
File destFile = new File(destPath);
zipFile(destFile, srcFile);
}
/**
* 压缩文件或文件夹
* @param destFile 压缩后的目录文件
* @param srcFile 需压缩的文件或文件夹
* @throws IOException
*/
public static void zipFile(File destFile,File srcFile) throws IOException{
//对输出文件做CRC32校验
CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(destFile), new CRC32());
ZipOutputStream zipOut = new ZipOutputStream(cos);
zipFile(srcFile, zipOut, "");
zipOut.flush();
zipOut.close();
}
/**
* 可以递归调用压缩方法
* @param srcFile 需压缩的源文件
* @param zipOut 压缩输出流
* @param basePath 上级名称
* @throws IOException
*/
public static void zipFile(File srcFile,ZipOutputStream zipOut,String basePath) throws IOException{
//判断是目录就直接putNextEntry
if(srcFile.isDirectory()){
File[] files = srcFile.listFiles();
if(files.length == 0){
zipOut.putNextEntry(new ZipEntry(basePath+srcFile.getName()+ZIPSEPARATOR));
zipOut.closeEntry();
System.out.println(basePath+srcFile.getName()+ZIPSEPARATOR);
}
//地柜调用压缩方法
for(File file:files){
zipFile(file,zipOut,basePath+srcFile.getName()+ZIPSEPARATOR);
}
//如果是文件对象节将该对象写入压缩文件
}else{
zipOut.putNextEntry(new ZipEntry(basePath+srcFile.getName()));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
System.out.println(basePath+srcFile.getName());
int read = 0;
byte[] buffer = new byte[1024];
while((read = bis.read(buffer)) != -1){
zipOut.write(buffer, 0, read);
}
bis.close();
zipOut.closeEntry();
}
}
/**
* 压缩文件或文件夹
* @param compressPath 需要压缩的文件或文件夹路径
* @throws IOException
*/
public static void zipFile(String compressPath) throws IOException{
File srcFile = new File(compressPath);
if(!srcFile.exists()){
throw new FileNotFoundException("not found file: "+srcFile.getAbsoluteFile());
}
String parentPath = srcFile.getParent();
String name = srcFile.getName();
String destPath = parentPath+ZIPSEPARATOR + name + EXT;
zipFile(destPath,srcFile);
}
/**
* 创建文件
* @param file 需要创建的file对象
* @return
* @throws IOException
*/
public static boolean createFile(File file) throws IOException{
if(!file.exists()){
mkdir(file.getParentFile());
}
return file.createNewFile();
}
/**
* 创建目录
* @param dir 需要创建的目录
*/
public static void mkdir(File dir){
if(!dir.exists()){
mkdir(dir.getParentFile());
}
dir.mkdir();
}
/**
* 如果file对象父级不存在就递归创建父级
* @param file
*/
public static void fileProber(File file){
File parentFile = file.getParentFile();
if(!parentFile.exists()){
fileProber(parentFile);
parentFile.mkdir();
}
}
public static void unzipFile(String unzipPath) throws Exception{
File unzipFile = new File(unzipPath);
String parentPath = unzipFile.getParent();
String destPath = parentPath+ZIPSEPARATOR;
unzipFile(unzipFile,destPath);
}
public static void unzipFile(File unzipFile,String destPath) throws Exception{
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(unzipFile), new CRC32());
ZipInputStream zipIn = new ZipInputStream(cis);
unzipFile(zipIn,destPath);
zipIn.close();
}
public static void unzipFile(ZipInputStream zipIn,String destPath) throws Exception{
File destFile = new File(destPath);
unzipFile(zipIn, destFile);
}
public static void unzipFile(ZipInputStream zipIn,File destFile) throws Exception{
ZipEntry entry = null;
while((entry = zipIn.getNextEntry()) != null){
String dir = destFile.getPath()+ZIPSEPARATOR+entry.getName();
File dirFile = new File(dir);
//检查父级文件夹是否存在,不存在则创建父级
fileProber(dirFile);
//如果是文件夹类型就创建文件夹
if(entry.isDirectory()){
dirFile.mkdir();
}else{
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dirFile));
byte[] buffer = new byte[1024];
int read = 0;
while((read = zipIn.read(buffer)) != -1){
bos.write(buffer, 0, read);
}
bos.close();
}
zipIn.closeEntry();
}
}
public static void main(String[] args) {
try {
// ZipUtils.zipFile("F:\\qq聊天图库");
ZipUtils.unzipFile(new File("F:\\qq聊天图库.zip"),"C:\\Test");
} catch (Exception e) {
e.printStackTrace();
}
}
}
压缩后:
解压缩后: