工具类 打包文件成压缩包文件

package com.myFirstSpring.test; 
/** 
 * @author tqf
 * @version 创建时间:2019-9-25 下午1:50:02 
 * 类说明:文件打包成压缩包
 */
 
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.Date;
import java.util.zip.ZipEntry;  
import java.util.zip.ZipOutputStream;  
 
import com.cloopen.rest.sdk.utils.DateUtil;
 
public class ZipUtil {  
  
    public static void main(String[] args){  
        String sourceFilePath = "D://Qrcoe/";  
        String zipFilePath = "D://Qrcoe/";  
        String fileName =  DateUtil.dateToStr(new Date(),"yyyyMMddHHmmss");
        String fn = ZipUtil.fileToZip(sourceFilePath, zipFilePath, fileName);  
        System.out.println(fn);
    }  
    
    /** 
     * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下 
     * @param sourceFilePath :待压缩的文件路径 
     * @param zipFilePath :压缩后存放路径 
     * @param fileName :压缩后文件的名称 
     * @return 
     */  
    public static String fileToZip(String sourceFilePath,String zipFilePath,String fileName){  
        File sourceFile = new File(sourceFilePath);  
        FileInputStream fis = null;  
        BufferedInputStream bis = null;  
        FileOutputStream fos = null;  
        ZipOutputStream zos = null;  
        //判断源文件是否存在  
        if(!sourceFile.exists()){  
            System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.");  
        }else{  
            try { 
                File zipFile = new File(zipFilePath + "/" + fileName +".rar");  
                //判断压缩后文件是否会重复
                if(zipFile.exists()){ 
                    System.out.println(zipFilePath + "目录下已存在名字为" + fileName +".rar" +"文件");  
                }else{  
                    
                    //每次生成压缩包之前先删除原有的压缩包
                    zipFile.delete();
                    //获取源文件夹下的所有文件
                    File[] sourceFiles = sourceFile.listFiles();  
                    if(null == sourceFiles || sourceFiles.length<1){  
                        System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");  
                    }else{  
                        //先删除之前的压缩包文件
                        for(int i=0; i<sourceFiles.length; i++){  
                           if(sourceFiles[i].getName().indexOf(".rar")!=-1){
                               File zipFile2 = new File(sourceFiles[i].getPath());
                               zipFile2.delete();
                               break;
                           }
                        } 
                        //重新获取文件夹所以文件
                        sourceFiles = sourceFile.listFiles();
                        fos = new FileOutputStream(zipFile);  
                        zos = new ZipOutputStream(new BufferedOutputStream(fos));  
                        byte[] bufs = new byte[1024*10];  
                        for(int i=0; i<sourceFiles.length; i++){  
                            //创建ZIP实体,并添加进压缩包  
                            ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());  
                            zos.putNextEntry(zipEntry);  
                            //读取待压缩的文件并写进压缩包里  
                            fis = new FileInputStream(sourceFiles[i]);  
                            bis = new BufferedInputStream(fis, 1024*10);  
                            int read = 0;  
                            while((read=bis.read(bufs, 0, 1024*10)) != -1){  
                                zos.write(bufs,0,read);  
                            }  
                        }  
                    }  
                }  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
                throw new RuntimeException(e);  
            } catch (IOException e) {  
                e.printStackTrace();  
                throw new RuntimeException(e);  
            } finally{  
                //关闭流  
                try {  
                    if(null != bis) bis.close();  
                    if(null != zos) zos.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                    throw new RuntimeException(e);  
                }  
            }  
        }  
        return fileName + ".rar";  
    }
      
    
      
}

posted @   码奴生来只知道前进~  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示