AceofHert 2016-12-04 07:26 采纳率: 78.6%
浏览 1408
已采纳

zip()方法中第三个参数""是什么意思,以及代码中的base是什么意思

import java.io.*;
import java.util.zip.*;

public class MyZip {
private void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
zip(out, inputFile, ""); //第三个参数“”是什么意思
System.out.println("压缩中…");
out.close();
}

private void zip(ZipOutputStream out, File f, String base)
        throws Exception { 
    if (f.isDirectory()) { 
        File[] fl = f.listFiles(); 
        out.putNextEntry(new ZipEntry(base + "/"));   //以下几行代码base是什么意思
        base = base.length() == 0 ? "" : base + "/"; 
        for (int i = 0; i < fl.length; i++) {
            zip(out, fl[i], base + fl[i]);
        }
    } else {
        out.putNextEntry(new ZipEntry(base)); 

        FileInputStream in = new FileInputStream(f);
        int b;
        System.out.println(base);
        while ((b = in.read()) != -1) { 
            out.write(b);
        }
        in.close();
    }
}

public static void main(String[] temp) { 
    MyZip book = new MyZip(); 
    try {

        book.zip("hello.zip", new File("src"));
        System.out.println("压缩完成"); 
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

  • 写回答

2条回答

  • 半杯凉月 2016-12-04 07:46
    关注

    base就是个你传入的路径,“”就是传入空内容的路径, base = base.length() == 0 ? "" : base + "/"; 三目运算符,判断如果传入的base内容为空串,base 就等于"/";

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?