xiaoyong931011
2022-07-07 bc43681f185af1edf833cf6c94833cb1cdd44a8e
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.xcong.farmer.cms.common.utils;
 
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
 
/**
 * @author wzy
 * @date 2022-07-04
 **/
public class FileUtils {
 
    public static String path(String path) {
        if (!path.endsWith("/")) {
            return path + "/";
        }
 
        return path;
    }
 
    public static String path(String path, String fileName) {
        File file = new File(path);
        if (!file.isDirectory()){
            return "";
        }
 
        String dir = path(path);
        return dir + fileName;
    }
 
    public static void zipUpload(String zipPath, String templateDir) throws IOException {
        FileInputStream fis = new FileInputStream(zipPath);
        ZipInputStream zipIs = new ZipInputStream(fis);
 
        ZipEntry ze = null;
        try {
            while ((ze = zipIs.getNextEntry()) != null) {
                File zfile = new File(path(templateDir, ze.getName()));
                if (ze.isDirectory()) {
                    if (!zfile.exists()) {
                        zfile.mkdirs();
                    }
                    zipIs.closeEntry();
                } else {
                    byte[] data = new byte[1024];
                    FileOutputStream fos = new FileOutputStream(zfile);
                    int i = 0;
 
                    while ((i = zipIs.read(data)) != -1) {
                        fos.write(data, 0, i);
                    }
                    zipIs.closeEntry();
                    fos.close();
                }
            }
 
            fis.close();
            zipIs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    public static void upload(File file, String dir) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
 
            for (File dirFile : files) {
                upload(dirFile, path(dir, dirFile.getName()));
            }
        } else {
            outputFile(file, dir);
        }
    }
 
    public static void outputFile(File file, String outputDir) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
 
            int count;
            int buffer = 1024;
            byte[] dataByte = new byte[buffer];
 
            FileOutputStream out = new FileOutputStream(path(outputDir, file.getName()));
 
            BufferedOutputStream bos = new BufferedOutputStream(out, buffer);
            while((count = bis.read(dataByte, 0, buffer)) != -1) {
                bos.write(dataByte, 0 ,count);
            }
 
            bos.flush();
            bos.close();
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}