Java 实现解压缩文件

http://blog.chinaunix.net/uid-21209537-id-3050665.html

 

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. try{
  4. //create output directory is not exists
  5. File folder = new File(OUTPUT_FOLDER);
  6. if(!folder.exists()){
  7.                 folder.mkdir();
  8. }
  9. //get the zip file content
  10. ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
  11. //get the zipped file list entry
  12. ZipEntry ze = zis.getNextEntry();
  13. while(ze!=null){
  14. String fileName = ze.getName();
  15. File newFile = new File(outputFolder + File.separator +fileName);
  16. System.out.println("file unzip: "+ newFile.getAbsoluteFile());
  17. //create all non exists folders
  18. //else you will hit FileNotFoundException for compressed folder
  19. new File(newFile.getParent()).mkdirs();
  20. FileOutputStream fos = new FileOutputStream(newFile);
  21. int len;
  22. while((len = zis.read(buffer)) > 0){
  23.                     fos.write(buffer,0,len);
  24. }
  25.                 fos.close();
  26.                 ze = zis.getNextEntry();
  27. }
  28.             zis.closeEntry();
  29.             zis.close();
  30. System.out.println("Extract completed.");
  31. }catch(IOException e){
  32.             e.printStackTrace();
  33. }
  34. }

压缩解压完整实例:

  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. private static final String INPUT_ZIP_FILE = "C:\\Logs.zip";
  16. private static final String OUTPUT_FOLDER = "C:\\outputzip";
  17.     AppZip(){
  18.         fileList =new ArrayList<String>();
  19. }
  20. public static void main(String[] args){
  21.         AppZip appzip = new AppZip();
  22.         appzip.generateFileList(new File(SOURCE_FOLDER));
  23.         appzip.zipIt(OUTPUT_ZIP_FILE);
  24.         appzip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
  25. }
  26. public void unZipIt(String zipFile, String outputFolder){
  27. byte[] buffer = new byte[1024];
  28. try{
  29. //create output directory is not exists
  30. File folder = new File(OUTPUT_FOLDER);
  31. if(!folder.exists()){
  32.                 folder.mkdir();
  33. }
  34. //get the zip file content
  35. ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
  36. //get the zipped file list entry
  37. ZipEntry ze = zis.getNextEntry();
  38. while(ze!=null){
  39. String fileName = ze.getName();
  40. File newFile = new File(outputFolder + File.separator +fileName);
  41. System.out.println("file unzip: "+ newFile.getAbsoluteFile());
  42. //create all non exists folders
  43. //else you will hit FileNotFoundException for compressed folder
  44. new File(newFile.getParent()).mkdirs();
  45. FileOutputStream fos = new FileOutputStream(newFile);
  46. int len;
  47. while((len = zis.read(buffer)) > 0){
  48.                     fos.write(buffer,0,len);
  49. }
  50.                 fos.close();
  51.                 ze = zis.getNextEntry();
  52. }
  53.             zis.closeEntry();
  54.             zis.close();
  55. System.out.println("Extract completed.");
  56. }catch(IOException e){
  57.             e.printStackTrace();
  58. }
  59. }
  60. public void zipIt(String zipFile) {
  61. byte[] buffer = new byte[1024];
  62. try{
  63. FileOutputStream fos = new FileOutputStream(zipFile);
  64. ZipOutputStream zos = new ZipOutputStream(fos);
  65. System.out.println("Output to Zip: "+ zipFile);
  66. for(String filename :this.fileList){
  67. System.out.println("File added: "+filename);
  68. ZipEntry ze = new ZipEntry(filename);
  69.                 zos.putNextEntry(ze);
  70. FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + filename);
  71. int len;
  72. while((len = in.read(buffer)) > 0){
  73.                     zos.write(buffer,0,len);
  74. }
  75. in.close();
  76. }
  77.             zos.closeEntry();
  78.             zos.close();
  79. System.out.println("Compress Completed.");
  80. }catch(IOException e){
  81.             e.printStackTrace();
  82. }
  83. }
  84. /**
  85.      * Traverse a directory and get all files,
  86.      * and add the file into fileList
  87.      * @param node file or directory
  88.      */
  89. public void generateFileList(File node) {
  90. //add file only
  91. if(node.isFile()){
  92.             fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
  93. }
  94. if(node.isDirectory()){
  95. String[] subNote = node.list();
  96. for(String filename : subNote){
  97.                 generateFileList(new File(node, filename));
  98. }
  99. }
  100. }
  101. /**
  102.      * Format the file path for zip
  103.      * @param file file path
  104.      * @return Formatted file path
  105.      */
  106. public String generateZipEntry(String filename) {
  107. return filename.substring(SOURCE_FOLDER.length()+1, filename.length());
  108. }
  109. }
posted @ 2013-04-19 17:32  IamThat  阅读(454)  评论(0编辑  收藏  举报