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

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-28 16:18:23

Java comes with “java.util.zip” library to implement the data compression in ZIp format. The overall concept is quite straightforward.

·Read file with “FileInputStream
·Add the file name to “ZipEntry” and output it to “ZipOutputStream

Simple ZIP example

Read a file “C:\\user.txt” and compress it into a zip file – “C:\\user.zip“.
  1. package org.hello.zip;

  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.zip.ZipEntry;
  6. import java.util.zip.ZipOutputStream;

  7. public class JavaZip {

  8.     public static void main(String[] args) {
  9.         byte[] buffer = new byte[1024];
  10.         try {
  11.             FileOutputStream fos = new FileOutputStream("C:\\user.zip");
  12.             ZipOutputStream zos = new ZipOutputStream(fos);
  13.             ZipEntry ze = new ZipEntry("user.txt");
  14.             zos.putNextEntry(ze);
  15.             FileInputStream in = new FileInputStream("C:\\user.txt");
  16.             /*
  17.              * int java.io.FileInputStream.read(byte[] b) throws IOException
  18.              * Reads up to b.length bytes of data from this input stream into an
  19.              * array of bytes. This method blocks until some input is available.
  20.              * Overrides: read(...) in InputStream Parameters: b the buffer into
  21.              * which the data is read. Returns: the total number of bytes read
  22.              * into the buffer, or -1 if there is no more data because the end
  23.              * of the file has been reached.
  24.              */
  25.             int len;
  26.             while ((len = in.read(buffer)) > 0) {
  27.                 zos.write(buffer, 0, len);
  28.             }
  29.             in.close();
  30.             zos.closeEntry();
  31.             zos.close();

  32.             System.out.println("Compress Completed!");

  33.         } catch (IOException e) {
  34.             e.printStackTrace();
  35.         }
  36.     }

  37. }
Advance ZIP example – Recursively

Read all files from folder “C:\\Logs” and compress it into a zip file – “C:\\Logs.zip“. It will recursively zip a directory as well.

  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.ZipOutputStream;

  10. public class AppZip {

  11.     List<String> fileList;
  12.     private static final String OUTPUT_ZIP_FILE = "C:\\Logs.zip";
  13.     private static final String SOURCE_FOLDER = "C:\\Logs";
  14.     
  15.     AppZip(){
  16.         fileList =new ArrayList<String>();
  17.     }
  18.     
  19.     public static void main(String[] args){
  20.         AppZip appzip = new AppZip();
  21.         appzip.generateFileList(new File(SOURCE_FOLDER));
  22.         appzip.zipIt(OUTPUT_ZIP_FILE);
  23.     }

  24.     private void zipIt(String zipFile) {
  25.         byte[] buffer = new byte[1024];
  26.         try{
  27.             FileOutputStream fos = new FileOutputStream(zipFile);
  28.             ZipOutputStream zos = new ZipOutputStream(fos);
  29.             System.out.println("Output to Zip: "+ zipFile);
  30.             for(String filename :this.fileList){
  31.                 System.out.println("File added: "+filename);
  32.                 ZipEntry ze = new ZipEntry(filename);
  33.                 zos.putNextEntry(ze);
  34.                 
  35.                 FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + filename);
  36.                 
  37.                 int len;
  38.                 while((len = in.read(buffer)) > 0){
  39.                     zos.write(buffer,0,len);
  40.                 }
  41.                 in.close();
  42.             }
  43.             zos.closeEntry();
  44.             zos.close();
  45.             System.out.println("Compress Completed.");
  46.             
  47.         }catch(IOException e){
  48.             e.printStackTrace();
  49.         }
  50.         
  51.     }

  52.     /**
  53.      * Traverse a directory and get all files,
  54.      * and add the file into fileList
  55.      * @param node file or directory
  56.      */
  57.     private void generateFileList(File node) {
  58.         //add file only
  59.         if(node.isFile()){
  60.             fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
  61.         }
  62.     
  63.         if(node.isDirectory()){
  64.             String[] subNote = node.list();
  65.             for(String filename : subNote){
  66.                 generateFileList(new File(node, filename));
  67.             }
  68.         }
  69.     
  70.     }

  71.      /**
  72.      * Format the file path for zip
  73.      * @param file file path
  74.      * @return Formatted file path
  75.      */
  76.     private String generateZipEntry(String filename) {
  77.         return filename.substring(SOURCE_FOLDER.length()+1, filename.length());
  78.     }
  79. }


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