FileUtils.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. package com.fdkk.sxz.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.github.junrar.Archive;
  4. import com.github.junrar.rarfile.FileHeader;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.tools.zip.ZipEntry;
  7. import org.apache.tools.zip.ZipFile;
  8. import org.springframework.util.ResourceUtils;
  9. import sun.misc.BASE64Decoder;
  10. import java.io.*;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. import java.net.URLDecoder;
  14. import java.util.*;
  15. @Slf4j
  16. public class FileUtils {
  17. //文件路径+名称
  18. private static String fileNameTemp;
  19. public static void uploadImg(String path, String base64Data)
  20. throws Exception {
  21. byte[] bt = null;
  22. try {
  23. BASE64Decoder decoder = new BASE64Decoder();
  24. if (base64Data.startsWith("data:image/png;base64,")) {
  25. bt = decoder.decodeBuffer(base64Data.replace("data:image/png;base64,", ""));
  26. } else if (base64Data.startsWith("data:image/jpeg;base64,")) {
  27. bt = decoder.decodeBuffer(base64Data.replace("data:image/jpeg;base64,", ""));
  28. } else if (base64Data.startsWith("data:image/bmp;base64,")) {
  29. bt = decoder.decodeBuffer(base64Data.replace("data:image/bmp;base64,", ""));
  30. } else {
  31. return;
  32. }
  33. writeBinary(bt, path);
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. public static void writeBinary(byte[] buf, String filePath) throws Exception {
  39. File fout = new File(filePath);
  40. if (!fout.getParentFile().exists()) {
  41. fout.getParentFile().mkdirs();
  42. }
  43. FileOutputStream fos = new FileOutputStream(fout);
  44. ByteArrayInputStream stream = new ByteArrayInputStream(buf);
  45. BufferedOutputStream bos = new BufferedOutputStream(fos);//设置输出路径
  46. BufferedInputStream bis = new BufferedInputStream(stream);
  47. int b = -1;
  48. while ((b = bis.read()) != -1) {
  49. bos.write(b);
  50. }
  51. bis.close();
  52. bos.close();
  53. }
  54. public static boolean createDir(String destDirName) {
  55. File dir = new File(destDirName);
  56. if (dir.exists()) {
  57. System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");
  58. return false;
  59. }
  60. if (!destDirName.endsWith(File.separator)) {
  61. destDirName = destDirName + File.separator;
  62. }
  63. //创建目录
  64. if (dir.mkdirs()) {
  65. System.out.println("创建目录" + destDirName + "成功!");
  66. return true;
  67. } else {
  68. System.out.println("创建目录" + destDirName + "失败!");
  69. return false;
  70. }
  71. }
  72. /**
  73. * 创建文件
  74. * @param fileName 文件名称
  75. * @param fileContent 文件内容
  76. * @return 是否创建成功,成功则返回true
  77. */
  78. public static boolean createFile(String path, String fileName,String fileContent){
  79. Boolean bool = false;
  80. fileNameTemp = path+fileName+".json";//文件路径+名称+文件类型
  81. File file = new File(fileNameTemp);
  82. try {
  83. File folder = new File(path);
  84. if (!folder.exists()){
  85. folder.mkdirs();
  86. }
  87. //如果文件不存在,则创建新的文件
  88. if(!file.exists()){
  89. file.createNewFile();
  90. bool = true;
  91. System.out.println("success create file,the file is "+ fileNameTemp);
  92. //创建文件成功后,写入内容到文件里
  93. writeFileContent(fileNameTemp, fileContent);
  94. }
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. return bool;
  99. }
  100. /**
  101. * 向文件中写入内容
  102. * @param filePath 文件路径与名称
  103. * @param newstr 写入的内容
  104. * @return
  105. * @throws IOException
  106. */
  107. public static boolean writeFileContent(String filePath, String newstr) throws IOException{
  108. Boolean bool = false;
  109. String filein = newstr+"\r\n";//新写入的行,换行
  110. String temp = "";
  111. FileInputStream fis = null;
  112. InputStreamReader isr = null;
  113. BufferedReader br = null;
  114. FileOutputStream fos = null;
  115. PrintWriter pw = null;
  116. try {
  117. File file = new File(filePath);//文件路径(包括文件名称)
  118. //将文件读入输入流
  119. fis = new FileInputStream(file);
  120. isr = new InputStreamReader(fis);
  121. br = new BufferedReader(isr);
  122. StringBuffer buffer = new StringBuffer();
  123. //文件原有内容
  124. for(int i=0;(temp =br.readLine())!=null;i++){
  125. buffer.append(temp);
  126. // 行与行之间的分隔符 相当于“\n”
  127. buffer = buffer.append(System.getProperty("line.separator"));
  128. }
  129. buffer.append(filein);
  130. fos = new FileOutputStream(file);
  131. pw = new PrintWriter(fos);
  132. pw.write(buffer.toString().toCharArray());
  133. pw.flush();
  134. bool = true;
  135. } catch (Exception e) {
  136. // TODO: handle exception
  137. e.printStackTrace();
  138. }finally {
  139. //不要忘记关闭
  140. if (pw != null) {
  141. pw.close();
  142. }
  143. if (fos != null) {
  144. fos.close();
  145. }
  146. if (br != null) {
  147. br.close();
  148. }
  149. if (isr != null) {
  150. isr.close();
  151. }
  152. if (fis != null) {
  153. fis.close();
  154. }
  155. }
  156. return bool;
  157. }
  158. /**
  159. * 删除单个文件
  160. *
  161. * @param fileName
  162. * 要删除的文件的文件名
  163. * @return 单个文件删除成功返回true,否则返回false
  164. */
  165. public static boolean deleteFile(String fileName) {
  166. File file = new File(fileName);
  167. if (file.exists() && file.isFile()) {
  168. if (file.delete()) {
  169. return true;
  170. } else {
  171. return false;
  172. }
  173. } else {
  174. return false;
  175. }
  176. }
  177. /**
  178. * 根据路径删除指定的目录,无论存在与否
  179. *@param sPath 要删除的目录path
  180. *@return 删除成功返回 true,否则返回 false。
  181. */
  182. public static boolean deleteFolder(String sPath) {
  183. boolean flag = false;
  184. File file = new File(sPath);
  185. // 判断目录或文件是否存在
  186. if (!file.exists()) { // 不存在返回 false
  187. return flag;
  188. } else {
  189. // 判断是否为文件
  190. if (file.isFile()) { // 为文件时调用删除文件方法
  191. return deleteFile(sPath);
  192. } else { // 为目录时调用删除目录方法
  193. return deleteDirectory(sPath);
  194. }
  195. }
  196. }
  197. /**
  198. * 删除目录以及目录下的文件
  199. * @param sPath 被删除目录的路径
  200. * @return 目录删除成功返回true,否则返回false
  201. */
  202. public static boolean deleteDirectory(String sPath) {
  203. //如果sPath不以文件分隔符结尾,自动添加文件分隔符
  204. if (!sPath.endsWith(File.separator)) {
  205. sPath = sPath + File.separator;
  206. }
  207. File dirFile = new File(sPath);
  208. //如果dir对应的文件不存在,或者不是一个目录,则退出
  209. if (!dirFile.exists() || !dirFile.isDirectory()) {
  210. return false;
  211. }
  212. boolean flag = true;
  213. //删除文件夹下的所有文件(包括子目录)
  214. File[] files = dirFile.listFiles();
  215. for (int i = 0; i < files.length; i++) {
  216. //删除子文件
  217. if (files[i].isFile()) {
  218. flag = deleteFile(files[i].getAbsolutePath());
  219. if (!flag) break;
  220. } //删除子目录
  221. else {
  222. flag = deleteDirectory(files[i].getAbsolutePath());
  223. if (!flag) break;
  224. }
  225. }
  226. if (!flag) return false;
  227. //删除当前目录
  228. if (dirFile.delete()) {
  229. return true;
  230. } else {
  231. return false;
  232. }
  233. }
  234. //按行读文件
  235. public static String readFile(String path) throws Exception {
  236. File f = new File(path);
  237. if (!f.exists()) {
  238. return null;
  239. }
  240. ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
  241. BufferedInputStream in = null;
  242. try {
  243. in = new BufferedInputStream(new FileInputStream(f));
  244. int buf_size = 1024;
  245. byte[] buffer = new byte[buf_size];
  246. int len = 0;
  247. while (-1 != (len = in.read(buffer, 0, buf_size))) {
  248. bos.write(buffer, 0, len);
  249. }
  250. return bos.toString();
  251. } catch (IOException e) {
  252. e.printStackTrace();
  253. throw e;
  254. } finally {
  255. try {
  256. in.close();
  257. } catch (IOException e) {
  258. e.printStackTrace();
  259. }
  260. bos.close();
  261. }
  262. }
  263. public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
  264. File srcFile = new File(srcFileName);
  265. // 判断源文件是否存在
  266. if (!srcFile.exists()) {
  267. return false;
  268. } else if (!srcFile.isFile()) {
  269. return false;
  270. }
  271. // 判断目标文件是否存在
  272. File destFile = new File(destFileName);
  273. if (destFile.exists()) {
  274. // 如果目标文件存在并允许覆盖
  275. if (overlay) {
  276. // 删除已经存在的目标文件,无论目标文件是目录还是单个文件
  277. new File(destFileName).delete();
  278. }
  279. } else {
  280. // 如果目标文件所在目录不存在,则创建目录
  281. if (!destFile.getParentFile().exists()) {
  282. // 目标文件所在目录不存在
  283. if (!destFile.getParentFile().mkdirs()) {
  284. // 复制文件失败:创建目标文件所在目录失败
  285. return false;
  286. }
  287. }
  288. }
  289. // 复制文件
  290. int byteread = 0; // 读取的字节数
  291. InputStream in = null;
  292. OutputStream out = null;
  293. try {
  294. in = new FileInputStream(srcFile);
  295. out = new FileOutputStream(destFile);
  296. byte[] buffer = new byte[1024];
  297. while ((byteread = in.read(buffer)) != -1) {
  298. out.write(buffer, 0, byteread);
  299. }
  300. return true;
  301. } catch (IOException e) {
  302. return false;
  303. } finally {
  304. try {
  305. if (out != null)
  306. out.close();
  307. if (in != null)
  308. in.close();
  309. } catch (IOException e) {
  310. e.printStackTrace();
  311. }
  312. }
  313. }
  314. /**
  315. * 从网络Url中下载文件
  316. *
  317. * @param urlStr
  318. * @param fileName
  319. * @param savePath
  320. * @return
  321. * @throws IOException
  322. */
  323. public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath){
  324. FileOutputStream fos = null;
  325. InputStream inputStream = null;
  326. try {
  327. URL url = new URL(urlStr);
  328. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  329. // 设置超时间为3秒
  330. conn.setConnectTimeout(3 * 1000);
  331. // 防止屏蔽程序抓取而返回403错误
  332. conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  333. // 得到输入流
  334. inputStream = conn.getInputStream();
  335. // 获取自己数组
  336. byte[] getData = readInputStream(inputStream);
  337. // 文件保存位置
  338. File saveDir = new File(savePath);
  339. if (!saveDir.exists()) {
  340. saveDir.mkdirs();
  341. }
  342. String filePath = saveDir + File.separator + fileName;
  343. String filePathFolder = filePath.substring(0, filePath.lastIndexOf("/") + 1);
  344. FileUtils.createDir(filePathFolder);
  345. File file = new File(filePath);
  346. fos = new FileOutputStream(file);
  347. fos.write(getData);
  348. if (fos != null) {
  349. fos.close();
  350. }
  351. if (inputStream != null) {
  352. inputStream.close();
  353. }
  354. log.info("info:" + url + " download success");
  355. } catch(FileNotFoundException e){
  356. return false;
  357. } catch (IOException e) {
  358. return false;
  359. }finally {
  360. if (fos != null) {
  361. try {
  362. fos.close();
  363. } catch (IOException e) {
  364. log.error("下载文件异常", e);
  365. }
  366. }
  367. if (inputStream != null) {
  368. try {
  369. inputStream.close();
  370. } catch (IOException e) {
  371. log.error("下载文件异常", e);
  372. }
  373. }
  374. }
  375. return true;
  376. }
  377. /**
  378. * 从输入流中获取字节数组
  379. *
  380. * @param inputStream
  381. * @return
  382. * @throws IOException
  383. */
  384. private static byte[] readInputStream(InputStream inputStream) throws IOException {
  385. byte[] buffer = new byte[1024];
  386. int len = 0;
  387. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  388. while ((len = inputStream.read(buffer)) != -1) {
  389. bos.write(buffer, 0, len);
  390. }
  391. bos.close();
  392. return bos.toByteArray();
  393. }
  394. public static void writeFile(String filePath,String str) throws IOException {
  395. File fout = new File(filePath);
  396. if(!fout.getParentFile().exists()){
  397. fout.getParentFile().mkdirs();
  398. }
  399. if(!fout.exists()){
  400. fout.createNewFile();
  401. }
  402. FileOutputStream fos = new FileOutputStream(fout);
  403. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
  404. bw.write(str);
  405. bw.close();
  406. }
  407. /**
  408. * 将byte数组写入文件
  409. *
  410. * @param path
  411. * @param fileName
  412. * @param content
  413. * @throws IOException
  414. */
  415. public static void writeFile(String path, String fileName, byte[] content)
  416. throws IOException {
  417. try {
  418. File f = new File(path);
  419. if (!f.exists()) {
  420. f.mkdirs();
  421. }
  422. FileOutputStream fos = new FileOutputStream(path + fileName);
  423. fos.write(content);
  424. fos.close();
  425. } catch (IOException e) {
  426. throw new RuntimeException(e);
  427. }
  428. }
  429. /**
  430. * 向json文件写入参数,重复的覆盖,多的新增
  431. * @return
  432. */
  433. public static void writeJsonFile(String path, Map<String, Object> map) throws Exception{
  434. String str = readFile(path);
  435. JSONObject json = new JSONObject();
  436. if(str!=null){
  437. json = JSONObject.parseObject(str);
  438. }
  439. else{
  440. File file = new File(path);
  441. if(!file.getParentFile().exists())
  442. {
  443. file.getParentFile().mkdirs();
  444. }
  445. if(!file.exists())
  446. {
  447. file.createNewFile();
  448. }
  449. }
  450. Iterator entries = map.entrySet().iterator();
  451. while (entries.hasNext()) {
  452. Map.Entry entry = (Map.Entry) entries.next();
  453. json.put(String.valueOf(entry.getKey()), entry.getValue());
  454. }
  455. writeFile(path, json.toString());
  456. }
  457. //删除文件夹
  458. public static void delFolder(String folderPath) {
  459. try {
  460. delAllFile(folderPath); //删除完里面所有内容
  461. String filePath = folderPath;
  462. filePath = filePath.toString();
  463. File myFilePath = new File(filePath);
  464. myFilePath.delete(); //删除空文件夹
  465. } catch (Exception e) {
  466. e.printStackTrace();
  467. }
  468. }
  469. //删除指定文件夹下的所有文件
  470. public static boolean delAllFile(String path) {
  471. boolean flag = false;
  472. File file = new File(path);
  473. if (!file.exists()) {
  474. return flag;
  475. }
  476. if (!file.isDirectory()) {
  477. return flag;
  478. }
  479. String[] tempList = file.list();
  480. File temp = null;
  481. if(tempList!=null)
  482. {
  483. for (int i = 0; i < tempList.length; i++) {
  484. if (path.endsWith(File.separator)) {
  485. temp = new File(path + tempList[i]);
  486. } else {
  487. temp = new File(path + File.separator + tempList[i]);
  488. }
  489. if (temp.isFile()) {
  490. temp.delete();
  491. }
  492. if (temp.isDirectory()) {
  493. delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
  494. delFolder(path + "/" + tempList[i]);//再删除空文件夹
  495. flag = true;
  496. }
  497. }
  498. }
  499. //再删除当前空文件夹
  500. file.delete();
  501. return flag;
  502. }
  503. public static List<String> readfileNamesForDirectory(String path, String except)
  504. {
  505. try{
  506. File file = new File(path);
  507. if(file.isDirectory())
  508. {
  509. String[] fileNames = file.list();
  510. List<String> list = new ArrayList<String>();
  511. if(fileNames!=null)
  512. {
  513. for(int i=0;i<fileNames.length;++i)
  514. {
  515. if(fileNames[i].toLowerCase().endsWith(except) )
  516. {
  517. list.add(fileNames[i]);
  518. }
  519. }
  520. }
  521. return list;
  522. }
  523. }catch(Exception e){
  524. e.printStackTrace();
  525. }
  526. return null;
  527. }
  528. //递归获取文件中所有文件的路径
  529. public static List<String> readfilePath(String path, List<String> urlList) {
  530. try{
  531. File file = new File(path);
  532. if(file != null && file.isDirectory()) {
  533. File[] files = file.listFiles();
  534. if(files != null) {
  535. for(int i=0;i<files.length;++i) {
  536. if(files[i].isDirectory()){
  537. readfilePath(files[i].getAbsolutePath(), urlList);
  538. }else {
  539. urlList.add(files[i].getAbsolutePath());
  540. }
  541. }
  542. }
  543. return urlList;
  544. }
  545. }catch(Exception e){
  546. e.printStackTrace();
  547. }
  548. return urlList;
  549. }
  550. public static void saveImageToDisk(String accessToken, String mediaId, String picName, String picPath,InputStream inputStream)
  551. throws Exception {
  552. byte[] data = new byte[10240];
  553. int len = 0;
  554. FileOutputStream fileOutputStream = null;
  555. try {
  556. fileOutputStream = new FileOutputStream(picPath+picName+".amr");
  557. while ((len = inputStream.read(data)) != -1) {
  558. fileOutputStream.write(data, 0, len);
  559. }
  560. } catch (IOException e) {
  561. e.printStackTrace();
  562. } finally {
  563. if (inputStream != null) {
  564. try {
  565. inputStream.close();
  566. } catch (IOException e) {
  567. e.printStackTrace();
  568. }
  569. }
  570. if (fileOutputStream != null) {
  571. try {
  572. fileOutputStream.close();
  573. } catch (IOException e) {
  574. e.printStackTrace();
  575. }
  576. }
  577. }
  578. }
  579. /**
  580. *
  581. * @param content base64内容
  582. * @param path 输出文件路径,需要后缀名
  583. * @return
  584. */
  585. public static boolean base64ToFileWriter(String content, String path) {
  586. if (content == null) {
  587. return false;
  588. }
  589. BASE64Decoder decoder = new BASE64Decoder();
  590. try {
  591. // decoder
  592. byte[] b = decoder.decodeBuffer(content);
  593. // processing data
  594. for (int i = 0; i < b.length; ++i) {
  595. if (b[i] < 0) {
  596. b[i] += 256;
  597. }
  598. }
  599. OutputStream out = new FileOutputStream(path);
  600. out.write(b);
  601. out.flush();
  602. out.close();
  603. return true;
  604. } catch (Exception e) {
  605. return false;
  606. }
  607. }
  608. /**
  609. * 获取类路径(classes路径)
  610. */
  611. public static String getResource(){
  612. String path = "";
  613. try {
  614. path = ResourceUtils.getURL("classpath:").getPath();
  615. path = URLDecoder.decode(path,"utf-8");
  616. } catch (Exception e) {
  617. }
  618. return path;
  619. }
  620. /**
  621. * 判断文件大小处于限制内
  622. *
  623. * @param fileLen 文件长度
  624. * @param fileSize 限制大小
  625. * @param fileUnit 限制的单位(B,K,M,G)
  626. * @return
  627. */
  628. public static boolean checkFileSizeIsLimit(Long fileLen, double fileSize, String fileUnit) {
  629. // long len = file.length();
  630. double fileSizeCom = 0;
  631. if ("B".equals(fileUnit.toUpperCase())) {
  632. fileSizeCom = (double) fileLen;
  633. } else if ("K".equals(fileUnit.toUpperCase())) {
  634. fileSizeCom = (double) fileLen / 1024;
  635. } else if ("M".equals(fileUnit.toUpperCase())) {
  636. fileSizeCom = (double) fileLen / (1024*1024);
  637. } else if ("G".equals(fileUnit.toUpperCase())) {
  638. fileSizeCom = (double) fileLen / (1024*1024*1024);
  639. }
  640. if (fileSizeCom > fileSize) {
  641. return false;
  642. }
  643. return true;
  644. }
  645. public static void decompress(String srcPath, String dest) throws Exception {
  646. File file = new File(srcPath);
  647. if (!file.exists()) {
  648. throw new RuntimeException(srcPath + "所指文件不存在");
  649. }
  650. ZipFile zf = new ZipFile(file);
  651. Enumeration entries = zf.getEntries();
  652. ZipEntry entry = null;
  653. while (entries.hasMoreElements()) {
  654. entry = (ZipEntry) entries.nextElement();
  655. log.info("解压" + entry.getName());
  656. if (entry.isDirectory()) {
  657. String dirPath = dest + "/" + entry.getName();
  658. File dir = new File(dirPath);
  659. dir.mkdirs();
  660. } else {
  661. // 表示文件
  662. File f = new File(dest + "/" + entry.getName());
  663. if (!f.exists()) {
  664. //String dirs = FileUtils.getParentPath(f);
  665. String dirs = f.getParent();
  666. File parentDir = new File(dirs);
  667. parentDir.mkdirs();
  668. }
  669. f.createNewFile();
  670. // 将压缩文件内容写入到这个文件中
  671. InputStream is = zf.getInputStream(entry);
  672. FileOutputStream fos = new FileOutputStream(f);
  673. int count;
  674. byte[] buf = new byte[8192];
  675. while ((count = is.read(buf)) != -1) {
  676. fos.write(buf, 0, count);
  677. }
  678. is.close();
  679. fos.close();
  680. }
  681. }
  682. }
  683. public static void unRar(String srcPath, String outDir) throws Exception {
  684. File file = new File(srcPath);
  685. if (!file.exists()) {
  686. throw new RuntimeException(srcPath + "所指文件不存在");
  687. }
  688. File outFileDir = new File(outDir);
  689. if (!outFileDir.exists()) {
  690. boolean isMakDir = outFileDir.mkdirs();
  691. if (isMakDir) {
  692. System.out.println("创建压缩目录成功");
  693. }
  694. }
  695. Archive archive = new Archive(new FileInputStream(file));
  696. FileHeader fileHeader = archive.nextFileHeader();
  697. while (fileHeader != null) {
  698. if (fileHeader.isDirectory()) {
  699. fileHeader = archive.nextFileHeader();
  700. continue;
  701. }
  702. File out = new File(outDir + fileHeader.getFileNameString());
  703. if (!out.exists()) {
  704. if (!out.getParentFile().exists()) {
  705. out.getParentFile().mkdirs();
  706. }
  707. out.createNewFile();
  708. }
  709. FileOutputStream os = new FileOutputStream(out);
  710. archive.extractFile(fileHeader, os);
  711. os.close();
  712. fileHeader = archive.nextFileHeader();
  713. }
  714. archive.close();
  715. }
  716. //按行读文件
  717. public static String readFileGBK(String filepath) throws Exception {
  718. File f = new File(filepath);
  719. if (!f.exists()) {
  720. return null;
  721. }
  722. BufferedReader buf = null;
  723. OutputStreamWriter pw=null;
  724. String str = null;
  725. String allstr="";
  726. buf = new BufferedReader(new InputStreamReader(new FileInputStream(filepath), "GBK"));
  727. while((str = buf.readLine()) != null){
  728. allstr=allstr+str;
  729. }
  730. buf.close();
  731. return allstr;
  732. }
  733. public static void main(String[] args) throws Exception{
  734. // File f = new File("F:\\桌面\\vr-t-2KZ4MQv");
  735. // for(File f1 : f.listFiles()){
  736. // f1.renameTo(new File(f1.getAbsolutePath().replace(".png", ".jpg")));
  737. // }
  738. // String filePath = "F:\\桌面\\c11m-T11-EA\\log";
  739. // String[] files = FileUtils.readFile("F:\\桌面\\c11m-T11-EA\\log\\files.txt").split(";");
  740. // File target = null;
  741. // for (String file : files) {
  742. // target = new File(filePath + file);
  743. // System.out.println(target.getName());
  744. // System.out.println(file);
  745. // }
  746. System.out.println(new String("WIv3YwqC\uFEFF"));
  747. System.out.println(new String("/web-shared/OneKeyDecorate/upload/WIv3YwqC/WIv3YwqC\uFEFF/WIv3YwqC.udatasmith"));
  748. // System.out.println(System.currentTimeMillis());
  749. }
  750. }