不解压直接读取压缩包中的文件
来源:互联网 发布:淘宝子账号权重值500 编辑:程序博客网 时间:2024/05/18 03:42
项目中需要用到不解压压缩包,直接读取压缩包中的文件,于是研究下了,现整理出来。
读取指定文件有两种思路,一种是在循环中遍历进行判断,另一种是直接通过文件名进行获取;
- 通过文件名直接获取
使用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
目录下。
0 0
- 不解压直接读取压缩包中的文件
- Java不解压直接读取压缩包文件
- 如何不解压压缩包让foobar直接读取音乐文件
- Android 不解压直接读取zip包
- 不解压的情况下直接读取压缩包内容(压缩包中有的文件名含中文)
- Java Gzip 不解压,直接读取txt文件内容
- linux文件包压缩解压
- 不解压直接查看tar包内容
- python中的压缩、解压包tarfile
- android解压ZIP压缩包文件
- springMVC 上传压缩包文件、解压
- SharpZipLib 解压 多个分压缩包文件
- 文件上传压缩包并解压
- 用UnRar\UnZip\UltraISO查看压缩包中的文件和解压文件
- 压缩包 解压包 保存包到网络 文件打包
- 文件上传---文件解压---读取文件---文件重新命名压缩
- java学习笔记:使用zip api进行文件解压缩以及不解压直接读取指定文件内容
- Java 小例子:读取和解压 zip 压缩包
- Hawk-and-Chicken (hdu 3639 强连通缩点+反向建图DFS)
- iOS 【transform属性】-通过tramsform熟悉个可以修改对象的平移、缩放比例和旋转角度
- HDU 1241 Oil Deposits【dfs最大联通块(洪水算法)】
- android应用生命周期
- HDU5348 MZL's endless loop (搜索)
- 不解压直接读取压缩包中的文件
- 菜鸟学Android(四十四):jsp标签技术简介及jsp标签的开发详解
- HTTPS、SPDY和HTTP/2的性能比较
- Python 爬虫
- Android 谷歌 开源 通信框架 VOLLEY(六)——应用实例
- ListView,3行
- Lttng document
- FZU 2167 大王叫我来巡山呐
- 关于ScrollView的知识