文件处理工具类

package com.toft.ui.tags;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.FileUtils;

import com.iss.file.dcs.utils.DcsService;

import java.io.File;
import java.net.URL;

public class FileHelper {
/**
 * 下载指定url
 * 
 * @param1 url 压缩文件路径
 * @param2 dir 下载存储路径
 */
public static String downloadFromUrl(String url, String dir) {

try {
URL httpurl = new URL(url);
String fileName = getFileNameFromUrl(url);
// System.out.println(fileName);
File f = new File(dir + fileName);
FileUtils.copyURLToFile(httpurl, f);
} catch (Exception e) {
e.printStackTrace();
return "Fault!";
}
return "Successful!";
}

public static String getFileNameFromUrl(String url) {
String name = new Long(System.currentTimeMillis()).toString() + ".X";
int index = url.lastIndexOf("/");
if (index > 0) {
name = url.substring(index + 1);
if (name.trim().length() > 0) {
return name;
}
}
return name;
}

/**
 * 解压文件
 * 
 * @param filePath
 *            压缩文件路径
 */
public static void unzip(String filePath) {
File source = new File(filePath);
if (source.exists()) {
FileInputStream in = null;
ZipInputStream zis = null;
BufferedOutputStream bos = null;
try {
in = new FileInputStream(source);
zis = new ZipInputStream(in);
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null
&& !entry.isDirectory()) {
File target = new File(source.getParent(), entry.getName());
if (!target.getParentFile().exists()) {
// 创建文件父目录
target.getParentFile().mkdirs();
}
// 写入文件
bos = new BufferedOutputStream(new FileOutputStream(target));
int read = 0;
byte[] buffer = new byte[1024 * 10];
while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, read);
}
bos.flush();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (bos != null) {
bos.close();
}
if (zis != null) {
zis.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
 * 移动目录及文件到指定目录下
 * 
 * @Parma1 src 移动前文件路径
 * @param2 dest 移动后文件路径
 */
public static void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// 递归复制
copyFolder(srcFile, destFile);
}
} else {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);

byte[] buffer = new byte[1024];

int length;

while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}

/**
 * 递归删除文件夹
 * 
 * @Parma dir 删除文件路径
 */
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
// 递归删除目录中的子目录下
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}

/**
 * 删除空目录
 * 
 * @param dir
 *            将要删除的目录路径
 */
public static void doDeleteEmptyDir(String dir) {
boolean success = (new File(dir)).delete();
if (success) {
System.out.println("Successfully deleted empty directory: " + dir);
} else {
System.out.println("Failed to delete empty directory: " + dir);
}
}

/**
 * 判断目录下文件夹大小
 * 
 * @param File
 *            文件级夹路径
 */
public static double getDirSize(File file) {
// 判断文件是否存在
if (file.exists()) {
// 如果是目录则递归计算其内容的总大小
if (file.isDirectory()) {
File[] children = file.listFiles();
double size = 0;
for (File f : children)
size += getDirSize(f);
return size;
} else {// 如果是文件则直接返回其大小,以“G”为单位
double size = (double) file.length() / 1024 / 1024 /1024;
return size;
}
} else {
System.out.println("所计算大小的文件或者文件夹不存在..");
return 0.0;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值