This class can compress multiple files and return to the stream, in the program can again operate the returned stream to do other functions, such as verifying MD5, see the code below
/**
* Method Description:<b> Test class </b></br>
*/
public class testfilestream{
Location of files and compression packs stored
Stringtempfilepath= "c:/temp/"
List<string>filelist=newarraylist<string> ();
Filelist.add (tempfilepath+ "file1.txt");
Filelist.add (tempfilepath+ "file2.png");
Filelist.add (tempfilepath+ "File3.xls");
Name of the compressed package generated
Stringzipname= "Filedata";
return stream
Bytearrayoutputstreamoutputstream=filetozip (Filelist,filedata,tempfilepath);
Page input Compression Package flow
Byte[]buffer=outputstream.tobytearray ();
Empty response
Response.reset ();
Set header for response
Response.AddHeader ("Content-disposition",
"Attachment;filename=" +
NewString (("Datafile.zip"). GetBytes ("gb2312"), "iso8859-1");
Response.AddHeader ("Content-length", "" "+outputstream.size ());
Toclient=newbufferedoutputstream (Response.getoutputstream ());
Response.setcontenttype ("Application/octet-stream");
Toclient.write (buffer);
Toclient.flush ();
}
/**
* Method Description:<b> Compresses multiple files into zip packages </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;
Read the contents of the file you want to download and package it into a zip file
while ((Datalen=fis.read (buffer)) >0) {
Out.write (Buffer,0,datalen);
}
Out.closeentry ();
Fis.close ();
}
Out.close ();
}
catch (Exceptionex)
{
Ex.printstacktrace ();
}
Reading a compressed package
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;
}