package com.wondersgroup.common.utils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
public class FileUtils {
/**
* 根据文件字节大小返回KB或MB大小
*
* @param size
* @return
*/
public static String getFileSize(long size) {
long d = size / 1024L;
if (d <= 0L) {
return size + " B";
}
d /= 1024L;
if (d <= 0L) {
return (int) (size / 10.24D) / 100.0D + " KB";
}
return (int) (size / 1024L / 10.24D) / 100.0D + " MB";
}
/**
* 复制文件或文件夹
* 如果源是文件,那么就复制文件至目标文件夹,
* 如果源是文件夹,那么就复制文件夹下所有内容(递归)至目标文件夹
* @param sourceFilePath 源文件或文件夹
* @param targetPath 目标文件夹
* @throws Exception
*/
public static void copy(String sourceFilePath, String targetPath) throws Exception{
File sourceFile = new File(sourceFilePath);
File targetFile = new File(targetPath);
//目标文件夹不存在就创建
if(!targetFile.exists()){
targetFile.mkdirs();
}
if(sourceFile.exists()){
//如果源是文件夹
if(sourceFile.isDirectory()){
for(File source : sourceFile.listFiles()){
if(source.isDirectory()){
copy(source.getAbsolutePath(), targetPath + File.separator + source.getName());
}
else{
copy(source.getAbsolutePath(), targetPath);
}
}
}
//如果源是文件
else{
copyFile(sourceFile, new File(targetPath + File.separator + sourceFile.getName()));
}
}
}
/**
* 复制文件
* @param sourceFile
* @param targetFile
* @throws IOException
*/
private static void copyFile(File sourceFile, File targetFile) throws IOException{
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 新建文件输入流并对它进行缓冲
fis = new FileInputStream(sourceFile);
bis = new BufferedInputStream(fis);
// 新建文件输出流并对它进行缓冲
fos = new FileOutputStream(targetFile);
bos = new BufferedOutputStream(fos);
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
// 刷新此缓冲的输出流
bos.flush();
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(fos != null){
fos.close();
bos.close();
}
if(fis != null){
fis.close();
bis.close();
}
}
}
/**
* 压缩文件夹
* @param zipDirectory 需要压缩的源文件夹路径
* @param destZipFile 目标压缩文件的路径
* @param encoding 文件编码
* @return
* @throws IOException
*/
public static File doZip(String zipDirectoryPath, String destZipFilePath, String encoding) throws IOException {
File zipDirectory = new File(zipDirectoryPath);
File destZipFile = new File(destZipFilePath);
if(!zipDirectory.exists()){
throw new RuntimeException(zipDirectory.getAbsolutePath() + "不存在!");
}
FileOutputStream fos = null;
CheckedOutputStream cos = null;
ZipOutputStream out = null;
try {
//得到目标zip文件的文件夹
File destPath = destZipFile.getParentFile();
//如果不存在就创建目录
if(!destPath.exists()){
destPath.mkdirs();
}
fos = new FileOutputStream(destZipFile);
cos = new CheckedOutputStream(fos, new CRC32());
out = new ZipOutputStream(cos);
out.setEncoding(encoding);
String basedir = "";
if (zipDirectory.isDirectory()) {
for (File childFile : zipDirectory.listFiles()) {
//递归
compress(childFile, out, basedir);
}
}
else{
compress(zipDirectory, out, basedir);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
out.close();
cos.close();
fos.close();
out = null;
cos = null;
fos = null;
}
return destZipFile;
}
/**
* 下载文件
* @param file
* @param rep
* @param deleteFilePath 如果需要在下载后删除,则传入需要删除的文件/目录路径,否则应传入null
*/
public static void downloadFile(File file, HttpServletResponse rep, String deleteFilePath)throws Exception{
BufferedInputStream bis = null;
FileInputStream fis = null;
OutputStream clientOs = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
byte[] buffer = new byte[fis.available()];
bis.read(buffer);
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
bis = null;
fis = null;
if(deleteFilePath != null && !"".equals(deleteFilePath)){
delete(deleteFilePath);
}
//涉及到中文文件
String filename= URLEncoder.encode(file.getName(),"UTF-8");
// 清空response
rep.reset();
// 设置response的Header
rep.addHeader("Content-Disposition", "attachment;filename=" + filename);
rep.addHeader("Access-Control-Allow-Origin","*");
//rep.addHeader("Content-Length", "" + file.length());
rep.setContentType("application/octet-stream");
clientOs = new BufferedOutputStream(rep.getOutputStream());
clientOs.write(buffer);
clientOs.flush();
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(clientOs != null){
try {
clientOs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
clientOs = null;
bis = null;
fis = null;
}
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
* @param sPath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean delete(String filePath) {
boolean flag = false;
File file = new File(filePath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(filePath);
} else { // 为目录时调用删除目录方法
return deleteDirectory(filePath);
}
}
}
//临时文件夹
public static Path getTempPath(){
try {
return Files.createTempDirectory("SHBB_");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 删除单个文件
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
private static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 删除目录(文件夹)以及目录下的文件
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
private static boolean deleteDirectory(String sPath) {
boolean flag;
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
flag = true;
//删除文件夹下的所有文件(包括子目录)
for (File file : dirFile.listFiles()) {
//删除子文件
if (file.isFile()) {
flag = deleteFile(file.getAbsolutePath());
if (!flag) break;
} //删除子目录
else {
flag = deleteDirectory(file.getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
//删除当前目录
return dirFile.delete();
}
/**
* 递归压缩目录内所有文件的方法
* @param file
* @param out
* @param basedir
* @throws IOException
*/
private static void compress(File file, ZipOutputStream out, String basedir) throws IOException {
if (!file.exists()){
return ;
}
//如果是目录
if (file.isDirectory()) {
for (File childFile : file.listFiles()) {
//递归
compress(childFile, out, basedir + file.getName() + "/");
}
}
//如果是文件
else {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[8192];
while ((count = bis.read(data, 0, 8192)) != -1) {
out.write(data, 0, count);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
bis.close();
fis.close();
bis = null;
fis = null;
}
}
}
}
FileUtils工具类
最新推荐文章于 2024-10-03 08:23:52 发布
304

被折叠的 条评论
为什么被折叠?



