有关于zip文件流的解压缩操作
来源:互联网 发布:集中式负载均衡算法 编辑:程序博客网 时间:2024/06/07 08:54
/***********************************************************************
*
*@author:chenyong
*@data: 2011-03-05
*
************************************************************************/
struts2中实现
一、读取zip文件流并解压读取文件内容:
this.request = ServletActionContext.getRequest();
this.response = ServletActionContext.getResponse();
System.out.println("==== begin ====");
BufferedReader br = null;
ServletInputStream is = null;
BufferedInputStream bis = null;
ZipInputStream zis = null;
try {
StringBuffer sbContent = new StringBuffer();
is = request.getInputStream();
bis = new BufferedInputStream(is, 2048);
zis = new ZipInputStream(bis);
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
long size = ze.getSize();
if (size > 0) {
String line;
br = new BufferedReader(new InputStreamReader(zis, "UTF-8"));
while ((line = br.readLine()) != null) {
sbContent.append(line);
}
}
}
System.out.println(sbContent.toString());
PrintWriter out = this.response.getWriter();
out.write(sbContent.toString());
out.write(new String("数据读取成功!".getBytes(),"UTF-8"));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(null!=br)
br.close();
zis.close();
bis.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("=== end === ");
return null;
二、下砸压缩文件流(将字符串写入xml中,再压缩后提供下载):
@SuppressWarnings("deprecation")
public String GetDelphiFile() {
this.request = ServletActionContext.getRequest();
this.response = ServletActionContext.getResponse();
return SUCCESS;
}
final int BUFFER = 1024;
public InputStream getInputStream() {
String FILENAME = "gdzc";
InputStream is=null;
Document document = DocumentHelper.createDocument();
Element element = document.addElement("root");
Element el = element.addElement("record");
el.addAttribute("id", "1");
el.addAttribute("name", new String("笔记本电脑"));
Element record2 = element.addElement("record");
record2.addAttribute("id", "2");
record2.addAttribute("name", "办公桌");
try {
byte[] b = document.asXML().getBytes();
int count;
String url =this.request.getRealPath("");
//创建xml文件File gdzc = File.createTempFile("gdzc", ".xml");
url = url.substring(0,url.lastIndexOf("//"));
String filepath =url.substring(0,url.lastIndexOf("//"));
File gdzc = new File(filepath+"//temp//"+FILENAME+".xml"); //此临时文件保存路径为服务器tomcat的安装目录下temp 文件夹中
if(!gdzc.exists()){
gdzc.createNewFile();
}
FileOutputStream fosgdzc = new FileOutputStream(gdzc);
for (int i = 0; i < b.length; i++) {
fosgdzc.write(b[i]);//将文本内容写入xml中
}
fosgdzc.close();
//创建压缩文件
File zip = File.createTempFile(FILENAME,".zip");
FileOutputStream fos = new FileOutputStream(zip);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
fos));
//创建压缩包内文件
ZipEntry zEntry = new ZipEntry(gdzc.getName());
out.putNextEntry(zEntry);
FileInputStream fis = new FileInputStream(gdzc);
BufferedInputStream bis = new BufferedInputStream(fis, BUFFER);
b = new byte[BUFFER];
while ((count = bis.read(b, 0, BUFFER)) != -1) {
out.write(b,0,count); //将gdzc.xml写入压缩包中
}
is = new FileInputStream(zip);
bis.close();
fis.close();
out.close();
//将zip包写入 inputstream 中
ServletOutputStream sos = this.response.getOutputStream();
b= new byte[BUFFER];
while((count=is.read(b, 0, BUFFER))!=-1){
sos.write(b,0,BUFFER);
}
sos.close();
//刪除临时文件
if(gdzc.exists()){
gdzc.delete();
}
if(zip.exists()){
zip.delete();
}
this.response.flushBuffer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
- 有关于zip文件流的解压缩操作
- Android 解压缩ZIP文件操作
- 解压缩带有中文名文件的ZIP包
- 解压缩带有中文名文件的ZIP包 .
- java 解压缩zip文件
- linux 解压缩zip文件
- java 解压缩zip文件
- PHP解压缩zip文件
- zip文件压缩解压缩
- Linux 解压缩Zip文件
- java解压缩zip文件
- java解压缩zip文件
- zip文件压缩解压缩
- java zip 解压缩文件
- 解压缩zip文件
- java Zip文件解压缩
- java zip文件解压缩
- python解压缩zip文件
- 数组
- SQL分页存储示例收藏
- 线性链表逆转
- 转:ubuntu另一串口工具cutecom
- 重回来看看
- 有关于zip文件流的解压缩操作
- 关于Symbian的Eleave
- linux学习
- linux
- 彻底解密C++宽字符:4、利用codecvt和use_facet转换
- Eclipse 中的 HelloOSGI
- ACE网络通信库的配置
- EXT combo 如何select的时候就失去焦点
- 2011-3-11 hibernate二级缓存实际体验