Created
December 18, 2017 01:47
-
-
Save TouiSoraHe/4b5238320594cb4e2ddf5b0d4465cda0 to your computer and use it in GitHub Desktop.
文件操作工具
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.Enumeration; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
public class FileUtil { | |
/** | |
* 获取压缩文件找中指定文件的最后修改时间 | |
* | |
* @param zipFilePath | |
* .zip文件路径 | |
* @param targetFileName | |
* 要查找的文件名,例如:xxx 压缩文件中存在多个同名文件,将返回查找到的第一个文件或文件夹 | |
* @param isDirectory | |
* 目标文件是否是一个文件夹,是:true | |
* 否:false | |
* @return 返回时间,查找失败将返回-1 | |
*/ | |
static public long findZipFileTime(String zipFilePath, String targetFileName, boolean isDirectory) { | |
File file = new File(zipFilePath); | |
if (!file.exists()) { | |
new Exception(file.getName() + " 文件不存在").printStackTrace(); | |
return -1; | |
} else if (file.isDirectory()) { | |
new Exception(file.getName() + " 不是一个压缩文件").printStackTrace(); | |
return -1; | |
} | |
ZipFile zipfile = null; | |
try { | |
zipfile = new ZipFile(file); | |
Enumeration<? extends ZipEntry> zipEntryList = zipfile.entries(); | |
ZipEntry temp = null; | |
while (zipEntryList.hasMoreElements()) { | |
temp = zipEntryList.nextElement(); | |
if (isDirectory) { | |
if (temp.isDirectory()) { | |
int index = temp.getName().lastIndexOf(targetFileName + "/"); | |
if (index != -1 && temp.getName().length() - (targetFileName + "/").length() == index) { | |
return temp.getTime(); | |
} | |
} | |
} else if (!temp.isDirectory()) { | |
int index = temp.getName().lastIndexOf(targetFileName); | |
if (index != -1 && temp.getName().length() - (targetFileName).length() == index) { | |
return temp.getTime(); | |
} | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (zipfile != null) { | |
try { | |
zipfile.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return -1; | |
} | |
/** | |
* 解压缩指定文件 | |
* | |
* @param filePath | |
* .zip文件路径 | |
* @param targetFolderPath | |
* 文件解压的目录 | |
* @return 成功返回true | |
*/ | |
static public boolean unZip(String filePath, String targetFolderPath) { | |
File file = new File(filePath); | |
if (!file.exists()) { | |
new Exception(file.getName() + " 文件不存在").printStackTrace(); | |
return false; | |
} else if (file.isDirectory()) { | |
new Exception(file.getName() + " 不是一个压缩文件").printStackTrace(); | |
return false; | |
} | |
File targetFolder = new File(targetFolderPath); | |
if (!targetFolder.exists() && !targetFolder.mkdirs()) { | |
new Exception("目标文件夹不存在,自动创建失败!").printStackTrace(); | |
return false; | |
} else if (!targetFolder.isDirectory()) { | |
new Exception("指定路径中已经存在一个同名的文件,无法创建指定目录!").printStackTrace(); | |
return false; | |
} | |
{// 获取zip文件名称(去除后缀) | |
String s = new String(file.getName().toCharArray(), 0, | |
file.getName().lastIndexOf(".") > -1 ? file.getName().lastIndexOf(".") : file.getName().length()); | |
targetFolder = new File(targetFolder.getPath() + File.separator + s); | |
if (!targetFolder.exists()) { | |
targetFolder.mkdirs(); | |
} else if (!targetFolder.isDirectory()) { | |
targetFolder = new File(targetFolder.getPath() + "副本"); | |
if (!targetFolder.mkdirs()) { | |
return false; | |
} | |
} | |
} | |
ZipFile zipfile = null; | |
try { | |
zipfile = new ZipFile(file); | |
Enumeration<? extends ZipEntry> zipEntryList = zipfile.entries(); | |
ZipEntry temp = null; | |
while (zipEntryList.hasMoreElements()) { | |
temp = zipEntryList.nextElement(); | |
if (!temp.isDirectory()) { | |
File targetFiles = new File(targetFolder.getPath() + File.separator + temp.getName()); | |
if (!targetFiles.getParentFile().exists()) { | |
targetFiles.getParentFile().mkdirs(); | |
} | |
Files.copy(zipfile.getInputStream(temp), Paths.get(targetFiles.getPath())); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (zipfile != null) { | |
try { | |
zipfile.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return true; | |
} | |
/** | |
* 删除指定文件或文件夹 | |
* | |
* @param filesPath | |
* 要删除的文件或文件夹的路径 | |
* | |
*/ | |
static public void deleteFiles(String filesPath) { | |
File files = new File(filesPath); | |
if (!files.exists()) { | |
return; | |
} | |
if (files.isFile()) { | |
files.delete(); | |
} else if (files.isDirectory()) { | |
for (File file : files.listFiles()) { | |
deleteFiles(file.getPath()); | |
} | |
files.delete(); | |
} | |
} | |
/** | |
* 下載指定的URL | |
* | |
* @param urlPath | |
* 网址 | |
* @param downloadFolderPath | |
* 下载文件保存的路径 | |
* @return 返回该文件的抽象表示形式 | |
*/ | |
static public File downloadFile(String urlPath, String downloadFolderPath, String fileName) { | |
File downloadFolder = new File(downloadFolderPath); | |
if (!downloadFolder.exists()) { | |
downloadFolder.mkdirs(); | |
} else if (!downloadFolder.isDirectory()) { | |
new Exception("指定路径中已经存在一个同名的文件,无法创建指定目录!").printStackTrace(); | |
return null; | |
} | |
File downloadFile = new File(downloadFolder.getPath() + File.separator + fileName); | |
for (int i = 1; downloadFile.exists(); ++i) { | |
StringBuilder newFileName = new StringBuilder(fileName); | |
if (newFileName.lastIndexOf(".") != -1) { | |
newFileName.insert(newFileName.lastIndexOf("."), " (" + i + ")"); | |
} else { | |
newFileName.append(" (" + i + ")"); | |
} | |
downloadFile = new File(downloadFolder.getPath() + File.separator + newFileName.toString()); | |
} | |
URL url = null; | |
URLConnection conn = null; | |
try { | |
url = new URL(urlPath); | |
if (url != null) { | |
conn = url.openConnection(); | |
} | |
if (conn != null) { | |
Files.copy(conn.getInputStream(), Paths.get(downloadFile.getPath())); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return downloadFile; | |
} | |
/* | |
* 将指定文件或者目录复制到指定目录中 | |
* | |
* @param source 源文件或原目目录,若该路径表示一个目录,则将该目录中所有子文件复制到目标目录 | |
* | |
* @param target 目标目录,若目录不存在,將自动创建该目录 | |
* | |
* @param isDeleteSourceFiles 是否刪除原文件或文件夾,删除为true,不删除为false | |
* | |
* @return 成功返回true,失败返回false | |
*/ | |
static public boolean moveFile(String sourcePath, String targetPath, boolean isDeleteSourceFiles) { | |
File targetFile = new File(targetPath); | |
File sourceFile = new File(sourcePath); | |
if (!targetFile.exists()) { | |
targetFile.mkdirs(); | |
} else if (!targetFile.isDirectory()) { | |
new Exception("指定路径中已经存在一个同名的文件,无法创建指定目录!").printStackTrace(); | |
return false; | |
} | |
if (sourceFile.exists()) { | |
if (sourceFile.isFile()) { | |
try (OutputStream out = new FileOutputStream( | |
targetFile.getPath() + File.separator + sourceFile.getName());) { | |
Files.copy(Paths.get(sourceFile.getPath()), out); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} else if (sourceFile.isDirectory()) { | |
File[] sourceFiles = sourceFile.listFiles(); | |
for (File s : sourceFiles) { | |
moveFile(s.getPath(), targetPath + File.separator + sourceFile.getName(), isDeleteSourceFiles); | |
} | |
} | |
if (isDeleteSourceFiles) { | |
sourceFile.delete(); | |
} | |
return true; | |
} | |
return false; | |
} | |
/* | |
* 查找指定文件或者文件夹是否包含指定文件,按文件夹层级查找,找到第一个同名文件时立即返回,不再继续查找 | |
* | |
* @param targetFolder 要查找的文件或者文件夹 | |
* | |
* @param fileName 要查找的文件名 | |
* | |
* @return 返回该文件的抽象表示形式,若未找到文件则返回null | |
*/ | |
public static File findFile(String targetFolderPath, String fileName) { | |
File targetFolder = new File(targetFolderPath); | |
if (targetFolder.exists()) { | |
if (targetFolder.isFile()) { | |
if (fileName.equals(targetFolder.getName())) { | |
return targetFolder; | |
} | |
} else if (targetFolder.isDirectory()) { | |
File[] files = targetFolder.listFiles(); | |
for (File file : files) { | |
File ret = findFile(file.getPath(), fileName); | |
if (ret != null) | |
return ret; | |
} | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment