zip,jar,tar.gz无需解压读取文件内容

1。zip包读取

/**
 * 读取zipversion.json的数据
 * */
public String readZipFile(String file) throws Exception {
    StringBuffer sb = new StringBuffer();
    ZipFile zf = new ZipFile(file);
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;
    while ((ze = zin.getNextEntry()) != null) {
        //如果是文件夹
        if (ze.isDirectory()) {
        } else {
            if(ze.getName().equals("installer/config/version.json")){
                long size = ze.getSize();
                if (size > 0) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze),"utf-8"));
                    String line;
                    //读取文件中的内容
                    while ((line = br.readLine()) != null) {
                        sb.append(line);
                    }
                    br.close();
                }
                break;

            }

        }
    }
    zin.closeEntry();
    return sb.toString();
}

2.jar读取文件

public String readFileFromJar(String jarPath ) throws IOException {
    JarFile jf = new JarFile(jarPath);
    Enumeration<JarEntry> jfs = jf.entries();
    StringBuffer sb  = new StringBuffer();
    while(jfs.hasMoreElements())
    {
        JarEntry jfn = jfs.nextElement();
        if(jfn.getName().endsWith("/version.json"))
        {
            InputStream is = jf.getInputStream(jfn);
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] buf = new byte[is.available()];
            while(bis.read(buf)!=-1)
            {
                sb.append(new String(buf).trim());
            }
            bis.close();
            is.close();
            break;
        }
    }
    return sb.toString();
}

3.tar.gz读取文件,需要添加一个jar包
org.apache.commons.compress

public String readTarGz(String tarPath) throws IOException {
    File targzFile = new File(tarPath);
    FileInputStream fileIn = null;
    BufferedInputStream bufIn = null;
    GZIPInputStream gzipIn = null;
    TarArchiveInputStream taris = null;
    try {
        fileIn = new FileInputStream(targzFile);
        bufIn = new BufferedInputStream(fileIn);
        gzipIn = new GZIPInputStream(bufIn);
        taris = new TarArchiveInputStream(gzipIn);
        TarArchiveEntry entry = null;
        while ((entry = taris.getNextTarEntry()) != null) {
            if (entry.isDirectory())
                continue;
            System.out.println(entry.getName());
            if(entry.getName().equals("config/version.json")){
                byte[] b = new byte[(int) entry.getSize()];
                taris.read(b, 0, (int) entry.getSize());
                return new String(b);
            }
        }
    } finally {
        taris.close();
        gzipIn.close();
        bufIn.close();
        fileIn.close();
    }
    return null;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值