Java 压缩文件
1、controller
response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition","attachment; filename="+ DateUtil.getDate()+".zip"); ZipOutputStream out = new ZipOutputStream(response.getOutputStream()); Calendar c = Calendar.getInstance(); try { for(Map<String, Object> m : list){ String uniqueid = m.get("uniqueid").toString(); //解析uniqueid 1530405990.12257796 String arr[] = uniqueid.split("\\."); String datastr = arr[0]+"000"; c.setTimeInMillis(Long.parseLong(datastr)); String Y = String.valueOf(c.get(Calendar.YEAR)); String M; String D; int month = c.get(Calendar.MONTH) + 1; int day = c.get(Calendar.DAY_OF_MONTH); if(month<10){ M = "0"+month; }else{ M = String.valueOf(month); } if(day<10){ D = "0"+day; }else{ D = String.valueOf(day); } String url = Y+M+"/"+D+"/"+uniqueid+".WAV"; url = recordUrl+url; ZipUtils.doCompress(request,url, out,uniqueid+".WAV"); response.flushBuffer(); } } catch (Exception e) { e.printStackTrace(); }finally{ out.close(); }
2、ziputils
package com.nilecom.utils; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URISyntaxException; import java.net.URL; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.servlet.http.HttpServletRequest; import org.bouncycastle.util.encoders.UrlBase64; public class ZipUtils { private ZipUtils(){ } public static void doCompress(String srcFile, String zipFile) throws IOException { doCompress(new File(srcFile), new File(zipFile)); } /** * 文件压缩 * @param srcFile 目录或者单个文件 * @param zipFile 压缩后的ZIP文件 */ public static void doCompress(File srcFile, File zipFile) throws IOException { ZipOutputStream out = null; try { out = new ZipOutputStream(new FileOutputStream(zipFile)); doCompress(srcFile, out); } catch (Exception e) { throw e; } finally { out.close();//记得关闭资源 } } public static void doCompress(HttpServletRequest request,String filelName, ZipOutputStream out,String name) throws IOException, URISyntaxException{
//下面是网络路径 先从吧文件下载到本地在进行压缩 URL url = new URL(filelName); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); InputStream inputStream = conn.getInputStream(); byte[] buffer = new byte[1024*10]; int len = 0; String filePath = request.getSession().getServletContext().getRealPath("/")+"download\\"; File saveDir = new File(filePath); if(!saveDir.exists()){ saveDir.mkdir(); } File file = new File(filePath+name); FileOutputStream bos = new FileOutputStream(file); while((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } doCompress(file, out); bos.close(); inputStream.close(); } public static void doCompress(File file, ZipOutputStream out) throws IOException{ doCompress(file, out, ""); } public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException { if ( inFile.isDirectory() ) { File[] files = inFile.listFiles(); if (files!=null && files.length>0) { for (File file : files) { String name = inFile.getName(); if (!"".equals(dir)) { name = dir + "/" + name; } ZipUtils.doCompress(file, out, name); } } } else { ZipUtils.doZip(inFile, out, dir); } } public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException { String entryName = null; if (!"".equals(dir)) { entryName = dir + "/" + inFile.getName(); } else { entryName = inFile.getName(); } ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); int len = 0 ; byte[] buffer = new byte[1024]; FileInputStream fis = new FileInputStream(inFile); while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); out.flush(); } out.closeEntry(); fis.close(); } public static void main(String[] args) throws IOException { doCompress("D:/java/", "D:/java.zip"); } }