java 文件包_java原生文件打包

/*** @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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值