java FileUtils 文件工具類

package com.sicdt.library.core.utils; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * <br>類 名: FileUtils * <br>描 述: 描述類完成的主要功能 * <br>做 者: shizhenwei * <br>創 建: 2017年5月15日 * <br>版 本: v0.0.2 * <br> * <br>歷 史: (版本) 做者 時間 註釋 */
public class FileUtils { private Logger log = LoggerFactory.getLogger(FileUtils.class); private File file; public FileUtils(File file) { this.file = file; } public FileUtils(String pathname) { this(new File(pathname)); } public static FileUtils create(String pathname) { return new FileUtils(pathname); } public static FileUtils create(File file) { return new FileUtils(file); } /** * 獲取不帶擴展名的文件名 * @return
     */
    public String getNameWithOutSuffix() { String name = file.getName(); if(!file.isFile()) return name; int flag = name.lastIndexOf("."); if(flag != -1){ return name.substring(0, flag); } return name; } /** * 獲取文件擴展名 * @return
     */
    public String getSuffix(){ if(!file.isFile()) return ""; String name = file.getName(); int flag = name.lastIndexOf("."); if(flag != -1){ return name.substring(flag + 1).toLowerCase(); } return ""; } /** * 獲取當前文件所在文件夾路徑 * @return
     */
    public String getFolderPath() { File parent = file.getParentFile(); if(parent == null || !parent.exists()){ return File.separator; } return parent.getAbsolutePath(); } /** * 將文件複製到 * @param pathname 文件夾名稱 * @return
     */
    public File copyTo(String pathname) { File targetFile = new File(pathname); if(file.isDirectory()){ if(!targetFile.exists() || !targetFile.isDirectory()){ targetFile.mkdirs(); } log.info("Copy folder to: " + pathname); File[] childFiles = file.listFiles(); if(childFiles != null && childFiles.length > 0){ for(File childFile: childFiles){ String targetpath = targetFile.getAbsolutePath() + File.separator + childFile.getName(); FileUtils childUtils = FileUtils.create(childFile); childUtils.copyTo(targetpath); } } }else{ try(InputStream input = new FileInputStream(file); OutputStream output = new FileOutputStream(targetFile);){ IOUtils.copy(input, output); log.info("Copy file to: " + pathname); }catch(IOException e){ e.printStackTrace(); return null; } } return targetFile; } public boolean exist() { return file.exists(); } /** * 壓縮至文件夾 * @param foldername * @return
     */
    public File zipTo(String foldername, String zipname) { if(StringUtils.isEmpty(zipname)){ zipname = getNameWithOutSuffix() + ".zip"; } File targetFolder = new File(foldername); if(!targetFolder.exists() || !targetFolder.isDirectory()){ targetFolder.mkdirs(); } File targetFile = new File(targetFolder.getAbsolutePath() + File.separator + zipname); try(FileOutputStream output = new FileOutputStream(targetFile); CheckedOutputStream cos = new CheckedOutputStream(output, new CRC32()); ZipOutputStream zipout = new ZipOutputStream(cos);){ compress(file, zipout, ""); return targetFile; }catch(Exception e){ log.error("文件壓縮失敗:" + targetFile.getAbsolutePath()); return null; } } private void compress(File zipfile, ZipOutputStream out, String basedir) { if(zipfile.isDirectory()){ File[] files = zipfile.listFiles(); for(File childFile: files){ compress(childFile, out, basedir + zipfile.getName() + "/"); } }else{ try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipfile));){ ZipEntry entry = new ZipEntry(basedir + zipfile.getName()); out.putNextEntry(entry); IOUtils.copy(bis, out); }catch(Exception e){ log.error("壓縮失敗:" + zipfile.getAbsolutePath() + ": " + e.getMessage(), e); } } } public void delete(){ delete(file); } private boolean delete(File file) { if (file.isDirectory()) { String[] children = file.list(); for (int i=0; i<children.length; i++) { boolean success = delete(new File(file, children[i])); if (!success) { return false; } } } return file.delete(); } /** * 獲取文件MD5 * @return
     */
    public String md5() { return MD5Utils.getFileMD5String(file); } }
相關文章
相關標籤/搜索