<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
    數(shù)據(jù)加載中……

    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>預(yù)覽彩信的 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 {
    ??????????? //創(chuàng)建目錄
    ??????????? 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();
    ??????????????? //當(dāng)文件不為目錄時進行保存
    ??????????????? 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());
    ??????? }
    ??????? //關(guān)閉文件
    ??????? 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,判斷解壓文件是否已經(jīng)存在
    ??????????? 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 龔椿深 閱讀(796) 評論(0)  編輯  收藏


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 羞羞网站免费观看| 亚洲国产精品日韩在线观看| 亚洲av成人一区二区三区观看在线| 麻花传媒剧在线mv免费观看| 亚洲网址在线观看你懂的| 久久国产精品免费专区| 亚洲午夜在线电影| 免费观看激色视频网站bd| 亚洲人和日本人jizz| 97免费人妻无码视频| 亚洲精品无码成人| 国产免费小视频在线观看| 男人j进女人p免费视频| 国产亚洲日韩在线三区| 国产精品极品美女自在线观看免费| 国产午夜亚洲精品国产成人小说| 成在线人视频免费视频| 亚洲∧v久久久无码精品| 天天影视色香欲综合免费| 亚洲av无码专区在线| 国产福利免费在线观看| 青青视频免费在线| 亚洲中文字幕无码久久综合网| 久久爰www免费人成| 精品亚洲成A人无码成A在线观看| 小小影视日本动漫观看免费| 四虎影视久久久免费观看| 亚洲中文字幕久久精品无码APP| 青青草原1769久久免费播放| 国产成人精品日本亚洲11| 亚洲精品久久久www | 2048亚洲精品国产| 精品国产麻豆免费人成网站| 亚洲午夜电影在线观看高清| 国产男女猛烈无遮挡免费视频| 国产做国产爱免费视频| 亚洲导航深夜福利| 亚洲AV伊人久久青青草原| 久久午夜伦鲁片免费无码| 久久亚洲中文字幕无码| 亚洲精品无码永久在线观看你懂的|