File处理
- package com.cfets.ts.u.shchgateway.util;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
- import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
- import org.apache.commons.compress.compressors.FileNameUtil;
- import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
- import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
- import org.apache.commons.lang3.StringUtils;
- import com.cfets.ts.s.log.TsLogger;
- public class FileUtils {
- private static final TsLogger logger = TsLogger.getLogger(FileUtils.class);
- public static final String fileSeparator = File.separator;
- private static final int BUFFER = 1024;
- private FileUtils(){
- }
- private static final FileNameUtil fileNameUtil;
- static {
- final Map<String, String> uncompressSuffix = new LinkedHashMap<>();
- uncompressSuffix.put(".tgz", ".tar");
- uncompressSuffix.put(".taz", ".tar");
- uncompressSuffix.put(".tar.bz2", ".tar");
- uncompressSuffix.put(".tbz2", ".tar");
- uncompressSuffix.put(".tbz", ".tar");
- fileNameUtil = new FileNameUtil(uncompressSuffix, ".gz");
- }
- /**
- * Detects common gzip suffixes in the given filename.
- *
- * @param filename name of a file
- * @return {@code true} if the filename has a common gzip suffix,
- * {@code false} otherwise
- */
- public static boolean isCompressedFilename(final String fileName) {
- return fileNameUtil.isCompressedFilename(fileName);
- }
- /**
- * getUncompressedFilename
- *
- * @param filename name of a file
- * @return name of the corresponding uncompressed file
- */
- public static String getUncompressedFilename(final String fileName) {
- return fileNameUtil.getUncompressedFilename(fileName);
- }
- /**
- * getCompressedFilename
- *
- * @param filename name of a file
- * @return name of the corresponding compressed file
- */
- public static String getCompressedFilename(final String fileName) {
- return fileNameUtil.getCompressedFilename(fileName);
- }
- /**
- * getInputStream
- *
- * @param filePath
- * @return
- * @author huateng 2017年5月01日
- */
- public static InputStream getInputStream(String filePath) {
- BufferedInputStream localBufferedReader = null;
- try {
- localBufferedReader = new BufferedInputStream(new FileInputStream(filePath), 1024);
- } catch (FileNotFoundException e) {
- logger.error("init the file {} BufferedInputStream failed:{}", filePath, e.getMessage());
- }
- return localBufferedReader;
- }
- /**
- * delFile
- *
- * @param fileName
- * @return
- * @author huateng 2017年5月2日
- */
- public static boolean delFile(String fileName) {
- if (StringUtils.isEmpty(fileName)) {
- return true;
- }
- return delFile(new File(fileName));
- }
- /**
- * delFile
- *
- * @param fileName
- * @return
- * @author huateng 2017年5月2日
- */
- public static boolean delFile(File fileName) {
- if (fileName.exists()) {
- fileName.delete();
- }
- return true;
- }
- /**
- * mkDir
- *
- * @param directory
- * @author huateng 2017年5月2日
- */
- public static void mkDir(String directory) {
- mkDir(new File(directory));
- }
- /**
- * mkDir
- *
- * @param directory
- * @author huateng 2017年5月2日
- */
- public static void mkDir(File directory) {
- if (!directory.exists()) {
- mkDir(directory.getParentFile());
- directory.mkdir();
- } else {
- if (directory.isFile()) {
- directory.delete();
- directory.mkdir();
- }
- }
- }
- /**
- * normalize
- *
- * @param fileName
- * @return
- * @author huateng 2017年5月1日
- */
- public static String normalize(String fileName) {
- if (fileName == null) {
- return null;
- }
- int size = fileName.length();
- if (size == 0) {
- return fileName;
- }
- if (!fileName.endsWith(FileUtils.fileSeparator)) {
- return new StringBuffer(fileName).append(FileUtils.fileSeparator).toString();
- }
- return fileName;
- }
- /**
- * listFiles
- *
- * @param filePath
- * @return
- * @author huateng 2017年7月9日
- */
- public static Collection<File> listFiles(String filePath){
- if(StringUtils.isEmpty(filePath)){
- return Collections.emptyList();
- }
- return org.apache.commons.io.FileUtils.listFiles(new File(filePath), null, true);
- }
- /**
- * getName
- *
- * @param fileName
- * @return
- * @author huateng 2017年7月9日
- */
- public static String getName(String fileName){
- return org.apache.commons.io.FilenameUtils.getName(fileName);
- }
- /**
- * getName
- *
- * @param file
- * @return
- * @author huateng 2017年7月9日
- */
- public static String getName(File file){
- if (file == null) {
- return null;
- }
- return org.apache.commons.io.FilenameUtils.getName(file.getName());
- }
- /**
- * getExtension
- *
- * @param fileName
- * @return
- * @author huateng 2017年7月9日
- */
- public static String getExtension(String fileName){
- return org.apache.commons.io.FilenameUtils.getExtension(fileName);
- }
- /**
- * getBaseName
- *
- * @param fileName
- * @return
- * @author huateng 2017年7月9日
- */
- public static String getBaseName(String fileName){
- return org.apache.commons.io.FilenameUtils.getBaseName(fileName);
- }
- /**
- * unCompressBz2
- *
- * @param src
- * @param dest
- *
- * @author huateng 2017年5月2日
- * @throws IOException
- */
- public static void unCompressBz2(String src, String dest) throws IOException {
- FileOutputStream out = null;
- BZip2CompressorInputStream bzIn = null;
- File file = new File(src);
- if (!file.exists()) {
- logger.error("the file {} not exists when uncompress the file {} to the tar file {} failed", src, src, dest);
- throw new IOException("file not exists :" + src);
- }
- try {
- out = new FileOutputStream(dest);
- bzIn = new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file)));
- final byte[] buffer = new byte[BUFFER];
- int n = 0;
- while (-1 != (n = bzIn.read(buffer))) {
- out.write(buffer, 0, n);
- }
- logger.info("uncompress the file {} to the file {} succeed", src, dest);
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (IOException e) {
- logger.error("close FileOutputStream when uncompress the file {} failed:{}", src, e);
- }
- try {
- if (bzIn != null) {
- bzIn.close();
- }
- } catch (IOException e) {
- logger.error("close BZip2CompressorInputStream when uncompress the file {} failed:{}", src, e);
- }
- }
- }
- /**
- * unCompressTgz
- *
- * @param src
- * @param dest
- * @return
- * @author huateng 2017年5月2日
- * @throws IOException
- */
- public static void unCompressTgz(String src, String dest) throws IOException {
- FileOutputStream out = null;
- GzipCompressorInputStream gzIn = null;
- File file = new File(src);
- if (!file.exists()) {
- logger.error("The file {} not exists when uncompress the file {} to the tar file {} failed", src, src, dest);
- throw new IOException("file not exists :" + src);
- }
- try {
- out = new FileOutputStream(dest);
- gzIn = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(file)));
- final byte[] buffer = new byte[BUFFER];
- int n = 0;
- while (-1 != (n = gzIn.read(buffer))) {
- out.write(buffer, 0, n);
- }
- logger.info("uncompress the tgz file {} to the file {} succeed", src, dest);
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (IOException e) {
- logger.error("close FileOutputStream when uncompress the file {} failed: {}", src, e);
- }
- try {
- if (gzIn != null) {
- gzIn.close();
- }
- } catch (IOException e) {
- logger.error("close GzipCompressorInputStream when uncompress the file {} failed: {}", src, e);
- }
- }
- }
- /**
- * unCompressTar
- *
- * @param src
- * @param dest
- * @return int
- * @author huateng 2017年5月1日
- * @throws IOException
- */
- public static int unCompressTar(String src, String dest) throws IOException {
- TarArchiveInputStream tais = null;
- TarArchiveEntry entry = null;
- try {
- tais = new TarArchiveInputStream(new FileInputStream(new File(src)));
- } catch (FileNotFoundException e) {
- logger.error("init TarArchiveInputStream when dearchiving the file {} failed: {}", src, e);
- throw new IOException("file not exists :" + src, e);
- }
- int fileCount = 0;
- try {
- while ((entry = tais.getNextTarEntry()) != null) {
- String entryName = entry.getName();
- if (StringUtils.isEmpty(entryName)) {
- continue;
- }
- String actualFileName = dest + entryName;
- File actualFile = new File(actualFileName);
- if (entry.isDirectory()) {
- actualFile.mkdirs();
- } else {
- fileCount++;
- BufferedOutputStream bos = null;
- try {
- bos = new BufferedOutputStream(new FileOutputStream(actualFile));
- int count;
- byte data[] = new byte[BUFFER];
- try {
- while ((count = tais.read(data, 0, BUFFER)) != -1) {
- bos.write(data, 0, count);
- }
- } finally {
- try {
- if (bos != null) {
- bos.close();
- }
- } catch (IOException e) {
- logger.error(
- "close BufferedOutputStream when uncompressing the file {} in the src {} failed:{}",
- actualFileName, src, e);
- }
- }
- } catch (FileNotFoundException e) {
- logger.error(
- "init BufferedOutputStream when uncompressing the file {} in the file {} failed: {}",
- entryName, src, e);
- throw new IOException("file not exists :" + actualFile, e);
- }
- }
- }
- } finally {
- if (tais != null) {
- try {
- tais.close();
- tais = null;
- } catch (IOException e) {
- logger.error("close TarArchiveInputStream {} when upload failed: {}", src, e.getMessage());
- }
- }
- }
- logger.info("dearchive the file {} to the path {} succeed", src, dest);
- return fileCount;
- }
- public static void main(String[] str) throws IOException {
- }
- }
File处理的更多相关文章
- 记一个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 ...
- HTML中上传与读取图片或文件(input file)----在路上(25)
input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...
- logstash file输入,无输出原因与解决办法
1.现象 很多同学在用logstash input 为file的时候,经常会出现如下问题:配置文件无误,logstash有时一直停留在等待输入的界面 2.解释 logstash作为日志分析的管道,在实 ...
- input[tyle="file"]样式修改及上传文件名显示
默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...
- .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍
1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...
- [笔记]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 ...
- VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%
1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...
- input type='file'上传控件假样式
采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...
- FILE文件流的中fopen、fread、fseek、fclose的使用
FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
随机推荐
- tensorflow中常用学习率更新策略
神经网络训练过程中,根据每batch训练数据前向传播的结果,计算损失函数,再由损失函数根据梯度下降法更新每一个网络参数,在参数更新过程中使用到一个学习率(learning rate),用来定义每次参数 ...
- 粘包、拆包发生原因滑动窗口、MSS/MTU限制、Nagle算法
[TCP协议](3)---TCP粘包黏包 [TCP协议](3)---TCP粘包黏包 有关TCP协议之前写过两篇博客: 1.[TCP协议](1)---TCP协议详解 2.[TCP协议](2)---TCP ...
- 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 ...
- Struts2重新学习之自定义拦截器(判断用户是否是登录状态)
拦截器 一:1:概念:Interceptor拦截器类似于我们学习过的过滤器,是可以再action执行前后执行的代码.是web开发时,常用的技术.比如,权限控制,日志记录. 2:多个拦截器Interce ...
- M端计算rem方法
(function(){var a=document.documentElement.clientWidth||document.body.clientWidth;if(a>460){a=460 ...
- ruby -检查json数据类型
HashObj={","language"=>"zh","make"=>"Apple"," ...
- git代码回退
情况1.还没有push可能 git add ,commit以后发现代码有点问题,想取消提交,用: reset git reset [--soft | --mixed | --hard] eg: gi ...
- TensorFlow 官方文档中文版学习
TensorFlow 官方文档中文版 地址:http://wiki.jikexueyuan.com/project/tensorflow-zh/
- zz 牛人啊
http://www.newsmth.net/nForum/#!article/CouponsLife/184517019:57:33cutepig 2015/6/9 19:57:33 http:// ...
- 【转】每天一个linux命令(28):tar命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/30/2795656.html 通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候 ...