• zip文件解压或压缩


    1. <span style="font-size:18px;">/** 
    2.  * lsz 
    3.  */  
    4. public final class ZipUtil {  
    5.   
    6.     /** 
    7.      * 解压zip文件 
    8.      * @param unZipfile 
    9.      * @param destFile 
    10.      */  
    11.     public static void unZip(String unZipfile, String destFile) {  
    12.         FileOutputStream fileOut;  
    13.         File file;  
    14.         InputStream inputStream;  
    15.         byte[]   buf = new byte[1024*4];  
    16.         try {  
    17.             //生成一个zip的文件  
    18.             ZipFile   zipFile = new ZipFile(unZipfile, "GBK");  
    19.             //遍历zipFile中所有的实体,并把他们解压出来  
    20.             for (@SuppressWarnings("unchecked")  
    21.             Enumeration<ZipEntry> entries = zipFile.getEntries(); entries  
    22.                     .hasMoreElements();) {  
    23.                 ZipEntry entry =  entries.nextElement();  
    24.                 //生成他们解压后的一个文件    
    25.                 file = new File(destFile+File.separator+entry.getName());  
    26.     
    27.                 if (entry.isDirectory()) {  
    28.                     file.mkdirs();  
    29.                 } else {  
    30.                     // 如果指定文件的目录不存在,则创建之.  
    31.                     File parent = file.getParentFile();  
    32.                     if (!parent.exists()) {  
    33.                         parent.mkdirs();  
    34.                     }    
    35.                     //获取出该压缩实体的输入流   
    36.                     inputStream = zipFile.getInputStream(entry);  
    37.     
    38.                     fileOut = new FileOutputStream(file);  
    39.                     int length = 0;  
    40.                     //将实体写到本地文件中去  
    41.                     while ((length = inputStream.read(buf)) > 0) {  
    42.                         fileOut.write(buf, 0, length);  
    43.                     }  
    44.                     fileOut.close();  
    45.                     inputStream.close();  
    46.                 }  
    47.             }  
    48.             zipFile.close();  
    49.             //解压完后将原压缩文件删除  
    50.             File zipfile = new File(unZipfile);  
    51.             if(zipfile.exists()){  
    52.                 zipfile.delete();  
    53.             }     
    54.         } catch (IOException ioe) {  
    55.             ioe.printStackTrace();  
    56.         }  
    57.     }  
    58.       
    59.   
    60.        
    61.     /** 
    62.      * 一个文件夹压缩 
    63.      * 压缩文件夹 
    64.      * @param filepath 
    65.      * @param savepath 
    66.      * @throws Exception 
    67.      */  
    68.       public static void toZip(String filepath,String savepath) throws Exception{  
    69.           File file = new File(filepath);  
    70.           if(file.exists()){  
    71.               //判断导出路径是否为空,如果为空,则将压缩包生成到当前路径下  
    72.               if(StringUtils.isBlank(savepath)){  
    73.                   savepath = filepath+".zip";  
    74.               }else{  
    75.                   savepath = savepath+".zip";  
    76.               }  
    77.               ZipOutputStream outPut = new ZipOutputStream(new FileOutputStream(new File(savepath)));    
    78.               outPut.setEncoding("GBK");//设置编码    
    79.               createZip(outPut,file.listFiles(),null);   
    80.               outPut.flush();    
    81.               outPut.close();  
    82.           }else{  
    83.               //not found  
    84.               throw new RuntimeException("Err :not found file exception:"+filepath);    
    85.           }       
    86.       }  
    87.         
    88.       private static void createZip(org.apache.tools.zip.ZipOutputStream outPut,File[] listFiles,String fuPath) throws Exception {    
    89.             for(File f : listFiles){    
    90.                 String name = fuPath==null?f.getName():fuPath+"/"+f.getName();;    
    91.                 if(f.isDirectory()){    
    92.                     outPut.putNextEntry(new ZipEntry(name+"/"));    
    93.                     createZip(outPut,f.listFiles(),name);    
    94.                 }else{    
    95.                     outPut.putNextEntry(new ZipEntry(name));    
    96.                     InputStream is = new FileInputStream(f);    
    97.                     byte[] bys = new byte[1024];    
    98.                     int len = 0;    
    99.                     while((len = is.read(bys))!=-1)    
    100.                         outPut.write(bys, 0, len);    
    101.                     is.close();    
    102.                     outPut.flush();    
    103.                 }    
    104.             }    
    105.         }   
    106.         
    107.       /* 
    108.        * 复制文件 只能使复制文件,不能复制文件夹 
    109.        */  
    110.       public static void fileChannelCopy(File fromfile, File tofile) {  
    111.             FileInputStream fi = null;  
    112.             FileOutputStream fo = null;  
    113.             FileChannel in = null;  
    114.             FileChannel out = null;  
    115.             try {  
    116.                 fi = new FileInputStream(fromfile);  
    117.                 fo = new FileOutputStream(tofile);  
    118.                 in = fi.getChannel();//得到对应的文件通道  
    119.                 out = fo.getChannel();//得到对应的文件通道  
    120.                 in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道  
    121.             } catch (IOException e) {  
    122.                 e.printStackTrace();  
    123.             } finally {  
    124.                 try {  
    125.                     fi.close();  
    126.                     in.close();  
    127.                     fo.close();  
    128.                     out.close();  
    129.                 } catch (IOException e) {  
    130.                     e.printStackTrace();  
    131.                 }  
    132.             }  
    133.         }  
    134. }</span> 
  • 相关阅读:
    Oracle使用sys登录时报错ORA-28009解决方法
    oem的使用
    isqlplus的使用
    oracle客户端中文乱码问题的解决
    十:jinja2模板查找路径
    九:flask-response响应
    八:flask-重定向示例
    七:flask-一些小细节
    六:flask-自定义URL转换器
    五:flask-url_for使用详解
  • 原文地址:https://www.cnblogs.com/challengeof/p/4281840.html
Copyright © 2020-2023  润新知