• java压缩文件


    利用org.apche.tools.zip.ZipOutputStream可以很好的将文件压缩保存,下面的例子是利用递归的方法将文件压缩的例子

    1. import java.io.*;   
    2. import org.apache.tools.zip.ZipEntry;  
    3. import org.apache.tools.zip.ZipOutputStream;  
    4.   将夜www.jiangyea.com
    5. /** 
    6.  * 提供文件压缩的常用方法 
    7.  */  
    8. public class ZipUtil {  
    9.     /** 
    10.      *  
    11.      * @param src 源文件或者目录 
    12.      * @param dest 压缩文件的目标路径 
    13.      */  
    14.     public static void zip(String src,String dest){  
    15.         ZipOutputStream out = null;  
    16.         try {  
    17.             File outFile = new File(dest);  
    18.             out = new ZipOutputStream(outFile);  
    19.             File fileDirectory = new File(src);  
    20.               
    21.             if(fileDirectory.isFile()){  
    22.                 zipFileOrDirectory(out,fileDirectory,"");  
    23.             }else{  
    24.                 File [] entries = fileDirectory.listFiles();  
    25.                 zipFileOrDirectory(out, fileDirectory, "");  
    26.             }  
    27.         } catch (Exception e) {  
    28.             // TODO Auto-generated catch block  
    29.             e.printStackTrace();  
    30.         }finally{  
    31.             if(out != null){  
    32.                 try {  
    33.                     out.close();  
    34.                 } catch (IOException e) {  
    35.                     // TODO Auto-generated catch block  
    36.                     e.printStackTrace();  
    37.                 }  
    38.             }  
    39.         }  
    40.     }  
    41.       
    42.     private static void zipFileOrDirectory(ZipOutputStream out,File fileOrDirectory,String curPath){  
    43.         FileInputStream in = null;  
    44.         try {  
    45.             if(!fileOrDirectory.isDirectory()){//判断当前不是一个目录级  
    46.                 byte[] buffer = new byte[4096];  
    47.                 int bytes_read;  
    48.                 in = new FileInputStream(fileOrDirectory);  
    49.                   
    50.                 ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName());  
    51.                 out.putNextEntry(entry);  
    52.                   
    53.                 while((bytes_read = in.read(buffer)) != -1){  
    54.                     //buffer=要写入的数据,0=偏移量,bytes_read=要写入的字节长度  
    55.                     out.write(buffer, 0 ,bytes_read);  
    56.                 }  
    57.                 out.closeEntry();  
    58.             }else{  
    59.                 File[] entries = fileOrDirectory.listFiles();  
    60.                 for(int i = 0;i < entries.length;i++){  
    61.                     zipFileOrDirectory(out, entries[i], curPath + fileOrDirectory.getName() + "/");//递归分解目录中的文件  
    62.                 }  
    63.             }  
    64.         } catch (Exception e) {  
    65.             // TODO Auto-generated catch block  
    66.             e.printStackTrace();  
    67.         }finally{  
    68.             if(in != null){  
    69.                 try {  
    70.                     in.close();  
    71.                 } catch (IOException e) {  
    72.                     // TODO Auto-generated catch block  
    73.                     e.printStackTrace();  
    74.                 }  
    75.             }  
    76.         }  
    77.     }  
    78. }  
  • 相关阅读:
    poj1703--Find them, Catch them
    poj2828--Buy Tickets
    POJ 2594 Treasure Exploration(Floyd+最小路径覆盖)
    HDU
    二分图的一些性质
    HDU
    POJ 1659 Frogs' Neighborhood (Havel定理构造图)
    HDU
    HDU
    2018 Multi-University Training Contest 1
  • 原文地址:https://www.cnblogs.com/jiangye/p/3319156.html
Copyright © 2020-2023  润新知