首页 > 技术文章 > Java解压缩zip文件

smilesmile 2014-07-11 08:49 原文

下面实现的功能是zip文件中的图像文件解压到当前目录下,用jdk自带的处理zip文件的代码处理的,但是不能处理中文名称的文件,要不然就会出错。

 

Java代码  收藏代码
  1. import java.io.BufferedInputStream;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.util.zip.ZipEntry;  
  7. import java.util.zip.ZipInputStream;  
  8.   
  9. /** 
  10.  * 不能处理中文文件名 
  11.  */  
  12. public class UnZip   
  13. {  
  14.     private static final int buffer = 2048;   
  15.       
  16.     public static void main(String[] args)   
  17.     {  
  18.         unZip("D:\\ss\\test.zip");  
  19.     }  
  20.   
  21.     public static void unZip(String path)   
  22.     {  
  23.         int count = -1;  
  24.         int index = -1;  
  25.           
  26.         String savepath = "";  
  27.         boolean flag = false;  
  28.           
  29.         savepath = path.substring(0, path.lastIndexOf("\\")) + "\\";  
  30.   
  31.         try   
  32.         {  
  33.             BufferedOutputStream bos = null;  
  34.             ZipEntry entry = null;  
  35.             FileInputStream fis = new FileInputStream(path);   
  36.             ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));  
  37.               
  38.             while((entry = zis.getNextEntry()) != null)   
  39.             {  
  40.                 byte data[] = new byte[buffer];   
  41.   
  42.                 String temp = entry.getName();  
  43.                   
  44.                 flag = isPics(temp);  
  45.                 if(!flag)  
  46.                     continue;  
  47.                   
  48.                 index = temp.lastIndexOf("/");  
  49.                 if(index > -1)  
  50.                     temp = temp.substring(index+1);  
  51.                 temp = savepath + temp;  
  52.                   
  53.                 File f = new File(temp);  
  54.                 f.createNewFile();  
  55.   
  56.                 FileOutputStream fos = new FileOutputStream(f);  
  57.                 bos = new BufferedOutputStream(fos, buffer);  
  58.                   
  59.                 while((count = zis.read(data, 0, buffer)) != -1)   
  60.                 {  
  61.                     bos.write(data, 0, count);  
  62.                 }  
  63.   
  64.                 bos.flush();  
  65.                 bos.close();  
  66.             }  
  67.   
  68.             zis.close();  
  69.   
  70.         } catch (Exception e) {  
  71.             e.printStackTrace();  
  72.         }  
  73.     }  
  74.       
  75.     public static boolean isPics(String filename)  
  76.     {  
  77.         boolean flag = false;  
  78.           
  79.         if(filename.endsWith(".jpg") || filename.endsWith(".gif")  || filename.endsWith(".bmp") || filename.endsWith(".png"))  
  80.             flag = true;  
  81.           
  82.         return flag;  
  83.     }  
  84. }  

 

下面是用的apache的zip文件处理包进行处理的,可以处理中文名称的文件,功能跟上面的一样。

 

Java代码  收藏代码
  1. import java.io.BufferedOutputStream;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.util.Enumeration;  
  7.   
  8. import org.apache.tools.zip.ZipEntry;  
  9. import org.apache.tools.zip.ZipFile;  
  10.   
  11. /** 
  12.  * 可以处理中文文件名 
  13.  */  
  14. public class UnZip2   
  15. {  
  16.     private static final int buffer = 2048;  
  17.       
  18.     public static void main(String[] args)  
  19.     {  
  20.         unZip("D:\\ss\\test.zip");  
  21.     }  
  22.       
  23.     public static void unZip(String path)  
  24.     {  
  25.         int count = -1;  
  26.         int index = -1;  
  27.         String savepath = "";  
  28.         boolean flag = false;  
  29.           
  30.         File file = null;   
  31.         InputStream is = null;    
  32.         FileOutputStream fos = null;    
  33.         BufferedOutputStream bos = null;  
  34.           
  35.         savepath = path.substring(0, path.lastIndexOf("\\")) + "\\";  
  36.   
  37.         try  
  38.         {   
  39.             ZipFile zipFile = new ZipFile(path);   
  40.   
  41.             Enumeration<?> entries = zipFile.getEntries();  
  42.               
  43.             while(entries.hasMoreElements())  
  44.             {   
  45.                 byte buf[] = new byte[buffer];   
  46.                   
  47.                 ZipEntry entry = (ZipEntry)entries.nextElement();   
  48.                   
  49.                 String filename = entry.getName();  
  50.                 index = filename.lastIndexOf("/");  
  51.                 if(index > -1)  
  52.                     filename = filename.substring(index+1);  
  53.                   
  54.                 filename = savepath + filename;  
  55.                   
  56.                 flag = isPics(filename);  
  57.                 if(!flag)  
  58.                     continue;  
  59.                   
  60.                 file = new File(filename);   
  61.                 file.createNewFile();  
  62.                   
  63.                 is = zipFile.getInputStream(entry);   
  64.                   
  65.                 fos = new FileOutputStream(file);   
  66.                 bos = new BufferedOutputStream(fos, buffer);  
  67.                   
  68.                 while((count = is.read(buf)) > -1)  
  69.                 {   
  70.                     bos.write(buf, 0, count );   
  71.                 }   
  72.                   
  73.                 fos.close();   
  74.   
  75.                 is.close();   
  76.             }   
  77.               
  78.             zipFile.close();   
  79.               
  80.         }catch(IOException ioe){   
  81.             ioe.printStackTrace();   
  82.         }   
  83.     }   
  84.   
  85.     public static boolean isPics(String filename)  
  86.     {  
  87.         boolean flag = false;  
  88.           
  89.         if(filename.endsWith(".jpg") || filename.endsWith(".gif")  || filename.endsWith(".bmp") || filename.endsWith(".png"))  
  90.             flag = true;  
  91.           
  92.         return flag;  
  93.     }  
  94. }  

 

 

推荐阅读