MCPcopy Create free account
hub / github.com/0x727/DropLabTools / FileZipUtils

Class FileZipUtils

src/main/java/com/droplab/Utils/FileZipUtils.java:16–145  ·  view source on GitHub ↗

用于创建特殊压缩包

Source from the content-addressed store, hash-verified

14 * 用于创建特殊压缩包
15 */
16public class FileZipUtils {
17 private int depth = 0; //目录穿越深度;
18 private String platform = "win"; //windows或者linux
19 private String dir;
20
21 public FileZipUtils(int depth, String platform) {
22 this.depth = depth;
23 this.platform = platform;
24 }
25
26 private void handlerFile(ZipOutputStream zip, File file, String path, boolean flag) throws Exception {
27 if (platform.equals("windows")) {
28 dir = "..\\";
29 if (!path.equals("") && path != null && !path.endsWith("\\")) {
30 path += "\\";
31 }
32 } else if (platform.equals("linux")) {
33 dir = "../";
34 if (!path.equals("") && path != null && !path.endsWith("/")) {
35 path += "/";
36 }
37 }
38 String tmpPath = "";
39 if (flag) {
40 for (int i = 0; i < depth; i++) {
41 tmpPath += dir;
42 }
43 }
44 String name = file.getName();
45 String filePath = tmpPath + path + name;
46 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
47 ZipEntry entry = new ZipEntry(filePath);
48 zip.putNextEntry(entry);
49 zip.write(FileUtils.readFileToByteArray(file));
50 IOUtils.closeQuietly(bis);
51 zip.flush();
52 zip.closeEntry();
53 }
54
55 public File createZip(HashMap<String, String> fileMap, String path) throws Exception {
56 try {
57 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
58 ZipOutputStream zip = new ZipOutputStream(outputStream);
59 //将目标文件打包成zip导出
60 Iterator<String> iterator = fileMap.keySet().iterator();
61 while (iterator.hasNext()) {
62 String next = iterator.next();
63 String s = fileMap.get(next);
64 boolean flag = true; //用于判断当前这个文件是否需要目录穿越
65 String tPath = s; //
66 if (!s.equals("") && s.contains("|")) {
67 String[] split = s.split("\\|");
68 tPath=split[0];
69 if (split[1].equals("false")) {
70 flag = false;
71 }
72 }
73 handlerFile(zip, new File(next), tPath, flag);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected