注只适配utf-8的场景,待完善!
压缩为zip文件
- 通过java程序输出文件
/*** 功能:压缩多个文件成一个zip文件* @param srcfile:源文件列表* @param zipfile:压缩后的文件*/
public static void zipFiles(File[] srcfile, File zipfile) {byte[] buf = new byte[1024];try {//ZipOutputStream类:完成文件或文件夹的压缩ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));for (int i = 0; i < srcfile.length; i++) {FileInputStream in = new FileInputStream(srcfile[i]);// 给列表中的文件单独命名out.putNextEntry(new ZipEntry(srcfile[i].getName()));int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}out.closeEntry();in.close();}out.close();System.out.println("压缩完成.");} catch (Exception e) {e.printStackTrace();}
}
2.1 通过浏览器下载文件(返回文件流) - 方式一
/*** 功能:压缩多个文件,输出压缩后的zip文件流* @param srcfile:源文件列表* @param zipFileName:压缩后的文件名* @return*/
@GetMapping(value = "/downzip")
public ResponseEntity<byte[]> zipFiles(File[] srcfile, String zipFileName) {byte[] buf = new byte[1024];// 获取输出流ByteArrayOutputStream bos = new ByteArrayOutputStream();try {// ZipOutputStream类:完成文件或文件夹的压缩ZipOutputStream out = new ZipOutputStream(bos);for (int i = 0; i < srcfile.length; i++) {// 此处可用任意其他输入流将FileInputStream取代,outputStream为其他步骤的输出流// ByteArrayInputStream in = new ByteArrayInputStream(outputStream.toByteArray());FileInputStream in = new FileInputStream(srcfile[i]);// 给列表中的文件单独命名out.putNextEntry(new ZipEntry(srcfile[i].getName()));int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}out.closeEntry();in.close();}out.close();bos.close();System.out.println("压缩完成.");} catch (Exception e) {e.printStackTrace();}// 设置http响应头HttpHeaders header = new HttpHeaders();header.add("Content-Disposition", "attachment;filename=" + zipFileName + ".zip");return new ResponseEntity<byte[]>(bos.toByteArray(), header, HttpStatus.CREATED);
}
2.2 通过浏览器下载文件(返回文件流) - 方式二
/*** 功能:压缩多个文件,输出压缩后的zip文件流* @param srcfile:源文件列表* @param zipFileName:压缩后的文件名* @param response: Http响应*/
@GetMapping(value = "/downzip")
public void zipFiles(File[] srcfile, String zipFileName, HttpServletResponse response) {byte[] buf = new byte[1024];// 获取输出流BufferedOutputStream bos = null;try {bos = new BufferedOutputStream(response.getOutputStream());} catch (IOException e) {e.printStackTrace();}try {response.reset(); // 重点突出// 不同类型的文件对应不同的MIME类型response.setContentType("application/x-msdownload");response.setCharacterEncoding("utf-8");response.setHeader("Content-disposition", "attachment;filename=" + zipFileName + ".zip");// ZipOutputStream类:完成文件或文件夹的压缩ZipOutputStream out = new ZipOutputStream(bos);for (int i = 0; i < srcfile.length; i++) {FileInputStream in = new FileInputStream(srcfile[i]);// 给列表中的文件单独命名out.putNextEntry(new ZipEntry(srcfile[i].getName()));int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}out.closeEntry();in.close();}out.close();bos.close();System.out.println("压缩完成.");} catch (Exception e) {e.printStackTrace();}
}
以上两种方式,都是以输出文件流的形式,通过浏览器端进行下载,不同的是,第一种将流直接通过接口返回,第二种也是比较常见的一种,将文件流通过response进行输出,两种方式均可,这里留存记录下。
解压缩zip文件
/*** 功能:解压缩* @param zipfile:需要解压缩的文件* @param descDir:解压后的目标目录*/
public static void unZipFiles(File zipfile, String descDir) {try {ZipFile zf = new ZipFile(zipfile);for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {ZipEntry entry = (ZipEntry) entries.nextElement();String zipEntryName = entry.getName();InputStream in = zf.getInputStream(entry);OutputStream out = new FileOutputStream(descDir + zipEntryName);byte[] buf1 = new byte[1024];int len;while ((len = in.read(buf1)) > 0) {out.write(buf1, 0, len);}in.close();out.close();System.out.println("解压缩完成.");}} catch (Exception e) {e.printStackTrace();}
}