1 import java.io.*;2 importjava.nio.channels.FileChannel;3 importjava.nio.file.Files;4 importjava.nio.file.Path;5 importjava.nio.file.Paths;6 importjava.nio.file.attribute.PosixFilePermission;7 importjava.util.ArrayList;8 importjava.util.Enumeration;9 importjava.util.HashSet;10 importjava.util.List;11 importjava.util.Set;12 importjava.util.zip.ZipEntry;13 importjava.util.zip.ZipFile;14
15 importorg.springframework.web.multipart.MultipartFile;16
17 importsg.com.mha.ummi.common.util.LogUtils;18
19
20 public classFileUtil {21
22 public static voidclearFiles(String workspaceRootPath) {23 File file = newFile(workspaceRootPath);24 deleteFile(file);25 }26
27 public static voiddeleteFile(File file) {28 if(file.exists()) {29 if(file.isDirectory()) {30 File[] files =file.listFiles();31 for (int i = 0; i < files.length; i++) {32 deleteFile(files[i]);33 }34 }35 }36 file.delete();37 }38
39 public static void fileWrite(String str, String fileNamePath) throwsIOException {40 FileWriter writer = null;41 try{42 File file = newFile(fileNamePath);43 if (!file.getParentFile().exists()) {44 file.getParentFile().mkdirs();45 file.createNewFile();46 }47 writer = new FileWriter(file, true);48 writer.write(str + System.getProperty("line.separator"));49
50 } catch(IOException e) {51 LogUtils.error(e.getMessage());52 } finally{53 if (writer != null) {54 writer.close();55 }56 }57 }58
59 public static void changePermission(File dirFile, int mode) throwsIOException {60 char[] modes =Integer.toOctalString(mode).toCharArray();61 if (modes.length != 3) {62 return;63 }64 Set perms = new HashSet();65 switch (modes[0]) {66 case '1':67 perms.add(PosixFilePermission.OWNER_EXECUTE);68 break;69 case '2':70 perms.add(PosixFilePermission.OWNER_WRITE);71 break;72 case '4':73 perms.add(PosixFilePermission.OWNER_READ);74 break;75 case '5':76 perms.add(PosixFilePermission.OWNER_READ);77 perms.add(PosixFilePermission.OWNER_EXECUTE);78 break;79 case '6':80 perms.add(PosixFilePermission.OWNER_READ);81 perms.add(PosixFilePermission.OWNER_WRITE);82 break;83 case '7':84 perms.add(PosixFilePermission.OWNER_READ);85 perms.add(PosixFilePermission.OWNER_WRITE);86 perms.add(PosixFilePermission.OWNER_EXECUTE);87 break;88
89 default:90 break;91 }92 switch (modes[1]) {93 case '1':94 perms.add(PosixFilePermission.GROUP_EXECUTE);95 break;96 case '2':97 perms.add(PosixFilePermission.GROUP_WRITE);98 break;99 case '4':100 perms.add(PosixFilePermission.GROUP_READ);101 break;102 case '5':103 perms.add(PosixFilePermission.GROUP_READ);104 perms.add(PosixFilePermission.GROUP_EXECUTE);105 break;106 case '6':107 perms.add(PosixFilePermission.GROUP_READ);108 perms.add(PosixFilePermission.GROUP_WRITE);109 break;110 case '7':111 perms.add(PosixFilePermission.GROUP_READ);112 perms.add(PosixFilePermission.GROUP_WRITE);113 perms.add(PosixFilePermission.GROUP_EXECUTE);114 break;115 default:116 break;117 }118 switch (modes[2]) {119 case '1':120 perms.add(PosixFilePermission.OTHERS_EXECUTE);121 break;122 case '2':123 perms.add(PosixFilePermission.OTHERS_WRITE);124 break;125 case '4':126 perms.add(PosixFilePermission.OTHERS_READ);127 break;128 case '5':129 perms.add(PosixFilePermission.OTHERS_EXECUTE);130 perms.add(PosixFilePermission.OTHERS_READ);131 break;132 case '6':133 perms.add(PosixFilePermission.OTHERS_READ);134 perms.add(PosixFilePermission.OTHERS_WRITE);135 break;136 case '7':137 perms.add(PosixFilePermission.OTHERS_EXECUTE);138 perms.add(PosixFilePermission.OTHERS_READ);139 perms.add(PosixFilePermission.OTHERS_WRITE);140 break;141 default:142 break;143 }144
145 try{146 Path path =Paths.get(dirFile.getAbsolutePath());147 Files.setPosixFilePermissions(path, perms);148 } catch(Exception e) {149 e.printStackTrace();150 }151 }152
153 public staticFile mkFile(String fileName) {154 File f = newFile(fileName);155 try{156 if(f.exists()) {157 f.delete();158 }159 f.createNewFile();160 } catch(IOException e) {161 e.printStackTrace();162 }163 returnf;164 }165
166
167 public static void copyDirAndFile(String oldPath, String newPath) throwsIOException {168 if (!(newFile(newPath)).exists()) {169 (newFile(newPath)).mkdir();170 }171 File file = newFile(oldPath);172 //file name list
173 String[] filePaths =file.list();174 for(String filePath : filePaths) {175 String oldFullPath = oldPath + file.separator +filePath;176 String newFullPath = newPath + file.separator +filePath;177 File oldFile = newFile(oldFullPath);178 File newFile = newFile(newFullPath);179 if(oldFile.isDirectory()) {180 copyDirAndFile(oldFullPath, newFullPath);181 } else if(oldFile.isFile()) {182 copyFile(oldFile, newFile);183 }184 }185 }186
187 public static void copyFile(File source, File dest) throwsIOException {188 FileChannel inputChannel = null;189 FileChannel outputChannel = null;190 try{191 inputChannel = newFileInputStream(source).getChannel();192 outputChannel = newFileOutputStream(dest).getChannel();193 outputChannel.transferFrom(inputChannel, 0, inputChannel.size());194 } finally{195 inputChannel.close();196 outputChannel.close();197 }198 }199
200 /**
201 *@authorpanchaoyuan202 *@paramsrcFile Unzipped file203 *@paramdestDirPath Unzipped destination folder204 *@throwsRuntimeException205 *@throwsIOException206 */
207 public static void unZip(MultipartFile srcFile, String destDirPath,String savePath) throwsRuntimeException, IOException {208 long startTime =System.currentTimeMillis();209
210 File file = null;211 InputStream ins =srcFile.getInputStream();212 file=new File(savePath+srcFile.getOriginalFilename());213 LogUtils.info("MultipartFile transform to File,MultipartFile name:"+srcFile.getOriginalFilename());214 inputStreamToFile(ins, file);215
216 if (!file.exists()) {217 throw new RuntimeException(file.getPath() + ",file is not found");218 }219 ZipFile zipFile = null;220 try{221 zipFile = newZipFile(file);222 Enumeration> entries =zipFile.entries();223 while(entries.hasMoreElements()) {224 ZipEntry entry =(ZipEntry) entries.nextElement();225 LogUtils.info("zipFile context name:"+entry.getName());226 if(entry.isDirectory()) {227 String dirPath = destDirPath + File.separator+entry.getName();228 File dir = newFile(dirPath);229 dir.mkdirs();230 }else{231 File targetFile = new File(destDirPath + File.separator +entry.getName());232 targetFile.setExecutable(true);233 if(!targetFile.getParentFile().exists()){234 targetFile.getParentFile().mkdirs();235 }236 targetFile.createNewFile();237 InputStream is =zipFile.getInputStream(entry);238 FileOutputStream fos = newFileOutputStream(targetFile);239 intlen;240 byte[] buf = new byte[1024];241 while ((len = is.read(buf)) != -1) {242 fos.write(buf, 0, len);243 }244 fos.close();245 is.close();246 }247 }248 long endTime =System.currentTimeMillis();249 LogUtils.info("unZip time-->" + (endTime - startTime) + " ms");250 }catch(Exception e) {251 throw new RuntimeException("unzip error from FileUtil", e);252 } finally{253 if(zipFile != null){254 try{255 zipFile.close();256 } catch(IOException e) {257 e.printStackTrace();258 }259 }260
261 //MultipartFile change to file may create a temp file in the project root folder(delete the temp file)
262 File del = newFile(file.toURI());263 del.delete();264 }265 }266
267 /**
268 * MultipartFile changed to File269 *@authorpanchaoyuan270 *@return
271 */
272 private static voidinputStreamToFile(InputStream ins, File file) {273 try{274 OutputStream os = newFileOutputStream(file);275 int bytesRead = 0;276 byte[] buffer = new byte[8192];277 while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {278 os.write(buffer, 0, bytesRead);279 }280 os.close();281 ins.close();282 LogUtils.info("MultipartFile transform to File completed!");283 }catch(Exception e) {284 e.printStackTrace();285 }286 }287
288 /**
289 *@authorpanchaoyuan290 */
291 public List getSubFiles(String desFile,ListfileList) {292 File file = newFile(desFile);293 File[] files =file.listFiles();294 for(File fileIndex : files) {295 if (!fileIndex.exists()) {296 throw new NullPointerException("Cannot find " +fileIndex);297 } else if(fileIndex.isFile()) {298 fileList.add(fileIndex);299 } else{300 if(fileIndex.isDirectory()) {301 getSubFiles(fileIndex.getAbsolutePath(),fileList);302 }303 }304 }305 returnfileList;306 }307
308
309 }