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 { } }

最新文章

  1. 【Visual Lisp】图元选择集专题
  2. iOS学习之C语言指针
  3. C# Task的使用---Task的启动
  4. Datum Form Goole Android
  5. 转:前端冷知识(~~some fun , some useful)
  6. 2px边框,4分之1内边框实现选中功能实现
  7. easyUI中datagrid的使用
  8. C#动态获取鼠标坐标
  9. 【Android源码解析】View.post()到底干了啥
  10. Windows系统下安装zabbix客户端
  11. eclipse的快捷键【转载】
  12. sql 某字段存储另一个表的多个id值并以逗号分隔,现根据id去中文并拼接同样以逗号分隔
  13. Jenkins 主备master-slave模式搭建
  14. pthreads v3下的worker和pool的使用
  15. HDU 6126.Give out candies 最小割
  16. 实现统计 android手机 CPU使用率
  17. MikroTik RouterOS 5.x破解工具HunterTik
  18. C#绘制数字图像灰度直方图
  19. 源码分析八(org.springframework.util包之StringUtils类))
  20. [Mybatis]resultMap的使用总结

热门文章

  1. 如何使用Java读写系统属性?
  2. Linux下XordDos木马的清除
  3. hacking a friend&#39;s Linux buzzer driver in OK335xS
  4. 使用animate()完成修改图片src切换图片的动画效果
  5. Cocos2D-X2.2.3学习笔记13(延时动作)
  6. HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
  7. tyvj1061Mobile Service
  8. 关闭IE 对剪切板访问的提示
  9. java 网络编程UDP
  10. 线性代数之SVD与PCA