国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP上面實現(xiàn)目錄壓縮

2024-09-05 00:20:31
字體:
供稿:網(wǎng)友

zip方法 zippath參數(shù)為保存zip的文件路徑  srcpath參數(shù)為需要壓縮的目錄   在linux window上面測試無問題!主要是編碼問題比較麻煩~要是有其他異常 請留言 或者 有什么更好的方法 歡迎給更多的意見

//zip zhe folder
void zip(string zippath, string srcpath,javax.servlet.jsp.jspwriter out) throws exception {
    fileoutputstream output = null;
    zipoutputstream zipoutput = null;
    try{
        output = new fileoutputstream(zippath);
        zipoutput = new zipoutputstream(output);
        zipentry(zipoutput,srcpath,srcpath,zippath);
    }catch(exception e){
        out.print("file zip error");
    }finally{
        if(zipoutput!=null)zipoutput.close();
    }
    out.print("zip ok"+zippath);
}
//add the zip entry
void zipentry(zipoutputstream zipos, string initpath,string filepath,string zippath) throws exception {
    string entryname = filepath;
    file f = new file(filepath);
    if (f.isdirectory()){// ??
        string[] files = f.list();
        for(int i = 0; i < files.length; i++)
            zipentry(zipos, initpath, filepath + file.separator + files[i],zippath);
        return;
    }
    string chph = initpath.substring(initpath.lastindexof("/") + 1);// ?????
    int idx=initpath.lastindexof(chph);
    if (idx != -1) {
        entryname = filepath.substring(idx);
    }
    zipentry entry;
    entry = new zipentry(entryname);
    file ff = new file(filepath);
    if(ff.getabsolutepath().equals(zippath))return;
    entry.setsize(ff.length());
    entry.settime(ff.lastmodified());
    //the crc efficacy 
    entry.setcrc(0);
    crc32 crc = new crc32();
    crc.reset();
    zipos.putnextentry(entry);
    int len = 0;
    byte[] buffer = new byte[2048];
    int bufferlen = 2048;
    fileinputstream input =null;
    try{
        input = new fileinputstream(filepath);
        while ((len = input.read(buffer, 0, bufferlen)) != -1) {
                zipos.write(buffer, 0, len);
                crc.update(buffer, 0, len);
        }
    }catch(exception e){
    }finally{
        if(input!=null)input.close();
    }
    entry.setcrc(crc.getvalue());


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊宁县| 凯里市| 九龙城区| 彰化县| 长治县| 永德县| 鹤庆县| 新河县| 余江县| 宜阳县| 云安县| 昌宁县| 五指山市| 福鼎市| 阳谷县| 博爱县| 应城市| 内丘县| 丰都县| 仪征市| 宣武区| 丽江市| 会泽县| 漳平市| 育儿| 新化县| 车致| 康定县| 湟中县| 天等县| 卢氏县| 抚顺市| 古田县| 曲松县| 凤翔县| 衡山县| 安多县| 大化| 抚松县| 东山县| 山东省|