java不解压直接读取.zip压缩文件该示例的,关于下面演示的项目源代下载地址如下:
源代码下载地址
package com.sd.test.readzip;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
//注意要将文件保存为utf-8,txt文件的默认保存为ANSI.要改成urf-8不然会像file:ziptestfile/fileInZip.txt
public class ReadZip {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ReadZip rz=new ReadZip();
rz.readZipContext();
}
public void readZipContext() throws IOException{
String zipPath="zipFile/ziptestfile.zip";
ZipFile zf=new ZipFile(zipPath);
InputStream in=new BufferedInputStream(new FileInputStream(zipPath));
ZipInputStream zin=new ZipInputStream(in);
//ZipEntry 类用于表示 ZIP 文件条目。
ZipEntry ze;
while((ze=zin.getNextEntry())!=null){
if(ze.isDirectory()){
//为空的文件夹什么都不做
}else{
System.err.println("file:"+ze.getName()+"\nsize:"+ze.getSize()+"bytes");
if(ze.getSize()>0){
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(zf.getInputStream(ze), "utf-8"));
String line=null;
while((line=reader.readLine())!=null){
System.out.println(line);
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
输出结果:
file:ziptestfile/fileInZip.txt
size:395bytes
1��zip�����
2��json���ݣ�{“age”:21,”list”:[“one”,”two”],”list1”:[{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}],”map”:{“one”:{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},”two”:{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}},”name”:”da jun”,”sex”:”boy”,”strudent”:false,”student”:null}
3��sdfadh
file:ziptestfile/fileInZip1.txt
size:408bytes
1、zip中的文字
2、json数据:{“age”:21,”list”:[“one”,”two”],”list1”:[{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}],”map”:{“one”:{“age”:20,”name”:”xiao ming”,”sex”:”boy”,”strudent”:true},”two”:{“age”:23,”name”:”xiao hong”,”sex”:”girl”,”strudent”:true}},”name”:”da jun”,”sex”:”boy”,”strudent”:false,”student”:null}
3、sdfadh