这个类可以压缩多个文件并且返回流,在程序中可以再操作返回的流做其它功能,比如验证md5,下面看代码吧
/**
*方法描述:<b>将多个文件压缩成zip包</b></br>
*/
publicbytearrayoutputstreamfiletozip(list<string>filelist,stringzipname,stringtempfilepath){
byte[]buffer=newbyte[1024];
zipoutputstreamout=null;
try{
out=newzipoutputstream(newfileoutputstream(tempfilepath+zipname+".zip"));
list<file>filedata=newarraylist<file>();
for(inti=0,len=filelist.size();i<len;i++)
{
filedata.add(newfile(filelist.get(i)));
}
for(intj=0,len=filedata.size();j<len;j++)
{
fileinputstreamfis=newfileinputstream(filedata.get(j));
out.putnextentry(newzipentry(filedata.get(j).getname()));
intdatalen;
//读入需要下载的文件的内容,打包到zip文件
while((datalen=fis.read(buffer))>0){
out.write(buffer,0,datalen);
}
out.closeentry();
fis.close();
}
out.close();
}
catch(exceptionex)
{
ex.printstacktrace();
}
//读取压缩包
filefilezip=newfile(tempfilepath+zipname+".zip");
bytearrayoutputstreambaos=null;
try
{
baos=newbytearrayoutputstream();
fileinputstreaminstream=newfileinputstream(filezip);
bufferedinputstreambis=newbufferedinputstream(instream);
intc=bis.read();
while(c!=-1){
baos.write(c);
c=bis.read();
}
bis.close();
instream.close();
}
catch(exceptionex)
{
ex.printstacktrace();
}
returnbaos;
}
如对本文有疑问, 点击进行留言回复!!
Error: Avoided redundant navigation to current location: “/XXX“.的问题
Avoided redundant navigation to current location:
荐 四十一、Vue项目上手 | 用户管理系统 实现用户修改和删除功能(完成篇)
网友评论