闪宁斯达 / 压缩/解压 / import java.util.zip.ZipEntry; zip 压缩...

分享

   

import java.util.zip.ZipEntry; zip 压缩 解压

2011-05-31  闪宁斯达
package com.zrar.cms.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
 
 
 
public class T {
 public static void main(String[] args) throws Exception{
//    createZip("F:/t/99999", "F:/t/8888.zip");
    releaseZipToFile("F:/t/8888.zip", "F:/t");
  
 }
 
 
    /**
     * zip压缩功能测试. 将d:\\temp\\zipout目录下的所有文件连同子目录压缩到d:\\temp\\out.zip.
     *
     * @param baseDir 所要压缩的目录名(包含绝对路径)
     * @param objFileName 压缩后的文件名
     * @throws Exception
     */
    public static void createZip(String baseDir, String objFileName) throws Exception {
           File folderObject = new File(baseDir);
           if (folderObject.exists()){
                   List<File> fileList = getSubFiles(new File(baseDir));
                   //压缩文件名
                   ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFileName));
                   ZipEntry ze = null;
                   byte[] buf = new byte[1024];
                   int readLen = 0;
                   for (int i = 0; i < fileList.size(); i++) {
                           File f = (File) fileList.get(i);
                           System.out.println("Adding: " + f.getPath());
                           //创建一个ZipEntry,并设置Name和其它的一些属性
                           ze = new ZipEntry(getAbsFileName(baseDir, f));
                           ze.setSize(f.length());
                           ze.setTime(f.lastModified());
                           //将ZipEntry加到zos中,再写入实际的文件内容
                           zos.putNextEntry(ze);
                           InputStream is = new BufferedInputStream(new FileInputStream(f));
                           while ((readLen = is.read(buf, 0, 1024)) != -1) {
                                   zos.write(buf, 0, readLen);
                           }
                           is.close();
                           System.out.println("done...");
                   }
                   zos.close();
           }else{
                   throw new Exception("this folder isnot exist!");
           }
   }
  
 private static List<File> getSubFiles(File baseDir) {
           List<File> ret = new ArrayList<File>();
           //File base=new File(baseDir);
           File[] tmp = baseDir.listFiles();
           for (int i = 0; i < tmp.length; i++) {
                   if (tmp[i].isFile()) {
                           ret.add(tmp[i]);
                   }
                   if (tmp[i].isDirectory()) {
                           ret.addAll(getSubFiles(tmp[i]));
                   }
           }
           return ret;
   }
    private  static String getAbsFileName(String baseDir, File realFileName) {
           File real = realFileName;
           File base = new File(baseDir).getParentFile();
           String ret = real.getName();
           while (true) {
                   real = real.getParentFile();
                   if (real == null)
                           break;
                   if (real.equals(base))
                           break;
                   else {
                           ret = real.getName() + "/" + ret;
                   }
           }
           System.out.println("Adding: " + ret);
           return ret;
   }
   
   
    /**
        * 测试解压缩功能. 将d:\\download\\test.zip连同子目录解压到d:\\temp\\zipout目录下.
        *
        * @throws Exception
        */
       public static void releaseZipToFile(String sourceZip, String outFileName)
                       throws IOException{
  ZipFile zfile = new ZipFile(sourceZip);
  Enumeration zList = zfile.entries();
  ZipEntry ze = null;
  byte[] buf = new byte[1024];
  while (zList.hasMoreElements()) {
   // 从ZipFile中得到一个ZipEntry
   ze = (ZipEntry) zList.nextElement();
   if (ze.isDirectory()) {
    continue;
   }
   // 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
   OutputStream os = new BufferedOutputStream(new FileOutputStream(
     getRealFileName(outFileName, ze.getName())));
   InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
   int readLen = 0;
   while ((readLen = is.read(buf, 0, 1024)) != -1) {
    os.write(buf, 0, readLen);
   }
   is.close();
   os.close();
  }
  zfile.close();
       }
       /**
        * 给定根目录,返回一个相对路径所对应的实际文件名.
        *
        * @param baseDir
        *            指定根目录
        * @param absFileName
        *            相对路径名,来自于ZipEntry中的name
        * @return java.io.File 实际的文件
        */
       private static File getRealFileName(String baseDir, String absFileName) {
               String[] dirs = absFileName.split("/");
               //System.out.println(dirs.length);
               File ret = new File(baseDir);
               //System.out.println(ret);
               if (dirs.length > 1) {
                       for (int i = 0; i < dirs.length - 1; i++) {
                               ret = new File(ret, dirs[i]);
                       }
               }
               if (!ret.exists()) {
                       ret.mkdirs();
               }
               ret = new File(ret, dirs[dirs.length - 1]);
               return ret;
       }
 
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多
    喜欢该文的人也喜欢 更多

    ×
    ×

    ¥.00

    微信或支付宝扫码支付:

    开通即同意《个图VIP服务协议》

    全部>>