Esta classe pode compactar vários arquivos e retornar fluxos.
A cópia do código é a seguinte:
/**
* Método Descrição: <b> Classe de teste </b> </br>
*/
classe pública testFileStream {
// Localização de armazenamento de arquivos e pacotes compactados
StringTempFilePath = "C:/temp/"
List <String> filelist = newArrayList <String> ();
filelist.add (tempfilepath+"file1.txt");
filelist.add (tempfilepath+"file2.png");
filelist.add (tempfilepath+"file3.xls");
// o nome de pacote compactado gerado
StringzipName = "Filedata";
// retorna ao fluxo
ByteArrayOutputStreamOutputStream = FileTozip (FILELIST, FILEDATA, TEMPFILEPATH);
// Fluxo de pacote compactado de entrada de página
byte [] buffer = outputStream.tobytearray ();
// resposta clara
Response.Reset ();
// Defina o cabeçalho de resposta
Response.addheader ("Disposição de conteúdo",
"Anexo; nome do arquivo ="+
newstring (("datafile.zip"). getBytes ("gb2312"), "iso8859-1"));
Response.AddHeader ("Length-comprimento", ""+outputStream.size ());
toClient = newBufferOutputStream (Response.getOutputStream ());
Response.setContentType ("Application/Octet-Stream");
toClient.Write (buffer);
toClient.flush ();
}
/**
*Método Descrição: <b> Compra vários arquivos em pacotes zip </b> </br>
*/
publicByTearRayOutputStreamFileTozip (List <String> FILELIST, StringzipName, StringTempFilePath) {
byte [] buffer = newbyte [1024];
ZipOutputStreamout = null;
tentar{
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;
// Leia o conteúdo do arquivo que você precisa baixar e empacotá -lo no arquivo zip
while ((datalen = fis.read (buffer))> 0) {
out.Write (buffer, 0, Datalen);
}
out.CloseEntry ();
fis.close ();
}
out.Close ();
}
Catch (ExceptionEx)
{
Ex.PrintStackTrace ();
}
// Leia o pacote compactado
Filefilezip = newfile (tempfilepath+zipName+". Zip");
ByteArrayOutputStreambaos = null;
tentar
{
Baos = newByTearraRayOutputStream ();
FileInputStreamInstream = newFileInputStream (FILEZIP);
BufferInputStreambis = newBufferInputStream (Instream);
intc = bis.read ();
while (c! =-1) {
baos.write (c);
c = bis.read ();
}
bis.close ();
Instruam.close ();
}
Catch (ExceptionEx)
{
Ex.PrintStackTrace ();
}
returnbaos;
}