使用java自带的zip包压缩、解压文件/文件夹。显示进度。在windows XP SP3 + jdk1.7下测试不存在中文路径乱码问题。
import java.io.*;
import java.util.zip.*;
public class Zip {
int filesCount;//总数量
int now;//已执行数量
ZipInputStream Zin;
/**
* 压缩文件或文件夹
* @param srcPath 被压缩的文件或文件夹
* @param targetFolderPath 压缩文件存放路径
* @throws Exception
*/
public void zip(String srcPath,String targetFolderPath) throws Exception
{
filesCount = getCount(srcPath);
now = 0;
File fsrc = new File(srcPath);
File ftg = new File(targetFolderPath);
if(!fsrc.exists() || !ftg.exists()){
throw new Exception("Zip::zip 路径不存在");
}
ZipOutputStream out = new ZipOutputStream(
new FileOutputStream(targetFolderPath + File.separator + fsrc.getName() + ".zip"));
BufferedOutputStream bo = new BufferedOutputStream(out);
zip(out, fsrc, fsrc.getName(), bo);
bo.close();
out.close(); //输出流关闭
}
private void zip(ZipOutputStream out, File f, String base, BufferedOutputStream bo) throws Exception
{
if (f.isDirectory()) {
File[] fl = f.listFiles();
if (fl.length == 0) {
//创建zip压缩进入点base
out.putNextEntry(new ZipEntry(base + File.separator));
}
for (int i = 0; i < fl.length; i++) {
//递归遍历子文件夹
zip(out, fl[i], base + File.separator + fl[i].getName(), bo);
}
}
else {
out.putNextEntry(new ZipEntry(base)); //创建zip压缩进入点base
bo.write(FileOperate.readAllBytes(f));
bo.flush();//此处重要,如没有该句,可能会造成文件不完整。
}
now++;
System.out.println(now*100/filesCount);
}
/**
* 获取压缩总数量
* @param folderPath
* @return
*/
int getCount(String path)
{
int count = 0;
File file = new File(path);
if(file.isFile()){
count = 1;
}
else if(file.isDirectory())
{
File[] files = file.listFiles();
if(files != null){
for(File f : files){
count +=1;
if(f.isDirectory()){
count += getCount(f.getAbsolutePath());
}
}
}
}
return count;
}
/**
*获得解压缩总数量
*/
public int getZipCount(String zipFilePath)
{
int zipFileCount = 0;
try{
Zin = new ZipInputStream(new FileInputStream(zipFilePath));
while (Zin.getNextEntry()!= null){
zipFileCount++;
}
return zipFileCount;
}catch(IOException e){
e.printStackTrace();
return -1;
}
}
/**
* 解压缩文件
* @param zipFilePath zip源文件
* @param targetFolder 解压到什么位置
* @throws FileNotFoundException
*/
public void unzip(String zipFilePath,String targetFolderPath) throws Exception
{
File fsrc = new File(zipFilePath);
File ftg = new File(targetFolderPath);
if(!fsrc.exists() || !ftg.exists()){
throw new Exception("Zip::unzip 路径不存在");
}
filesCount = getZipCount(zipFilePath);
now = 0;
Zin = new ZipInputStream(new FileInputStream(zipFilePath));
BufferedInputStream Bin = new BufferedInputStream(Zin);
File Fout = null;
ZipEntry entry;
while((entry = Zin.getNextEntry())!= null){
if(!entry.isDirectory()){
Fout = new File(targetFolderPath,entry.getName());
if(!Fout.exists()){
(new File(Fout.getParent())).mkdirs();
}
try{
FileOutputStream out = new FileOutputStream(Fout);
BufferedOutputStream Bout=new BufferedOutputStream(out);
int b;
while((b=Bin.read())!=-1){
Bout.write(b);
}
Bout.close();
out.close();
}
catch(Exception ex){
}
}
now++;
System.out.println(now*100/filesCount);
}
Bin.close();
Zin.close();
}
}