码迷,mamicode.com
首页 > 其他好文 > 详细

zip文件解压或压缩

时间:2015-02-09 17:49:12      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:

  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> 

zip文件解压或压缩

标签:

原文地址:http://www.cnblogs.com/challengeof/p/4281840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!