zip的打包与解包和包下载
text文件压缩包解析与下载
//压缩包下载
private StreamedContent newsTemplate;
//该方法是对压缩包进行下载
public StreamedContent getNewsTemplate() {
//对多个Text文件进行压缩
ZipFiles(files,file);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(FaceContextUtil.getCurtExternalContext().getRealPath("") +
"/resources/pack/appData.zip");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
newsTemplate = new DefaultStreamedContent(inputStream, "", "appData.zip");
return newsTemplate;
}
//压缩多个文件
public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf = new byte[1024];
try {
//创建zip压缩包输出流
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// 压缩文件
for (int i = 0; i < srcfile.length; i++) {
//读取数组中的文件
FileInputStream in = new FileInputStream(srcfile[i]);
// 将该文件名添加到zip输出
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
// Transfer bytes from the file to the ZIP file
//传送字节到zip包的文件中
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
//完成第一个文件的传入
out.closeEntry();
in.close();
}
// Complete the ZIP file
//完成压缩,关闭输出流
out.close();
System.out.println("压缩完成.");
}
catch (IOException e) {
e.printStackTrace();
}
}
对zip包进行解压
Enumeration /nju:mê'rei shên/列举 计算 项目
public static void unZipFiles(File zipfile,String descDir){
try {
ZipFile zf=new ZipFile(zipfile);
//Enumeration接口的对象,他生成一系列元素,一次生成一个, 连续调用nextElement方法返回一系列连续元素
//zf为所有元素,entries为Enumeration的一个元素,ertries.hasMoreElements为是否还存在下一个元素
for(Enumeration entries=zf.entries();entries.hasMoreElements();){
ZipEntry entry=(ZipEntry) entries.nextElement();
String zipEntryName=entry.getName();
InputStream in=zf.getInputStream(entry);
OutputStream out=new FileOutputStream(descDir+zipEntryName);
byte[] buf1=new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
System.out.println("解压缩完成.");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
不解压,直接读取压缩包文件 ,根据json转换,加入集合
public String readZipFile(String file) throws Exception {
list = new ArrayList<RltVip>();
list2 = new ArrayList<RltStaR>();
list3 = new ArrayList<RltAppDev>();
//创建Zip压缩文件
ZipFile zipFile = null;
//创建Zip流
ZipInputStream zin = null;
InputStream in = null;
StringBuffer sbf = null;
try {
zipFile = new ZipFile(file);
in = new BufferedInputStream(new FileInputStream(file));
zin = new ZipInputStream(in);
ZipEntry ze;
sbf = new StringBuffer();
while ((ze = zin.getNextEntry()) != null) {
Object lh = (Object) ze;
String rv = String.valueOf(lh);
if (ze.isDirectory()) {
return null;
} else {
if (rv.equals("ResultVip.txt")) {
StringBuffer sBuffer = new StringBuffer();
// 这里的判断不能用ze.getSize() > 0, 当文件的大小很小时,会返回-1
if (ze.getSize() != 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(
zipFile.getInputStream(ze), Charset.forName("utf-8")));
String line;
while ((line = br.readLine()) != null) {
sBuffer.append(line + "\r\n");
try {
RltVip vip = JsonHelper.CRAZY_GSON.fromJson(line, RltVip.class);
list.add(vip);
} catch (Exception e) {
rltVipIndex++;
continue;
}
}
sbf.append(sBuffer);
br.close();
}
} else if (rv.equals("ResultStatistic.txt")) {
StringBuffer sBuffer = new StringBuffer();
// 这里的判断不能用ze.getSize() > 0, 当文件的大小很小时,会返回-1
if (ze.getSize() != 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(
zipFile.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
sBuffer.append(line + "\r\n");
try {
RltStaR rltStaR1 = JsonHelper.CRAZY_GSON.fromJson(line, RltStaR.class);
System.out.printf(rltStaR1.toString());
list2.add(rltStaR1);
} catch (Exception e) {
rltStatIndex++;
continue;
}
}
sbf.append(sBuffer);
br.close();
}
} else if (rv.equals("ResultDevice.txt")) {
StringBuffer sBuffer = new StringBuffer();
// 这里的判断不能用ze.getSize() > 0, 当文件的大小很小时,会返回-1
if (ze.getSize() != 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(
zipFile.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
sBuffer.append(line + "\r\n");
try {
RltAppDev rlt = JsonHelper.CRAZY_GSON.fromJson(line, RltAppDev.class);
list3.add(rlt);
} catch (Exception e) {
rltDevsIndex++;
continue;
}
}
sbf.append(sBuffer);
br.close();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (zin != null) {
zin.close();
}
if (zipFile != null) {
zipFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return sbf.toString();
}