java将文件打包成ZIP压缩文件的工具类实例

来源:互联网 发布:安居客盗图软件 编辑:程序博客网 时间:2024/05/21 19:26
01.package com.lanp;  02.  03.import java.io.BufferedInputStream;  04.import java.io.BufferedOutputStream;  05.import java.io.File;  06.import java.io.FileInputStream;  07.import java.io.FileNotFoundException;  08.import java.io.FileOutputStream;  09.import java.io.IOException;  10.import java.util.zip.ZipEntry;  11.import java.util.zip.ZipOutputStream;  12.  13./** 14. * 将文件打包成ZIP压缩文件 15. * @author LanP 16. * @since 2012-3-1 15:47 17. */  18.public final class FileToZip {  19.      20.    private FileToZip() {  21.          22.    }  23.      24.    /** 25.     * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的ZIP文件,并存放到zipFilePath。 26.     * @param sourceFilePath 待压缩的文件路径 27.     * @param zipFilePath    压缩后存放路径 28.     * @param fileName       压缩后文件的名称 29.     * @return flag 30.     */  31.    public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName) {  32.        boolean flag = false;  33.        File sourceFile = new File(sourceFilePath);  34.        FileInputStream fis = null;  35.        BufferedInputStream bis = null;  36.        FileOutputStream fos = null;  37.        ZipOutputStream zos = null;  38.          39.        if(sourceFile.exists() == false) {  40.            System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 不存在. <<<<<<");  41.        } else {  42.            try {  43.                File zipFile = new File(zipFilePath + "/" + fileName + ".zip");  44.                if(zipFile.exists()) {  45.                    System.out.println(">>>>>> " + zipFilePath + " 目录下存在名字为:" + fileName + ".zip" + " 打包文件. <<<<<<");  46.                } else {  47.                    File[] sourceFiles = sourceFile.listFiles();  48.                    if(null == sourceFiles || sourceFiles.length < 1) {  49.                        System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 里面不存在文件,无需压缩. <<<<<<");  50.                    } else {  51.                        fos = new FileOutputStream(zipFile);  52.                        zos = new ZipOutputStream(new BufferedOutputStream(fos));  53.                        byte[] bufs = new byte[1024*10];  54.                        for(int i=0;i<sourceFiles.length;i++) {  55.                            // 创建ZIP实体,并添加进压缩包  56.                            ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());  57.                            zos.putNextEntry(zipEntry);  58.                            // 读取待压缩的文件并写进压缩包里  59.                            fis = new FileInputStream(sourceFiles[i]);  60.                            bis = new BufferedInputStream(fis,1024*10);  61.                            int read = 0;  62.                            while((read=bis.read(bufs, 0, 1024*10)) != -1) {  63.                                zos.write(bufs, 0, read);  64.                            }  65.                        }  66.                        flag = true;  67.                    }  68.                }  69.            } catch (FileNotFoundException e) {  70.                e.printStackTrace();  71.                throw new RuntimeException(e);  72.            } catch (IOException e) {  73.                e.printStackTrace();  74.                throw new RuntimeException(e);  75.            } finally {  76.                // 关闭流  77.                try {  78.                    if(null != bis) bis.close();  79.                    if(null != zos) zos.close();  80.                } catch (IOException e) {  81.                    e.printStackTrace();  82.                    throw new RuntimeException(e);  83.                }  84.            }  85.        }  86.          87.        return flag;  88.    }  89.      90.    /** 91.     * 将文件打包成ZIP压缩文件,main方法测试 92.     * @param args 93.     */  94.    public static void main(String[] args) {  95.        String sourceFilePath = "C:\\home\\lp20120301";  96.        String zipFilePath = "C:\\home";  97.        String fileName = "lp20120301";  98.        boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);  99.        if(flag) {  100.            System.out.println(">>>>>> 文件打包成功. <<<<<<");  101.        } else {  102.            System.out.println(">>>>>> 文件打包失败. <<<<<<");  103.        }  104.    }  105.}  

0 0
原创粉丝点击