Java 压缩文件

public static void compresszip (List(File) fileList, Outputstream outputstream)if (nuli--fileList 1)fileList.isEmpty()II null--outputStream)return
//ZIP输出流
Zipoutputstream zipoutputstream null
//将文件流或者网络流包装成ZIP输出流,
zipoutputstream new zipoutputstrear w Bufferedoutputstream(outputstream)//设置压缩方法
zipoutputstream.setMethod(zipEntry.DEFLATED);
for(File file:fileList){
//如果文件不存在,或是隐藏文件,或是目录,则过滤
if (Ifile, exists() ll file, isHidden() ll file, isDirectory()) continue;
Inputstream in new FileInputstream(file);
//添加zipEntry
zipoutputstream.putNextEntry(new ZipEntry(f41e.getName());byte[] cache.new byte[1024];
int count ;
while(count-in.read(cache))I--1){
11 详zip输出流对象写入数据
zipoutputstream.write(cache,off:e,count);
//关闭当前输入流
in.close();
1/关闭entry流
zipoutputstream.closeEntry();
} catch (FileNotFoundException e) {
e.printstackTrace();
System.out.println("文件未找到异常:"+e.getMessage());
} catch(IOException e){
e-printstackTrace();
System.out.println("读取文件异常:"+e.getMessage());
} finally {
try
if(objects.nonNull(zipoutputstream)){
// 将最后写人的数据刷入输出流
ZipQutputstream.flush();
zipoutputstream.close();
if(objects,nonNull(outputstream))
outputstream,close();
public static HttpServletResponse setDownloadResponse (HttpServletRequest request, HttpServletResponse response, String downloadione)try {
response.reset();
response.setCharacterEncoding("UTF-8"):
response.setContentType("application/octet-stream");
response.setheader(s"Content-Disposition",sl:"attachment;filename-"+FileUtils.setFileDownLoodHeader(request,downloadilame));return respons
} catch (UnsupportedEncodingException e){
e-printstackTrace();
return null;
public static String setFileDown loadHeader(HttpServletRequest request, String fileName)throws UnsupportedEncodingException(
final String agent - request-getHeader("USER-AGENT");
String filename=fileName;
if(agent.contains("MSIE")){
//IE浏览器
filename·URLEncoder.encode(filename,"utf-8");
filename=filename.replace("+", "");
}else if(agent.contains("Firefox")){
//火狐浏览器
filename = new String(fileName.getBytes(),  "IS08859-1");
}else if(agent.contains("Chrome")){
// google浏览器
filename = new String(fileName.getBytes(), "IS08859-1");
// filename = URLEncoder . encode ( filename , " IS08859-1 " ) ;
}else
// 它浏览器
filename=new String(fileName.getBytes(),"utf-8");
// filename = URLEncoder.encode(filename,"utf-8");
return filename;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值