zip,jar,tar.gz无需解压读取文件内容
2015-12-09 17:55
1476 查看
1。zip包读取
/** * 读取zip中version.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; }
相关文章推荐
- iOS封装静态库,也就是.a文件
- TSql Merge On子句和When not matched 语义理解
- Excel中如何提取字符串中的数字
- Eclipse中Maven工程缺少Maven Dependencies
- yun install gcc 报错 Loaded plugins: fastestmirror, refresh-packagekit, security
- 静态库与动态库介绍(.a库资源处理需验证补充)
- 我的Android进阶之旅------>android Button上面的英文字符串自动大写的问题解决
- Cocos2D-Android-1之源码详解:6.ClickAndMoveTest
- 直接操作USRP属性树
- IIS日志文件清理
- Highcharts candlestick(K线图)案例
- matlab 实践程序5——批量将图片转移文件夹
- C#的Task多线程编程
- git 命令
- Nginx 安装
- 文件和参数一起上传到服务器
- Swift - 加载XIB 文件
- LAMP LAMP环境搭建【rpm】+ 实例应用
- Java(Android)线程池
- MYSQL使用手册