不解压直接读取压缩包中的文件
2015-08-05 14:44
513 查看
项目中需要用到不解压压缩包,直接读取压缩包中的文件,于是研究下了,现整理出来。
读取指定文件有两种思路,一种是在循环中遍历进行判断,另一种是直接通过文件名进行获取;
通过文件名直接获取
使用zipFile.getEntry(“文件名”)方法获取
循环遍历中根据文件名进行判断
在main方法中调用
通过上面的几种方法可以实现直接从有压缩包中读取指定文件,当然我们也可以把指定文件写入到指定目录中再打开,实现代码如下:
上面的代码实现了,把
读取指定文件有两种思路,一种是在循环中遍历进行判断,另一种是直接通过文件名进行获取;
通过文件名直接获取
使用zipFile.getEntry(“文件名”)方法获取
public static void readZipFile1(String file,String fileName) throws Exception { ZipFile zf = new ZipFile(file); ZipEntry ze = zf.getEntry(fileName); InputStream in = zf.getInputStream(ze); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; StringBuffer result = new StringBuffer(); while ((line = br.readLine()) != null) { result.append(line+"\n"); } System.out.println(result); }
循环遍历中根据文件名进行判断
public static void readZipFile2(String file,String fileName) throws Exception { ZipFile zf = new ZipFile(file); InputStream in = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zin = new ZipInputStream(in); ZipEntry ze; StringBuffer result = new StringBuffer(); while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { continue; } else { long size = ze.getSize(); String name = ze.getName(); if (size > 0 && fileName.equals(name)) { BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze))); String line; while ((line = br.readLine()) != null) { result.append(line+"\n"); } br.close(); } } } System.out.println(result); zin.closeEntry(); }
public static void readZipFile3(String file, String fileName) { try { ZipFile zipFile = new ZipFile(file); StringBuffer result = new StringBuffer(); Enumeration<ZipEntry> enu = (Enumeration<ZipEntry>) zipFile.entries(); while (enu.hasMoreElements()) { ZipEntry zipElement = (ZipEntry) enu.nextElement(); InputStream read = zipFile.getInputStream(zipElement); String name = zipElement.getName(); if (name != null && fileName.equals(name)) { BufferedReader br = new BufferedReader(new InputStreamReader(read)); String line; while ((line = br.readLine()) != null) { result.append(line+"\n"); } br.close(); } } } catch (Exception e) { e.printStackTrace(); } }
在main方法中调用
public static void main(String[] args) throws Exception { readZipFile("G://report.zip", "1004165931.html"); }
通过上面的几种方法可以实现直接从有压缩包中读取指定文件,当然我们也可以把指定文件写入到指定目录中再打开,实现代码如下:
/** * * @param file 压缩包路径 * @param saveRootDirectory 写入文件夹路径 * @param fileName 文件名 * @throws FileNotFoundException * @throws IOException */ public static void writeZipFile(String file,String saveRootDirectory,String fileName) throws FileNotFoundException, IOException { int len = 0; ZipFile zf = new ZipFile(file); ZipEntry ze = zf.getEntry(fileName); InputStream read = zf.getInputStream(ze); File writeFile = new File(saveRootDirectory + fileName); if (!writeFile.exists()) { File rootDirectoryFile = new File(saveRootDirectory); //创建目录 if (!rootDirectoryFile.exists()) { rootDirectoryFile.mkdirs(); } //创建文件 writeFile.createNewFile(); BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(file)); //写入文件内容 while ((len = read.read()) != -1) { write.write(len); } write.flush(); write.close(); } read.close(); }
上面的代码实现了,把
file路径下的压缩包中的名为
fileName的文件写入到
saveRootDirectory目录下。
相关文章推荐
- iOS 【transform属性】-通过tramsform熟悉个可以修改对象的平移、缩放比例和旋转角度
- Android 谷歌 开源 通信框架 VOLLEY(六)——应用实例
- 用Jquery来控制A标签的class,以达到控制其样式
- java获取当前路径
- oracle安装报错:Could not retrieve local nodename.
- DRA(Data Recovery Advisor)的使用
- Android图片下载缓存库picasso解析
- MBR与GPT分区格式(实例-创建大于2TB的分区)
- ListView的item点击事件不响应(你觉得应该响应的,但是就是没响应,而且不是代码问题)
- iOS 知识点
- 【Qt】滚动条QScroolBar实现滚屏功能
- HDU 4597 Play Game
- 解决jquery操作checkbox全选全不选无法勾选问题
- 剑指offer面试题15——扩展2:判断一个单向链表是否形成环结构
- Mac OS 上安装Ruby环境
- 单片机矩阵按键长短按
- Marquee Effect
- 南邮 OJ 1356 方格取数问题
- Android 自定义ViewGroup 实现流式布局
- ExtJs 中 Ajax 连接超时设置与问题排查