zoukankan      html  css  js  c++  java
  • java将文件打包成ZIP压缩文件的工具类实例

    http://blog.csdn.net/lanpiao_87/article/details/7310461
    1. package com.lanp;  
    2.   
    3. import java.io.BufferedInputStream;  
    4. import java.io.BufferedOutputStream;  
    5. import java.io.File;  
    6. import java.io.FileInputStream;  
    7. import java.io.FileNotFoundException;  
    8. import java.io.FileOutputStream;  
    9. 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. }  
  • 相关阅读:
    使用BigQuery分析GitHub上的C#代码
    ASP.NET Core 处理 404 Not Found
    C# 7 局部函数剖析
    调试 ASP.NET Core 2.0 源代码
    Entity Framework Core Like 查询揭秘
    ASP.NET Core Razor 视图组件
    Thread 1 cannot allocate new log引起的宕机事故(转载)
    docker默认网段和主机网段冲突解决
    docker安装异常以及网络问题总结
    max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
  • 原文地址:https://www.cnblogs.com/zhwl/p/4944937.html
Copyright © 2011-2022 走看看