package com.vxdata.activity.utils; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import java.io.*; import java.util.base64; import java.util.HashMap; import java.util.linkedHashMap; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; @Slf4j public class ZipUtils { // 要压缩的文件格式 public static final String FORMAT = "zip"; public static final String FILE_UPLOAD_SEPARATOR = "/"; public static final String FILE_UPLOAD_POINT = "."; public static void zipLocal(String path, String outputPath) throws IOException { zipFileTree(new File(path), FORMAT, outputPath); } public static void unZipLocal(String zipFilePath, String desDirectory) throws Exception { unzip(zipFilePath, desDirectory); } private static void zipFileTree(File sourceFile, String format, String outputPath) throws IOException { ZipOutputStream zipOutputStream = null; try { String zipFileName; if (sourceFile.isDirectory()) { // 文件夹 zipFileName = outputPath + File.separator + sourceFile.getName() + "." + format; } else { // 单个文件 zipFileName = outputPath + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + "." + format; } // 压缩输出流 zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFileName)); // 递归压缩内部文件 zip(sourceFile, zipOutputStream, ""); } finally { if (null != zipOutputStream) { // 关闭流 try { zipOutputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } private static void zip(File file, ZipOutputStream zipOutputStream, String relativePath) throws IOException { FileInputStream fileInputStream = null; try { // 当前为文件夹 if (file.isDirectory()) { // 当前文件夹下的所有文件 File[] list = file.listFiles(); if (null != list) { // 计算当前的相对路径 relativePath += (relativePath.length() == 0 ? "" : File.separator) + file.getName(); // 递归压缩每个文件 for (File f : list) { zip(f, zipOutputStream, relativePath); } } } else { // 压缩文件 // 计算文件的相对路径 relativePath += (relativePath.length() == 0 ? "" : File.separator) + file.getName(); // 写入单个文件 zipOutputStream.putNextEntry(new ZipEntry(relativePath)); fileInputStream = new FileInputStream(file); int readLen; byte[] buffer = new byte[1024]; while ((readLen = fileInputStream.read(buffer)) != -1) { zipOutputStream.write(buffer, 0, readLen); } zipOutputStream.closeEntry(); } } finally { // 关闭流 if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } public static void unzip(String zipFilePath, String desDirectory) throws Exception { unzipByFile(new File(zipFilePath), desDirectory); } public static void unzipByFile(File zipFile, String desDirectory) throws Exception{ File desDir = new File(desDirectory); if (!desDir.exists()) { boolean mkdirSuccess = desDir.mkdir(); if (!mkdirSuccess) { throw new Exception("创建解压目标文件夹失败"); } } // 读入流 ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile)); // 遍历每一个文件 ZipEntry zipEntry = zipInputStream.getNextEntry(); while (zipEntry != null) { if (zipEntry.isDirectory()) { // 文件夹 String unzipFilePath = desDirectory + File.separator + zipEntry.getName(); // 直接创建 mkdir(new File(unzipFilePath)); } else { // 文件 String unzipFilePath = desDirectory + File.separator + zipEntry.getName(); File file = new File(unzipFilePath); // 创建父目录 mkdir(file.getParentFile()); // 写出文件流 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(unzipFilePath)); byte[] bytes = new byte[1024]; int readLen; while ((readLen = zipInputStream.read(bytes)) != -1) { bufferedOutputStream.write(bytes, 0, readLen); } bufferedOutputStream.close(); } zipInputStream.closeEntry(); zipEntry = zipInputStream.getNextEntry(); } zipInputStream.close(); } private static void mkdir(File file) { if (null == file || file.exists()) { return; } mkdir(file.getParentFile()); file.mkdir(); } public static Map提供前端下载文件readZip2Map(File file) throws Exception { Map map = new HashMap<>(); Map photos = new linkedHashMap<>(); try(InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zipInputStream = new ZipInputStream(inputStream); ) { ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { if (zipEntry.isDirectory()) { continue; } if ("txt".equals(getSuffixName(zipEntry.getName()))) { StringBuilder sb = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipInputStream)); try { String line = null; while ((line = bufferedReader.readLine()) != null) { sb.append(line); } JSONObject json = JSONObject.parseObject(sb.toString()); map.put("json", json); }catch (Exception e){ e.printStackTrace(); } } else { String name = getFileName(zipEntry.getName()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedInputStream bis = new BufferedInputStream(zipInputStream); try { int size; byte[] bytes = new byte[1024 * 4]; while ((size = bis.read(bytes, 0, bytes.length)) > 0) { baos.write(bytes, 0, size); } baos.flush(); String base64 = asbase64(baos); photos.put(name,base64); }catch (Exception e){ e.printStackTrace(); }finally { try{ if(baos!=null){ baos.close(); } }catch (Exception e){} } } } if(photos.size()>0){ map.put("photos", photos); } zipInputStream.closeEntry(); }catch (Exception e){ e.printStackTrace(); } return map; } private static String getSuffixName(String fileName) { String suffix = null; String originalFilename = fileName; if (StringUtils.isNotBlank(fileName)) { if (fileName.contains(FILE_UPLOAD_SEPARATOR)) { fileName = fileName.substring(fileName.lastIndexOf(FILE_UPLOAD_SEPARATOR) + 1); } if (fileName.contains(FILE_UPLOAD_POINT)) { suffix = fileName.substring(fileName.lastIndexOf(FILE_UPLOAD_POINT) + 1); } else { if (log.isErrorEnabled()) { log.error("filename error without suffix : {}", originalFilename); } } } return suffix; } private static String getFileName(String path) { int index = path.lastIndexOf("/"); if(index==-1) { return path; }else { return path.substring(index+1); } } private static String asbase64(ByteArrayOutputStream baos) { //base64Encoder encoder = new base64Encoder(); byte[] data = baos.toByteArray(); return base64.getEncoder().encodeToString(data); } }
public void downloadZip(HttpServletResponse response, String zipFileName) { // zipName为上一步文件打包zip时传入的zipName File zipFile = new File(zipFileName); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName()); FileInputStream fileInputStream = null; OutputStream outputStream = null; try { outputStream = response.getOutputStream(); fileInputStream = new FileInputStream(zipFile); byte[] bufs = new byte[1024 * 10]; int read = 0; while ((read = fileInputStream.read(bufs, 0, 1024 * 10)) != -1) { outputStream.write(bufs, 0, read); } fileInputStream.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { // 删除压缩包 File file = new File(zipFileName); file.delete(); if (fileInputStream != null) { fileInputStream.close(); } if (outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }将InputStream写入本地文件
****.txt等 * @param input 输入流 * @throws IOException */ public static void writeToLocal(String destination, InputStream input) throws IOException { int len = -1; byte[] bytes = new byte[1024 * 4]; try (FileOutputStream fos = new FileOutputStream(destination)) { while ((len = input.read(bytes)) != -1) { fos.write(bytes, 0, len); } fos.flush(); } catch (Exception e) { e.printStackTrace(); } }把多个文件打包生压缩包传给前端
可以是文件夹, 但是文件夹里面如果包含文件夹,只会压缩最里层文件夹
package com.vxdata.activity.utils; import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class DownloadZipUtil { public static void downloadZipFiles(HttpServletResponse response, ListsrcFiles, String zipFileName) { try { response.reset(); // 重点突出 response.setCharacterEncoding("UTF-8"); response.setContentType("application/zip"); // 对文件名进行编码处理中文问题 zipFileName = new String(zipFileName.getBytes(), StandardCharsets.UTF_8); response.setHeader("Content-Disposition", "attachment;filename=" + zipFileName); // --设置成这样可以不用保存在本地,再输出, 通过response流输出,直接输出到客户端浏览器中。 ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); zipFile(srcFiles, zos); } catch (IOException e) { e.printStackTrace(); } } private static void zipFile(List filePaths, ZipOutputStream zos) { //设置读取数据缓存大小 byte[] buffer = new byte[4096]; try { //循环读取文件路径集合,获取每一个文件的路径 for (String filePath : filePaths) { File inputFile = new File(filePath); //判断文件是否存在 if (inputFile.exists()) { //判断是否属于文件,还是文件夹 if (inputFile.isFile()) { //创建输入流读取文件 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile)); //将文件写入zip内,即将文件进行打包 zos.putNextEntry(new ZipEntry(inputFile.getName())); //写入文件的方法,同上 int size = 0; //设置读取数据缓存大小 while ((size = bis.read(buffer)) > 0) { zos.write(buffer, 0, size); } //关闭输入输出流 zos.closeEntry(); bis.close(); } else { //如果是文件夹,则使用穷举的方法获取文件,写入zip File[] files = inputFile.listFiles(); List filePathsTem = new ArrayList (); for (File fileTem : files) { filePathsTem.add(fileTem.toString()); } zipFile(filePathsTem, zos); } } } } catch (IOException e) { e.printStackTrace(); } finally { if (null != zos) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)