package com.sim.newlook; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.junit.Test; import com.aliyun.oss.model.ListObjectsRequest; import com.fasterxml.jackson.annotation.JsonIgnore; import com.sim.newlook.data.domain.Team; public class ZipFileTest { static final int BUFFER = 8192; @Test public void test() { try { zipFile("D:\task001", "001", "D:\test"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // 目录此时为空,可以删除 return dir.delete(); } public void zipFile(String taskId, String teamId, String tempPath) throws IOException { Node root = getTeamNode(teamId); loopfile(taskId, root, tempPath); ZipOutputStream out = null; File file = new File(tempPath + File.separator + root.getName()); try { FileOutputStream fileOutputStream = new FileOutputStream(new File(tempPath+File.separator+"test.zip")); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); out = new ZipOutputStream(cos); String basedir = ""; compress(file, out, basedir); out.close(); } catch (Exception e) { throw new RuntimeException(e); } deleteDir(file); } private void compress(File file, ZipOutputStream out, String basedir) { /* 判断是目录还是文件 */ if (file.isDirectory()) { System.out.println("压缩:" + basedir + file.getName()); this.compressDirectory(file, out, basedir); } else { System.out.println("压缩:" + basedir + file.getName()); this.compressFile(file, out, basedir); } } private void compressDirectory(File dir, ZipOutputStream out, String basedir) { if (!dir.exists()) return; File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { compress(files[i], out, basedir + dir.getName() + "/"); } } private void compressFile(File file, ZipOutputStream out, String basedir) { if (!file.exists()) { return; } try { BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file)); ZipEntry entry = new ZipEntry(basedir + file.getName()); out.putNextEntry(entry); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } bis.close(); } catch (Exception e) { throw new RuntimeException(e); } } private void loopfile(String taskId, Node root, String path) throws IOException { String folderName = root.getName(); String prefix = root.getId(); List<Node> nodes = root.getChilds(); System.out.println(path + File.separator + folderName); File tempFile = new File(path + File.separator + folderName); if (!tempFile.exists()) { tempFile.mkdirs(); } if (nodes == null) { List<ObjectSummary> objectSummaries = listObjects(new ListObjectsRequest(taskId).withPrefix(prefix)); for (ObjectSummary objectSummary : objectSummaries) { InputStream inputStream = getObject(objectSummary.bucket, objectSummary.key); FileOutputStream fos = new FileOutputStream(new File(tempFile.getPath() + File.separator + objectSummary.key)); byte[] b = new byte[BUFFER]; while ((inputStream.read(b)) != -1) { fos.write(b); } inputStream.close(); fos.close(); } } else { for (Node node : nodes) { loopfile(taskId, node, tempFile.getPath()); } } } private List<ObjectSummary> listObjects(ListObjectsRequest listObjectRequest) { File file = new File(listObjectRequest.getBucketName()); List<ObjectSummary> objectSummaries = new ArrayList<ZipFileTest.ObjectSummary>(); if (file.exists()) { File[] files = file.listFiles(); if (files != null) { for (File f : files) { if (f.getName().startsWith(listObjectRequest.getPrefix())) { objectSummaries.add(new ObjectSummary(listObjectRequest.getBucketName(), f.getName())); } } } } return objectSummaries; } public InputStream getObject(String bucket, String key) throws FileNotFoundException { return new FileInputStream(new File(bucket + File.separator + key)); } public Node getTeamNode(String teamId) { List<Team> teams = getChildTeams(teamId); Node root = null; Map<String, Node> nodeMap = new HashMap<String, Node>(); Map<String, List<Node>> siblings = new HashMap<String, List<Node>>(); for (Team department : teams) { String parentId = department.getParentTeam() == null ? null : department.getParentTeam().getId(); Node node2Create = new Node(department.getId(), department.getName(), parentId); nodeMap.put(department.getId(), node2Create); if (node2Create.getParentid() == null) { root = node2Create; } } // Find parent for each node if any for (Node node : nodeMap.values()) { if (node.getParentid() != null) { node.setParent(nodeMap.get(node.getParentid())); if (siblings.get(node.getParentid()) == null) { siblings.put(node.getParentid(), new ArrayList<Node>()); } siblings.get(node.getParentid()).add(node); } } // Find children for each node if any for (Entry<String, List<Node>> entry : siblings.entrySet()) { nodeMap.get(entry.getKey()).setChilds(entry.getValue()); } System.out.println(root); return root; } public List<Team> getChildTeams(String teamId) { List<Team> teams = new ArrayList<>(); // create team for test Team team1 = new Team(); team1.setId("001"); team1.setName("team01"); Team team2 = new Team(); team2.setId("002"); team2.setName("team02"); team2.setParentTeam(team1); Team team3 = new Team(); team3.setId("003"); team3.setName("team03"); team3.setParentTeam(team1); Team team4 = new Team(); team4.setId("004"); team4.setName("team04"); team4.setParentTeam(team2); Team team5 = new Team(); team5.setId("005"); team5.setName("team05"); team5.setParentTeam(team2); Team team6 = new Team(); team6.setId("006"); team6.setName("team06"); team6.setParentTeam(team3); Team team7 = new Team(); team7.setId("007"); team7.setName("team07"); team7.setParentTeam(team3); teams.add(team7); teams.add(team6); teams.add(team5); teams.add(team4); teams.add(team3); teams.add(team2); teams.add(team1); return teams; } class ObjectSummary { private String bucket; private String key; public ObjectSummary(String bucket, String key) { super(); this.bucket = bucket; this.key = key; } } class Node { @JsonIgnore private Node parent; public String id; public String name; public String parentid; private List<Node> childs; public Node() { super(); } public Node(String id, String name, String parentid) { super(); this.id = id; this.parentid = parentid; this.name = name; } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } public List<Node> getChilds() { return childs; } public void setChilds(List<Node> childs) { this.childs = childs; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getParentid() { return parentid; } public void setParentid(String parentid) { this.parentid = parentid; } @Override public String toString() { return "Node {id=" + id + ", name=" + name + ", childs=" + childs + "}"; } } }
处理后压缩文件的文件目录结构如下
team01
-team02
-team04
-team05
-team03
-team06
-team07