Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2531093
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-29 09:48:02

In this article you will learn how to unzip file.

   1. Read ZIP file with “ZipInputStream
   2. Get the files to “ZipEntry” and output it to “FileOutputStream

Decompress ZIP file example


In this example, it will read a ZIP file from zipFile, and decompress all zipped files to OUTPUT_FOLDER folder.
  1. public void unZipIt(String zipFile, String outputFolder){
  2.         byte[] buffer = new byte[1024];
  3.         
  4.         try{
  5.             //create output directory is not exists
  6.             File folder = new File(OUTPUT_FOLDER);
  7.             if(!folder.exists()){
  8.                 folder.mkdir();
  9.             }
  10.             //get the zip file content
  11.             ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
  12.             //get the zipped file list entry
  13.             ZipEntry ze = zis.getNextEntry();
  14.             
  15.             while(ze!=null){
  16.                 String fileName = ze.getName();
  17.                 File newFile = new File(outputFolder + File.separator +fileName);
  18.                 System.out.println("file unzip: "+ newFile.getAbsoluteFile());
  19.                 
  20.                 //create all non exists folders
  21.                 //else you will hit FileNotFoundException for compressed folder
  22.                 new File(newFile.getParent()).mkdirs();
  23.                 
  24.                 FileOutputStream fos = new FileOutputStream(newFile);
  25.                 
  26.                 int len;
  27.                 while((len = zis.read(buffer)) > 0){
  28.                     fos.write(buffer,0,len);
  29.                 }
  30.                 fos.close();
  31.                 ze = zis.getNextEntry();
  32.             }
  33.             
  34.             zis.closeEntry();
  35.             zis.close();
  36.             System.out.println("Extract completed.");
  37.         }catch(IOException e){
  38.             e.printStackTrace();
  39.         }
  40.     }
压缩解压完整实例:

  1. package org.hello.zip;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipInputStream;
  10. import java.util.zip.ZipOutputStream;

  11. public class AppZip {

  12.     List<String> fileList;
  13.     private static final String OUTPUT_ZIP_FILE = "C:\\Logs.zip";
  14.     private static final String SOURCE_FOLDER = "C:\\Logs";
  15.     
  16.     private static final String INPUT_ZIP_FILE = "C:\\Logs.zip";
  17.     private static final String OUTPUT_FOLDER = "C:\\outputzip";
  18.     
  19.     AppZip(){
  20.         fileList =new ArrayList<String>();
  21.     }
  22.     
  23.     public static void main(String[] args){
  24.         AppZip appzip = new AppZip();
  25.         appzip.generateFileList(new File(SOURCE_FOLDER));
  26.         appzip.zipIt(OUTPUT_ZIP_FILE);
  27.         appzip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
  28.     }
  29.     
  30.     public void unZipIt(String zipFile, String outputFolder){
  31.         byte[] buffer = new byte[1024];
  32.         
  33.         try{
  34.             //create output directory is not exists
  35.             File folder = new File(OUTPUT_FOLDER);
  36.             if(!folder.exists()){
  37.                 folder.mkdir();
  38.             }
  39.             //get the zip file content
  40.             ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
  41.             //get the zipped file list entry
  42.             ZipEntry ze = zis.getNextEntry();
  43.             
  44.             while(ze!=null){
  45.                 String fileName = ze.getName();
  46.                 File newFile = new File(outputFolder + File.separator +fileName);
  47.                 System.out.println("file unzip: "+ newFile.getAbsoluteFile());
  48.                 
  49.                 //create all non exists folders
  50.                 //else you will hit FileNotFoundException for compressed folder
  51.                 new File(newFile.getParent()).mkdirs();
  52.                 
  53.                 FileOutputStream fos = new FileOutputStream(newFile);
  54.                 
  55.                 int len;
  56.                 while((len = zis.read(buffer)) > 0){
  57.                     fos.write(buffer,0,len);
  58.                 }
  59.                 fos.close();
  60.                 ze = zis.getNextEntry();
  61.             }
  62.             
  63.             zis.closeEntry();
  64.             zis.close();
  65.             System.out.println("Extract completed.");
  66.         }catch(IOException e){
  67.             e.printStackTrace();
  68.         }
  69.     }

  70.     public void zipIt(String zipFile) {
  71.         byte[] buffer = new byte[1024];
  72.         try{
  73.             FileOutputStream fos = new FileOutputStream(zipFile);
  74.             ZipOutputStream zos = new ZipOutputStream(fos);
  75.             System.out.println("Output to Zip: "+ zipFile);
  76.             for(String filename :this.fileList){
  77.                 System.out.println("File added: "+filename);
  78.                 ZipEntry ze = new ZipEntry(filename);
  79.                 zos.putNextEntry(ze);
  80.                 
  81.                 FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + filename);
  82.                 
  83.                 int len;
  84.                 while((len = in.read(buffer)) > 0){
  85.                     zos.write(buffer,0,len);
  86.                 }
  87.                 in.close();
  88.             }
  89.             zos.closeEntry();
  90.             zos.close();
  91.             System.out.println("Compress Completed.");
  92.             
  93.         }catch(IOException e){
  94.             e.printStackTrace();
  95.         }
  96.         
  97.     }

  98.     /**
  99.      * Traverse a directory and get all files,
  100.      * and add the file into fileList
  101.      * @param node file or directory
  102.      */
  103.     public void generateFileList(File node) {
  104.         //add file only
  105.         if(node.isFile()){
  106.             fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
  107.         }
  108.     
  109.         if(node.isDirectory()){
  110.             String[] subNote = node.list();
  111.             for(String filename : subNote){
  112.                 generateFileList(new File(node, filename));
  113.             }
  114.         }
  115.     
  116.     }

  117.      /**
  118.      * Format the file path for zip
  119.      * @param file file path
  120.      * @return Formatted file path
  121.      */
  122.     public String generateZipEntry(String filename) {
  123.         return filename.substring(SOURCE_FOLDER.length()+1, filename.length());
  124.     }
  125. }

阅读(2814) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~