1. package com.cfets.ts.u.shchgateway.util;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.util.Collection;
  12. import java.util.Collections;
  13. import java.util.LinkedHashMap;
  14. import java.util.Map;
  15.  
  16. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  17. import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
  18. import org.apache.commons.compress.compressors.FileNameUtil;
  19. import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
  20. import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
  21. import org.apache.commons.lang3.StringUtils;
  22.  
  23. import com.cfets.ts.s.log.TsLogger;
  24.  
  25. public class FileUtils {
  26. private static final TsLogger logger = TsLogger.getLogger(FileUtils.class);
  27.  
  28. public static final String fileSeparator = File.separator;
  29. private static final int BUFFER = 1024;
  30.  
  31. private FileUtils(){
  32. }
  33.  
  34. private static final FileNameUtil fileNameUtil;
  35.  
  36. static {
  37. final Map<String, String> uncompressSuffix = new LinkedHashMap<>();
  38. uncompressSuffix.put(".tgz", ".tar");
  39. uncompressSuffix.put(".taz", ".tar");
  40. uncompressSuffix.put(".tar.bz2", ".tar");
  41. uncompressSuffix.put(".tbz2", ".tar");
  42. uncompressSuffix.put(".tbz", ".tar");
  43. fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz");
  44. }
  45.  
  46. /**
  47. * Detects common gzip suffixes in the given filename.
  48. *
  49. * @param filename name of a file
  50. * @return {@code true} if the filename has a common gzip suffix,
  51. * {@code false} otherwise
  52. */
  53. public static boolean isCompressedFilename(final String fileName) {
  54. return fileNameUtil.isCompressedFilename(fileName);
  55. }
  56.  
  57. /**
  58. * getUncompressedFilename
  59. *
  60. * @param filename name of a file
  61. * @return name of the corresponding uncompressed file
  62. */
  63. public static String getUncompressedFilename(final String fileName) {
  64. return fileNameUtil.getUncompressedFilename(fileName);
  65. }
  66.  
  67. /**
  68. * getCompressedFilename
  69. *
  70. * @param filename name of a file
  71. * @return name of the corresponding compressed file
  72. */
  73. public static String getCompressedFilename(final String fileName) {
  74. return fileNameUtil.getCompressedFilename(fileName);
  75. }
  76.  
  77. /**
  78. * getInputStream
  79. *
  80. * @param filePath
  81. * @return
  82. * @author huateng 2017年5月01日
  83. */
  84. public static InputStream getInputStream(String filePath) {
  85. BufferedInputStream localBufferedReader = null;
  86. try {
  87. localBufferedReader = new BufferedInputStream(new FileInputStream(filePath), 1024);
  88. } catch (FileNotFoundException e) {
  89. logger.error("init the file {} BufferedInputStream failed:{}", filePath, e.getMessage());
  90. }
  91. return localBufferedReader;
  92. }
  93.  
  94. /**
  95. * delFile
  96. *
  97. * @param fileName
  98. * @return
  99. * @author huateng 2017年5月2日
  100. */
  101. public static boolean delFile(String fileName) {
  102. if (StringUtils.isEmpty(fileName)) {
  103. return true;
  104. }
  105. return delFile(new File(fileName));
  106. }
  107.  
  108. /**
  109. * delFile
  110. *
  111. * @param fileName
  112. * @return
  113. * @author huateng 2017年5月2日
  114. */
  115. public static boolean delFile(File fileName) {
  116. if (fileName.exists()) {
  117. fileName.delete();
  118. }
  119. return true;
  120. }
  121.  
  122. /**
  123. * mkDir
  124. *
  125. * @param directory
  126. * @author huateng 2017年5月2日
  127. */
  128. public static void mkDir(String directory) {
  129. mkDir(new File(directory));
  130. }
  131.  
  132. /**
  133. * mkDir
  134. *
  135. * @param directory
  136. * @author huateng 2017年5月2日
  137. */
  138. public static void mkDir(File directory) {
  139. if (!directory.exists()) {
  140. mkDir(directory.getParentFile());
  141. directory.mkdir();
  142. } else {
  143. if (directory.isFile()) {
  144. directory.delete();
  145. directory.mkdir();
  146. }
  147. }
  148. }
  149.  
  150. /**
  151. * normalize
  152. *
  153. * @param fileName
  154. * @return
  155. * @author huateng 2017年5月1日
  156. */
  157. public static String normalize(String fileName) {
  158. if (fileName == null) {
  159. return null;
  160. }
  161. int size = fileName.length();
  162. if (size == 0) {
  163. return fileName;
  164. }
  165. if (!fileName.endsWith(FileUtils.fileSeparator)) {
  166. return new StringBuffer(fileName).append(FileUtils.fileSeparator).toString();
  167. }
  168. return fileName;
  169. }
  170.  
  171. /**
  172. * listFiles
  173. *
  174. * @param filePath
  175. * @return
  176. * @author huateng 2017年7月9日
  177. */
  178. public static Collection<File> listFiles(String filePath){
  179. if(StringUtils.isEmpty(filePath)){
  180. return Collections.emptyList();
  181. }
  182. return org.apache.commons.io.FileUtils.listFiles(new File(filePath), null, true);
  183. }
  184.  
  185. /**
  186. * getName
  187. *
  188. * @param fileName
  189. * @return
  190. * @author huateng 2017年7月9日
  191. */
  192. public static String getName(String fileName){
  193. return org.apache.commons.io.FilenameUtils.getName(fileName);
  194. }
  195.  
  196. /**
  197. * getName
  198. *
  199. * @param file
  200. * @return
  201. * @author huateng 2017年7月9日
  202. */
  203. public static String getName(File file){
  204. if (file == null) {
  205. return null;
  206. }
  207. return org.apache.commons.io.FilenameUtils.getName(file.getName());
  208. }
  209.  
  210. /**
  211. * getExtension
  212. *
  213. * @param fileName
  214. * @return
  215. * @author huateng 2017年7月9日
  216. */
  217. public static String getExtension(String fileName){
  218. return org.apache.commons.io.FilenameUtils.getExtension(fileName);
  219. }
  220.  
  221. /**
  222. * getBaseName
  223. *
  224. * @param fileName
  225. * @return
  226. * @author huateng 2017年7月9日
  227. */
  228. public static String getBaseName(String fileName){
  229. return org.apache.commons.io.FilenameUtils.getBaseName(fileName);
  230. }
  231. /**
  232. * unCompressBz2
  233. *
  234. * @param src
  235. * @param dest
  236. *
  237. * @author huateng 2017年5月2日
  238. * @throws IOException
  239. */
  240. public static void unCompressBz2(String src, String dest) throws IOException {
  241. FileOutputStream out = null;
  242. BZip2CompressorInputStream bzIn = null;
  243. File file = new File(src);
  244. if (!file.exists()) {
  245. logger.error("the file {} not exists when uncompress the file {} to the tar file {} failed", src, src, dest);
  246. throw new IOException("file not exists :" + src);
  247. }
  248. try {
  249. out = new FileOutputStream(dest);
  250. bzIn = new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file)));
  251. final byte[] buffer = new byte[BUFFER];
  252. int n = 0;
  253. while (-1 != (n = bzIn.read(buffer))) {
  254. out.write(buffer, 0, n);
  255. }
  256. logger.info("uncompress the file {} to the file {} succeed", src, dest);
  257. } finally {
  258. try {
  259. if (out != null) {
  260. out.close();
  261. }
  262. } catch (IOException e) {
  263. logger.error("close FileOutputStream when uncompress the file {} failed:{}", src, e);
  264. }
  265. try {
  266. if (bzIn != null) {
  267. bzIn.close();
  268. }
  269. } catch (IOException e) {
  270. logger.error("close BZip2CompressorInputStream when uncompress the file {} failed:{}", src, e);
  271. }
  272. }
  273. }
  274.  
  275. /**
  276. * unCompressTgz
  277. *
  278. * @param src
  279. * @param dest
  280. * @return
  281. * @author huateng 2017年5月2日
  282. * @throws IOException
  283. */
  284. public static void unCompressTgz(String src, String dest) throws IOException {
  285. FileOutputStream out = null;
  286. GzipCompressorInputStream gzIn = null;
  287. File file = new File(src);
  288. if (!file.exists()) {
  289. logger.error("The file {} not exists when uncompress the file {} to the tar file {} failed", src, src, dest);
  290. throw new IOException("file not exists :" + src);
  291. }
  292. try {
  293. out = new FileOutputStream(dest);
  294. gzIn = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(file)));
  295. final byte[] buffer = new byte[BUFFER];
  296. int n = 0;
  297. while (-1 != (n = gzIn.read(buffer))) {
  298. out.write(buffer, 0, n);
  299. }
  300. logger.info("uncompress the tgz file {} to the file {} succeed", src, dest);
  301. } finally {
  302. try {
  303. if (out != null) {
  304. out.close();
  305. }
  306. } catch (IOException e) {
  307. logger.error("close FileOutputStream when uncompress the file {} failed: {}", src, e);
  308. }
  309. try {
  310. if (gzIn != null) {
  311. gzIn.close();
  312. }
  313. } catch (IOException e) {
  314. logger.error("close GzipCompressorInputStream when uncompress the file {} failed: {}", src, e);
  315. }
  316. }
  317. }
  318.  
  319. /**
  320. * unCompressTar
  321. *
  322. * @param src
  323. * @param dest
  324. * @return int
  325. * @author huateng 2017年5月1日
  326. * @throws IOException
  327. */
  328. public static int unCompressTar(String src, String dest) throws IOException {
  329.  
  330. TarArchiveInputStream tais = null;
  331. TarArchiveEntry entry = null;
  332. try {
  333. tais = new TarArchiveInputStream(new FileInputStream(new File(src)));
  334. } catch (FileNotFoundException e) {
  335. logger.error("init TarArchiveInputStream when dearchiving the file {} failed: {}", src, e);
  336. throw new IOException("file not exists :" + src, e);
  337. }
  338. int fileCount = 0;
  339. try {
  340. while ((entry = tais.getNextTarEntry()) != null) {
  341. String entryName = entry.getName();
  342. if (StringUtils.isEmpty(entryName)) {
  343. continue;
  344. }
  345. String actualFileName = dest + entryName;
  346. File actualFile = new File(actualFileName);
  347. if (entry.isDirectory()) {
  348. actualFile.mkdirs();
  349. } else {
  350. fileCount++;
  351. BufferedOutputStream bos = null;
  352. try {
  353. bos = new BufferedOutputStream(new FileOutputStream(actualFile));
  354. int count;
  355. byte data[] = new byte[BUFFER];
  356. try {
  357. while ((count = tais.read(data, 0, BUFFER)) != -1) {
  358. bos.write(data, 0, count);
  359. }
  360. } finally {
  361. try {
  362. if (bos != null) {
  363. bos.close();
  364. }
  365. } catch (IOException e) {
  366. logger.error(
  367. "close BufferedOutputStream when uncompressing the file {} in the src {} failed:{}",
  368. actualFileName, src, e);
  369. }
  370. }
  371. } catch (FileNotFoundException e) {
  372. logger.error(
  373. "init BufferedOutputStream when uncompressing the file {} in the file {} failed: {}",
  374. entryName, src, e);
  375. throw new IOException("file not exists :" + actualFile, e);
  376. }
  377. }
  378. }
  379. } finally {
  380. if (tais != null) {
  381. try {
  382. tais.close();
  383. tais = null;
  384. } catch (IOException e) {
  385. logger.error("close TarArchiveInputStream {} when upload failed: {}", src, e.getMessage());
  386. }
  387. }
  388. }
  389. logger.info("dearchive the file {} to the path {} succeed", src, dest);
  390. return fileCount;
  391. }
  392.  
  393. public static void main(String[] str) throws IOException {
  394.  
  395. }
  396.  
  397. }

File处理的更多相关文章

  1. 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file

    我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...

  2. HTML中上传与读取图片或文件(input file)----在路上(25)

    input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...

  3. logstash file输入,无输出原因与解决办法

    1.现象 很多同学在用logstash input 为file的时候,经常会出现如下问题:配置文件无误,logstash有时一直停留在等待输入的界面 2.解释 logstash作为日志分析的管道,在实 ...

  4. input[tyle="file"]样式修改及上传文件名显示

    默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...

  5. .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

    1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...

  6. [笔记]HAproxy reload config file with uninterrupt session

    HAProxy is a high performance load balancer. It is very light-weight, and free, making it a great op ...

  7. VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%

    1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...

  8. input type='file'上传控件假样式

    采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...

  9. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

  10. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

随机推荐

  1. tensorflow中常用学习率更新策略

    神经网络训练过程中,根据每batch训练数据前向传播的结果,计算损失函数,再由损失函数根据梯度下降法更新每一个网络参数,在参数更新过程中使用到一个学习率(learning rate),用来定义每次参数 ...

  2. 粘包、拆包发生原因滑动窗口、MSS/MTU限制、Nagle算法

    [TCP协议](3)---TCP粘包黏包 [TCP协议](3)---TCP粘包黏包 有关TCP协议之前写过两篇博客: 1.[TCP协议](1)---TCP协议详解 2.[TCP协议](2)---TCP ...

  3. Microsoft - Union Two Sorted List with Distinct Value

    Union Two Sorted List with Distinct Value Given X = { 10, 12, 16, 20 } &  Y = {12, 18, 20, 22} W ...

  4. Struts2重新学习之自定义拦截器(判断用户是否是登录状态)

    拦截器 一:1:概念:Interceptor拦截器类似于我们学习过的过滤器,是可以再action执行前后执行的代码.是web开发时,常用的技术.比如,权限控制,日志记录. 2:多个拦截器Interce ...

  5. M端计算rem方法

    (function(){var a=document.documentElement.clientWidth||document.body.clientWidth;if(a>460){a=460 ...

  6. ruby -检查json数据类型

    HashObj={","language"=>"zh","make"=>"Apple"," ...

  7. git代码回退

    情况1.还没有push可能 git add ,commit以后发现代码有点问题,想取消提交,用: reset git reset [--soft | --mixed | --hard] eg:  gi ...

  8. TensorFlow 官方文档中文版学习

    TensorFlow 官方文档中文版 地址:http://wiki.jikexueyuan.com/project/tensorflow-zh/

  9. zz 牛人啊

    http://www.newsmth.net/nForum/#!article/CouponsLife/184517019:57:33cutepig 2015/6/9 19:57:33 http:// ...

  10. 【转】每天一个linux命令(28):tar命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/30/2795656.html 通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候 ...