压缩工具

public class ZipUtil {

    private static final Logger logger = Logger.getLogger(ZipUtil.class);

    /**
     * 功能:压缩多个文件成一个zip文件 
     * @param srcfile:源文件列表 
     * @param zipfile:压缩后的文件 
     */
    public static boolean zipFiles(File[] srcfile,File zipfile){
        boolean reFlag = false;
        byte[] buf=new byte[10240];
        ZipOutputStream out = null;
        FileInputStream in = null;
        try {
            //ZipOutputStream类:完成文件或文件夹的压缩  
            out=new ZipOutputStream(new FileOutputStream(zipfile));
            //注意此处编码设置
            out.setEncoding("gbk");
            for(int i=0;i0){
                    out.write(buf,0,len);
                }
                out.closeEntry();
                in.close();
                in = null;
            }
            out.close();
            out = null;
            reFlag = true;
            logger.info("压缩完成,文件详细信息为:"+zipfile.getAbsolutePath());
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new RuntimeException(e.getMessage());
        } finally{
            if(in!=null){
                try {
                    in.close();
                    in = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return reFlag;
    }

你可能感兴趣的:(压缩工具)