whyclj
2019-08-16 a0f3a8ba6df4c1391188f9e16216acf04b0d1644
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.backup;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
/**
 * 文件操作
 */
public class ZipUtil {
 
    public static void main(String[] args) throws IOException {
        //copyFile("E:\\upload\\create\\1436144988371_JL33041594.xml","E:\\test\\upload");
        //deleteFile("E:\\test\\upload\\");
    }    
    /**
     *        移动 文件或者文件夹
     * @param oldPath
     * @param newPath
     * @throws IOException
     */
    public static void moveTo(String oldPath,String newPath) throws IOException {
        copyFile(oldPath,newPath);
        deleteFile(oldPath);
    }
 
    /**
               *     删除 文件或者文件夹
     * @param filePath
     */
    public static void deleteFile(String filePath){
        File file = new File(filePath);
        if (!file.exists()) {
            return;
        }
        if (file.isDirectory() ) {
            File[] list = file.listFiles();
 
            for (File f : list) {
                deleteFile(f.getAbsolutePath()) ;
            }
        }
        file.delete();
    }
 
    /**
     * 复制 文件或者文件夹
     * @param oldPath
     * @param newPath
     * @throws IOException
     */
    public static void  copyFile(String oldPath ,String newPath ) throws IOException {
        System.out.println("copy file from [" + oldPath + "] to [" + newPath +"]");
 
        File oldFile = new File(oldPath) ;
        if  (oldFile.exists())  {
 
            if(oldFile.isDirectory()){ // 如果是文件夹
                File newPathDir = new File(newPath);
                newPathDir.mkdirs();
                File[] lists = oldFile.listFiles() ;
                if(lists != null && lists.length > 0 ){
                    for (File file : lists) {
                        copyFile(file.getAbsolutePath(), newPath.endsWith(File.separator) ? newPath + file.getName() : newPath + File.separator + file.getName()) ;
                    }
                }
            }else {
                InputStream  inStream  =  new  FileInputStream(oldFile);  //读入原文件
                FileOutputStream  fs  =  new  FileOutputStream(newPath);
                write2Out(inStream ,fs) ;
                inStream.close();
            }
        }
    }
 
    /**
     * 重命名文件
     * @param file
     * @param name
     * @return
     */
    public static File renameFile(File file , String name ){
        String fileName = file.getParent()  + File.separator + name ;
        File dest = new File(fileName);
        file.renameTo(dest) ;
        return dest ;
    }
 
    /**
     * 压缩多个文件。
     * @param zipFileName 压缩输出文件名
     * @param files 需要压缩的文件
     * @return
     * @throws Exception
     */
    public static File createZip(String zipFileName, File... files) throws Exception {
        File outFile = new File(zipFileName) ;
        ZipOutputStream out = null;
        BufferedOutputStream bo = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(outFile));
            bo = new BufferedOutputStream(out);
 
            for (File file : files) {
                zip(out, file, file.getName(), bo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bo.close();
            } finally {
                out.close(); // 输出流关闭
            }
        }
        return outFile;
    }
 
    /**
     *
     * @param zipFileName 压缩输出文件名
     * @param inputFile 需要压缩的文件
     * @return
     * @throws Exception
     */
    public static File createZip(String zipFileName, File inputFile) throws Exception {
        File outFile = new File(zipFileName) ;
        ZipOutputStream out = null;
        BufferedOutputStream bo = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(outFile));
            bo = new BufferedOutputStream(out);
            zip(out, inputFile, inputFile.getName(), bo);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bo.close();
            } finally {
                out.close(); // 输出流关闭
            }
        }
        return outFile;
    }
 
    private static void zip(ZipOutputStream out, File f, String base,BufferedOutputStream bo) throws Exception { // 方法重载
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            if ( fl == null ||  fl.length == 0) {
                out.putNextEntry(new ZipEntry(base + "/")); // 创建创建一个空的文件夹
            }else{
                for (int i = 0; i < fl.length; i++) {
                    zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 递归遍历子文件夹
                }
            }
 
        } else {
            out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入 base 文件
            //System.out.println(base);
            BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f));
 
            try {
                write2Out(bi,out) ;
            } catch (IOException e) {
                //Ignore
            }finally {
                bi.close();// 输入流关闭
            }
        }
    }
 
    private static void write2Out(InputStream input , OutputStream out) throws IOException {
        byte[] b = new byte[1024];
        int c = 0 ;
        while ( (c = input.read(b)) != -1 ) {
            out.write(b,0,c);
            out.flush();
        }
        out.flush();
    }
}