/*** @Description 获取文件夹中所有文件路径
*@authorliuy-8
* @date 2015年5月27日 下午5:25:24
*@paramfolderPath 文件夹路径
*@return文件夹中所有文件路径*/
public static ListgetFilesPath(String folderPath){
List filesPath = new ArrayList();
File folder= newFile(folderPath);
File[] fileList=folder.listFiles();for(File file : fileList){if(file.isDirectory()){//是文件夹,递归遍历
filesPath.addAll(getFilesPath(file.getPath()));
}else{//是文件,加入文件列表
filesPath.add(file.getPath());
}
}returnfilesPath;
}/*** @Description 压缩文件夹
*@authorliuy-8
* @date 2015年5月26日 下午1:42:00
*@paramfolderPath 文件夹路径
*@paramzipFilePath 压缩文件路径*/
public static voidfolder2Zip(String folderPath, String zipFilePath){//获取当前系统文件分隔符
String fileSeparator = System.getProperty("file.separator");//若传入路径最后没有文件分隔符,加上
if(folderPath.lastIndexOf(fileSeparator) != (folderPath.length() - 1)){
folderPath= folderPath +fileSeparator;
}//获取文件夹下所有文件路径
List filesPath =getFilesPath(folderPath);//文件输出流
FileOutputStream fos = null;//缓冲输出流
BufferedOutputStream bos = null;//zip输出流
ZipOutputStream zos = null;try{
fos= newFileOutputStream(zipFilePath);
bos= newBufferedOutputStream(fos);
zos= newZipOutputStream(bos);for(String filePath : filesPath){//将文件写入zip输出流
writeFile2Zip(folderPath, filePath, zos);
}
zos.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{//关闭zip输出流
try{if(null !=zos){
zos.close();
}
}catch(IOException e) {
e.printStackTrace();
}//关闭缓冲输出流
try{if(null !=bos){
bos.close();
}
}catch(IOException e) {
e.printStackTrace();
}//关闭文件输出流
try{if(null !=fos){
fos.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}