Java,J2EE,Weblogic,Oracle

java项目随笔
随笔 - 90, 文章 - 6, 评论 - 61, 引用 - 0
数据加载中……

Java 压缩、解压zip

import java.util.zip.ZipFile;
import java.util.List;
import java.io.File;
import java.util.zip.ZipEntry;
import java.util.ArrayList;
import java.util.Enumeration;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * <p>预览彩信的 smil  </p>
 *
 * <p>该类是解压 zip 文件,及获取 smil 文件的 url </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>卓望_山东168_CMS</p>
 *
 * @author 龚椿深 2006-11-13
 * @version 1.0
 */
public class UnZip {
    private static Log logger = LogFactory.getLog(UnZip.class);
    public UnZip() {
    }

    public static void unZipFile(String filename) {
        ZipEntry entry = null;
        InputStream in = null;
        BufferedOutputStream bout = null;
        ZipFile zip = null;
        try {
            //创建目录
            String tagerpath = filename.substring(0, filename.lastIndexOf(".")); //获取目录名
            File directory = new File(tagerpath);
            directory.mkdirs();

            File file = new File(filename);
            zip = new ZipFile(file);
            Enumeration save = zip.entries();
            //遍历Enumeration,进行保存操作
            while (save.hasMoreElements()) {
                entry = (ZipEntry) save.nextElement();
                //当文件不为目录时进行保存
                if (!entry.isDirectory() && !entry.getName().endsWith("zip")) {
                    //进行保存文件
                    in = zip.getInputStream(entry);
                    bout = new BufferedOutputStream(new FileOutputStream(
                            tagerpath + "/" + entry.getName()));
                    int rc = 0;
                    byte[] buf = new byte[102400];
                    while ((rc = in.read(buf, 0, 102400)) > 0) {
                        bout.write(buf, 0, rc);
                    }
                    bout.flush();
                } else if (entry.getName().endsWith("zip")) {
                    //进行保存文件
//                    InputStream input = new FileInputStream();
                    in = zip.getInputStream(entry);
                    OutputStream out = new FileOutputStream(tagerpath + "/" +
                            entry.getName());

                    // Transfer bytes from in to out
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    in.close();
                    out.close();
                }
            }
        } catch (Exception e) {
            logger.error("解压文件不成功!!" + e.getMessage());
        }
        //关闭文件
        finally {
            if (bout != null) {
                try {
                    bout.close();
                } catch (IOException er) {
                    logger.error(er.getMessage());
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    logger.error(ex.getMessage());
                }
            }
            if (zip != null) {
                try {
                    zip.close();
                } catch (IOException ec) {
                    logger.error(ec.getMessage());
                }
            }
        }
    }


    public static List getSmilList(String tagerpath) {
        File filepath = null;
        List list = null;
        try {
            filepath = new File(tagerpath);
            list = new ArrayList();

            //找出扩展名为 smil 的文件,并加入到 list
            File temp[] = filepath.listFiles();
            for (int i = 0; i < temp.length; i++) {
                System.out.println("abs:"+temp[i].getAbsolutePath());
                System.out.println("can:"+temp[i].getCanonicalPath());
                System.out.println("path:"+temp[i].getPath());
                System.out.println("url:"+temp[i].toURI());
                if (temp[i].getAbsolutePath().toLowerCase().endsWith("smil")) {
                    list.add(temp[i].getAbsolutePath().toString());
                }
              }
        } catch (Exception e) {
            logger.error("UnZip-->getSmilList" + e.getMessage());
        }

        return list;
    }
//获取文件列表
    public static List getZipFileList(String filepath) {
        //定义zip中的实体
        ZipEntry entry = null;
        ZipFile zip = null;
        List list = new ArrayList();
        try {
            File file = new File(filepath);
            zip = new ZipFile(file);
            //将zip中的每个实体放入Enumeration中
            Enumeration num = zip.entries();
            //遍历Enumeration,判断解压文件是否已经存在
            while (num.hasMoreElements()) {
                entry = (ZipEntry) num.nextElement();
                //把 smil 文件添加到列表中
                if (entry.getName().toLowerCase().endsWith("smil")) {
                    list.add(entry.getName());
                }
            }
        } catch (Exception e) {
            logger.error("解压文件不成功!!" + e.getMessage());
        }
        return list;
    }

posted on 2006-11-17 09:53 龚椿深 阅读(783) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: