이 클래스는 프로그램에서 여러 파일을 압축 할 수 있습니다.
코드 사본은 다음과 같습니다.
/**
* 메소드 설명 : <b> 테스트 클래스 </b> </br>
*/
공개 수업 testfilestream {
// 파일 및 압축 패키지의 저장 위치
StringTempFilePath = "c :/temp/"
List <string> filleList = newArrayList <String> ();
fileList.add (TempFilePath+"File1.txt");
filelist.add (tempfilepath+"file2.png");
filelist.add (tempfilepath+"file3.xls");
// 생성 된 압축 패키지 이름
StringZipName = "FileData";
// 스트림으로 돌아갑니다
BytearRayoutputStreamOutputStream = filetozip (FilElist, filedata, tempfilepath);
// 페이지 입력 압축 패킷 흐름
바이트 [] buffer = outputStream.tobytearRay ();
// 응답을 명확하게합니다
response.reset ();
// 응답 헤더를 설정합니다
response.addheader ( "Content-Disposition",
"첨부 파일; 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 (버퍼);
toclient.flush ();
}
/**
*메소드 설명 : <b> 여러 파일을 zip 패키지로 압축합니다 </b> </br>
*/
publicbytearrayoutputstreamfiletozip (list <string> filleList, StringZipName, StringTempFilePath) {
바이트 [] 버퍼 = NewByte [1024];
ZipOutputStreamout = null;
노력하다{
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+Zipamame+". Zip");
bytearrayoutputstreambaos = null;
노력하다
{
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;
}