一、 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public void folderToZip (String sourceDirectory, String zipDirectoryPath, String zipName) { File src = new File(sourceDirectory); ZipOutputStream zos = null ; if (src == null || !src.exists() || !src.isDirectory()) { throw new RuntimeException("压缩源目录不存在或非目录!" + sourceDirectory); } File destdir = new File(zipDirectoryPath); if (!destdir.exists()) { destdir.mkdirs(); } File zipfile = new File(new File(zipDirectoryPath).getAbsolutePath() + "/" + zipName); File[] srclist = src.listFiles(); if (srclist == null || srclist.length == 0 ) { throw new RuntimeException("源目录内容为空,无需压缩下载!" + sourceDirectory); } try { zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipfile))); compress(zos, src, src.getName()); zos.close(); } catch (FileNotFoundException e) { throw new RuntimeException("压缩目标文件不存在!" + e.getMessage()); } catch (IOException e) { throw new RuntimeException("压缩文件IO异常!" + e.getMessage()); } finally { if (zos != null ) { try { zos.close(); } catch (Exception e) { } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 private void compress (ZipOutputStream zos, File src, String name) throws IOException { if (src == null || !src.exists()) { return ; } if (src.isFile()) { byte [] bufs = new byte [10240 ]; ZipEntry zentry = new ZipEntry(name); zos.putNextEntry(zentry); FileInputStream in = new FileInputStream(src); BufferedInputStream bin = new BufferedInputStream(in, 10240 ); int readcount = 0 ; while ( (readcount = bin.read(bufs, 0 , 10240 )) != -1 ) { zos.write(bufs, 0 , readcount); } zos.closeEntry(); bin.close(); } else { File[] fs = src.listFiles(); if (fs == null || fs.length == 0 ) { zos.putNextEntry(new ZipEntry(name + File.separator )); zos.closeEntry(); return ; } for (File f : fs) { compress(zos, f, name + File.separator + f.getName()); } } }
二、 为了防止忘记用到的类都是哪个包下的,附带包目录,可供参考(包含一些无关的,懒得删):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import org.apache.commons.io.FileUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.io.*;import java.lang.reflect.Type;import java.time.LocalDate;import java.util.*;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;
三、
如果还想用来作为网络传输用的,需要对字节流进行一点包装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public ResponseEntity<byte []> obtainZipDirEntity(String directoryAbsolutePath, String zipName) throws IOException { File dir = new File(directoryAbsolutePath); System.out.println("Check dir real path: " +dir); HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment" , zipName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); File tempDirPath = new File(new File("" ).getAbsolutePath() + "/temp" ); if (!tempDirPath.exists()) { tempDirPath.mkdirs(); } String fileName = zipName + ".zip" ; this .folderToZip(directoryAbsolutePath, tempDirPath.getAbsolutePath(), fileName); File tempFilePath = new File(tempDirPath.getAbsolutePath() + "/" + fileName); return new ResponseEntity<byte []>(FileUtils.readFileToByteArray(tempFilePath), headers, HttpStatus.CREATED); }