国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 呼伦贝尔市| 堆龙德庆县| 西乡县| 本溪市| 余姚市| 长海县| 利津县| 石屏县| 昌平区| 牡丹江市| 武胜县| 行唐县| 赞皇县| 丰宁| 康平县| 靖州| 祁阳县| 南汇区| 措勤县| 抚松县| 四平市| 巴南区| 高碑店市| 油尖旺区| 石城县| 仁布县| 任丘市| 岱山县| 正阳县| 丹棱县| 综艺| 淮南市| 南和县| 西藏| 苍溪县| 桐柏县| 鄂尔多斯市| 桐柏县| 同心县| 张家界市| 青浦区|