<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 龔椿深 閱讀(792) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲国产成人无码av在线播放| 亚洲AV无码久久精品狠狠爱浪潮| 亚洲一级在线观看| 99国产精品免费观看视频| 亚洲va久久久噜噜噜久久| 免费无码av片在线观看| 亚洲gv白嫩小受在线观看| 久久ww精品w免费人成| 久久久久亚洲av无码专区导航| 最近中文字幕国语免费完整 | 亚洲男人的天堂在线va拉文| 产传媒61国产免费| 在线观看国产区亚洲一区成人 | 一级毛片aa高清免费观看| 亚洲国产小视频精品久久久三级 | 18禁美女裸体免费网站| 亚洲国产精品成人精品软件| 国产精品成人免费一区二区| 亚洲狠狠婷婷综合久久| 免费亚洲视频在线观看| 久久精品免费网站网| 亚洲AV无码国产精品麻豆天美| 中文字幕免费在线| 亚洲一卡一卡二新区无人区| 日本免费v片一二三区| 一级一黄在线观看视频免费| 亚洲AV无码不卡无码| 免费在线看v网址| 久久综合亚洲色hezyo| 国产gv天堂亚洲国产gv刚刚碰 | 国产精品免费一区二区三区四区| 亚洲专区先锋影音| 在线a人片天堂免费观看高清| 美女又黄又免费的视频| 亚洲精品无码久久千人斩| 日本高清在线免费| 日韩精品视频在线观看免费| 亚洲男人的天堂在线播放| 午夜影视在线免费观看| 全黄大全大色全免费大片| 亚洲一级特黄特黄的大片|