Java无需解压直接读取Zip文件和文件内容
2017-02-08 17:44
671 查看
package test;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class aaaa {
public static void main(String[] args) throws Exception {
try {
readZipFile("D:\\ztree.zip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readZipFile(String file) throws Exception {
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 {
System.err.println("file - " + ze.getName() + " : "
+ ze.getSize() + " bytes");
long size = ze.getSize();
if (size > 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
System.out.println();
}
}
zin.closeEntry();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class aaaa {
public static void main(String[] args) throws Exception {
try {
readZipFile("D:\\ztree.zip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readZipFile(String file) throws Exception {
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 {
System.err.println("file - " + ze.getName() + " : "
+ ze.getSize() + " bytes");
long size = ze.getSize();
if (size > 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
System.out.println();
}
}
zin.closeEntry();
}
}
相关文章推荐
- Java 并发:线程间通信与协作
- spring cloud
- 浅谈多线程之锁的机制
- struts默认配置文件
- 第二章:java_基本语法_3 运算符
- 在Java中使用协程(Coroutine)
- java 经典算法 例题
- Java 手机号码正则表达式验证
- Java协程框架----Kilim实现机制解析
- JAVA入门[3]—Spring依赖注入
- JAVA入门[2]-安装Maven
- Java并发编程中CountDownLatch和CyclicBarrier的使用
- 性能分析之-- JAVA Thread Dump 分析综述
- Java 中的中文编码
- 深入JVM锁机制-synchronized
- myEclipse2014集成MyBatis插件自动生成代码
- 深入JVM锁机制-Lock
- java的堆,栈,静态代码区 详解
- 跟我学习SSH框架中的国际化如何实现
- synchronized的底层JVM实现机制