Java实现文件压缩 使用GZIP和Zip方式http://blog.csdn.net/kevin_luan/article/details/7903400

来源:互联网 发布:苹果软件视频桌面 编辑:程序博客网 时间:2024/05/17 03:32
[java] view plaincopy
  1. package com.lss.common.file.zip;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.util.zip.GZIPInputStream;  
  9. import java.util.zip.GZIPOutputStream;  
  10. /** 
  11.  * @description 压缩文件 
  12.  * @author SHOUSHEN LUAN 
  13.  * @DATE 2012-1-8下午03:10:01 
  14.  */  
  15. public class GZipUtils {  
  16.  public static void main(String[] args) throws IOException {  
  17.   zipFile("D:\\test\\kevin.mp4""D:\\test\\kevin.mp4.zip");  
  18.   unZipFile("D:\\test\\kevin.mp4.zip""D:\\test\\kevin.mp4.zip.mp4");  
  19.     
  20.  }  
  21.   
  22.  /** 
  23.   * Member cache 文件解压处理 
  24.   *  
  25.   * @param buf 
  26.   * @return 
  27.   * @throws IOException 
  28.   */  
  29.  public static byte[] unGzip(byte[] buf) throws IOException {  
  30.   GZIPInputStream gzi = null;  
  31.   ByteArrayOutputStream bos = null;  
  32.   try {  
  33.    gzi = new GZIPInputStream(new ByteArrayInputStream(buf));  
  34.    bos = new ByteArrayOutputStream(buf.length);  
  35.    int count = 0;  
  36.    byte[] tmp = new byte[2048];  
  37.    while ((count = gzi.read(tmp)) != -1) {  
  38.     bos.write(tmp, 0, count);  
  39.    }  
  40.    buf = bos.toByteArray();  
  41.   } finally {  
  42.    if (bos != null) {  
  43.     bos.flush();  
  44.     bos.close();  
  45.    }  
  46.    if (gzi != null)  
  47.     gzi.close();  
  48.   }  
  49.   return buf;  
  50.  }  
  51.   
  52.  /** 
  53.   * Member cache 文件压缩处理 
  54.   *  
  55.   * @param val 
  56.   * @return 
  57.   * @throws IOException 
  58.   */  
  59.  public static byte[] gzip(byte[] val) throws IOException {  
  60.   ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);  
  61.   GZIPOutputStream gos = null;  
  62.   try {  
  63.    gos = new GZIPOutputStream(bos);  
  64.    gos.write(val, 0, val.length);  
  65.    gos.finish();  
  66.    gos.flush();  
  67.    bos.flush();  
  68.    val = bos.toByteArray();  
  69.   } finally {  
  70.    if (gos != null)  
  71.     gos.close();  
  72.    if (bos != null)  
  73.     bos.close();  
  74.   }  
  75.   return val;  
  76.  }  
  77.   
  78.  /** 
  79.   * 对文件进行压缩 
  80.   *  
  81.   * @param source 
  82.   *            源文件 
  83.   * @param target 
  84.   *            目标文件 
  85.   * @throws IOException 
  86.   */  
  87.  public static void zipFile(String source, String target) throws IOException {  
  88.   FileInputStream fin = null;  
  89.   FileOutputStream fout = null;  
  90.   GZIPOutputStream gzout = null;  
  91.   try {  
  92.    fin = new FileInputStream(source);  
  93.    fout = new FileOutputStream(target);  
  94.    gzout = new GZIPOutputStream(fout);  
  95.    byte[] buf = new byte[1024];  
  96.    int num;  
  97.    while ((num = fin.read(buf)) != -1) {  
  98.     gzout.write(buf, 0, num);  
  99.    }  
  100.   } finally {  
  101.    if (gzout != null)  
  102.     gzout.close();  
  103.    if (fout != null)  
  104.     fout.close();  
  105.    if (fin != null)  
  106.     fin.close();  
  107.   }  
  108.  }  
  109.   
  110.  /** 
  111.   * 解压文件 
  112.   *  
  113.   * @param source源文件 
  114.   * @param target目标文件 
  115.   * @throws IOException 
  116.   */  
  117.  public static void unZipFile(String source, String target)  
  118.    throws IOException {  
  119.   FileInputStream fin = null;  
  120.   GZIPInputStream gzin = null;  
  121.   FileOutputStream fout = null;  
  122.   try {  
  123.    fin = new FileInputStream(source);  
  124.    gzin = new GZIPInputStream(fin);  
  125.    fout = new FileOutputStream(target);  
  126.    byte[] buf = new byte[1024];  
  127.    int num;  
  128.    while ((num = gzin.read(buf, 0, buf.length)) != -1) {  
  129.     fout.write(buf, 0, num);  
  130.    }  
  131.   } finally {  
  132.    if (fout != null)  
  133.     fout.close();  
  134.    if (gzin != null)  
  135.     gzin.close();  
  136.    if (fin != null)  
  137.     fin.close();  
  138.   }  
  139.  }  
  140. }  
  141.   
  142.    
  143.   
  144. package com.lss.common.file.zip;  
  145.   
  146. import java.io.BufferedInputStream;  
  147. import java.io.BufferedOutputStream;  
  148. import java.io.File;  
  149. import java.io.FileInputStream;  
  150. import java.io.FileOutputStream;  
  151. import java.io.InputStream;  
  152. import java.io.OutputStream;  
  153. import java.util.ArrayList;  
  154. import java.util.Enumeration;  
  155. import java.util.List;  
  156. import java.util.zip.ZipEntry;  
  157. import java.util.zip.ZipFile;  
  158. import java.util.zip.ZipOutputStream;  
  159.   
  160. @SuppressWarnings("unchecked")  
  161. public class ZipToFile {  
  162.   
  163.  public static final String ZIP_FILENAME = "D:\\test\\source.zip";// 需要解压缩的文件名  
  164.  public static final String ZIP_DIR = "D:\\test\\source\\";// 需要压缩的文件夹  
  165.  public static final String UN_ZIP_DIR = "D:\\test\\";// 要解压的文件目录  
  166.  public static final int BUFFER = 1024;// 缓存大小  
  167.   
  168.  public static void main(String[] args) {  
  169.   try {  
  170.    // zipFile(ZIP_DIR,ZIP_FILENAME);  
  171.    unZipFile();  
  172.   } catch (Exception e) {  
  173.    e.printStackTrace();  
  174.   }  
  175.  }  
  176.   
  177.  /** 
  178.   * zip压缩功能. 压缩baseDir(文件夹目录)下所有文件,包括子目录 
  179.   *  
  180.   * @throws Exception 
  181.   */  
  182.  public static void zipFile(String baseDir, String fileName)  
  183.    throws Exception {  
  184.   List fileList = getSubFiles(new File(baseDir));  
  185.   ZipOutputStream zos = new ZipOutputStream(  
  186.     new FileOutputStream(fileName));  
  187.   ZipEntry ze = null;  
  188.   byte[] buf = new byte[BUFFER];  
  189.   int readLen = 0;  
  190.   for (int i = 0; i < fileList.size(); i++) {  
  191.    File f = (File) fileList.get(i);  
  192.    ze = new ZipEntry(getAbsFileName(baseDir, f));  
  193.    ze.setSize(f.length());  
  194.    ze.setTime(f.lastModified());  
  195.    zos.putNextEntry(ze);  
  196.    InputStream is = new BufferedInputStream(new FileInputStream(f));  
  197.    while ((readLen = is.read(buf, 0, BUFFER)) != -1) {  
  198.     zos.write(buf, 0, readLen);  
  199.    }  
  200.    is.close();  
  201.   }  
  202.   zos.close();  
  203.  }  
  204.   
  205.  /** 
  206.   * 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径. 
  207.   *  
  208.   * @param baseDir 
  209.   *            java.lang.String 根目录 
  210.   * @param realFileName 
  211.   *            java.io.File 实际的文件名 
  212.   * @return 相对文件名 
  213.   */  
  214.  private static String getAbsFileName(String baseDir, File realFileName) {  
  215.   File real = realFileName;  
  216.   File base = new File(baseDir);  
  217.   String ret = real.getName();  
  218.   while (true) {  
  219.    real = real.getParentFile();  
  220.    if (real == null)  
  221.     break;  
  222.    if (real.equals(base))  
  223.     break;  
  224.    else  
  225.     ret = real.getName() + "/" + ret;  
  226.   }  
  227.   return ret;  
  228.  }  
  229.   
  230.  /** 
  231.   * 取得指定目录下的所有文件列表,包括子目录. 
  232.   *  
  233.   * @param baseDir 
  234.   *            File 指定的目录 
  235.   * @return 包含java.io.File的List 
  236.   */  
  237.  private static List getSubFiles(File baseDir) {  
  238.   List ret = new ArrayList();  
  239.   File[] tmp = baseDir.listFiles();  
  240.   for (int i = 0; i < tmp.length; i++) {  
  241.    if (tmp[i].isFile())  
  242.     ret.add(tmp[i]);  
  243.    if (tmp[i].isDirectory())  
  244.     ret.addAll(getSubFiles(tmp[i]));  
  245.   }  
  246.   return ret;  
  247.  }  
  248.   
  249.  /** 
  250.   * 解压缩功能. 将ZIP_FILENAME文件解压到ZIP_DIR目录下. 
  251.   *  
  252.   * @throws Exception 
  253.   */  
  254.  public static void unZipFile() throws Exception {  
  255.   ZipFile zfile = new ZipFile(ZIP_FILENAME);  
  256.   Enumeration zList = zfile.entries();  
  257.   ZipEntry ze = null;  
  258.   byte[] buf = new byte[1024];  
  259.   while (zList.hasMoreElements()) {  
  260.    ze = (ZipEntry) zList.nextElement();  
  261.    if (ze.isDirectory()) {  
  262.     File f = new File(ZIP_DIR + ze.getName());  
  263.     f.mkdir();  
  264.     continue;  
  265.    }  
  266.    OutputStream os = new BufferedOutputStream(new FileOutputStream(  
  267.      getRealFileName(ZIP_DIR, ze.getName())));  
  268.    InputStream is = new BufferedInputStream(zfile.getInputStream(ze));  
  269.    int readLen = 0;  
  270.    while ((readLen = is.read(buf, 01024)) != -1) {  
  271.     os.write(buf, 0, readLen);  
  272.    }  
  273.    is.close();  
  274.    os.close();  
  275.   }  
  276.   zfile.close();  
  277.  }  
  278.   
  279.  /** 
  280.   * 给定根目录,返回一个相对路径所对应的实际文件名. 
  281.   *  
  282.   * @param baseDir 
  283.   *            指定根目录 
  284.   * @param absFileName 
  285.   *            相对路径名,来自于ZipEntry中的name 
  286.   * @return java.io.File 实际的文件 
  287.   */  
  288.  public static File getRealFileName(String baseDir, String absFileName) {  
  289.   String[] dirs = absFileName.split("/");  
  290.   File ret = new File(baseDir);  
  291.   if (dirs.length > 1) {  
  292.    for (int i = 0; i < dirs.length - 1; i++) {  
  293.     ret = new File(ret, dirs[i]);  
  294.    }  
  295.    if (!ret.exists())  
  296.     ret.mkdirs();  
  297.    ret = new File(ret, dirs[dirs.length - 1]);  
  298.    return ret;  
  299.   }  
  300.   return ret;  
  301.  }  
  302.   
  303. }  
  304.   
  305.   
  306.   
1 0