生活随笔
收集整理的這篇文章主要介紹了
Java zip 压缩 文件夹删除,移动,重命名,复制
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
FileUtil.java
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/*** 文件操作* Created by heavenick on 2015/7/8.*/
public class FileUtil {public static void main(String[] args) throws IOException {copyFile("E:\\upload\\create\\1436144988371_JL33041594.xml","E:\\test\\upload");
// deleteFile("E:\\test\\upload\\");} /*** 移動(dòng) 文件或者文件夾* @param oldPath* @param newPath* @throws IOException*/public static void moveTo(String oldPath,String newPath) throws IOException {copyFile(oldPath,newPath);deleteFile(oldPath);}/*** 刪除 文件或者文件夾* @param filePath*/public static void deleteFile(String filePath){File file = new File(filePath);if (!file.exists()) {return;}if (file.isDirectory() ) {File[] list = file.listFiles();for (File f : list) {deleteFile(f.getAbsolutePath()) ;}}file.delete();}/*** 復(fù)制 文件或者文件夾* @param oldPath* @param newPath* @throws IOException*/public static void copyFile(String oldPath ,String newPath ) throws IOException {System.out.println("copy file from [" + oldPath + "] to [" + newPath +"]");File oldFile = new File(oldPath) ;if (oldFile.exists()) {if(oldFile.isDirectory()){ // 如果是文件夾File newPathDir = new File(newPath);newPathDir.mkdirs();File[] lists = oldFile.listFiles() ;if(lists != null && lists.length > 0 ){for (File file : lists) {copyFile(file.getAbsolutePath(), newPath.endsWith(File.separator) ? newPath + file.getName() : newPath + File.separator + file.getName()) ;}}}else {InputStream inStream = new FileInputStream(oldFile); //讀入原文件FileOutputStream fs = new FileOutputStream(newPath);write2Out(inStream ,fs) ;inStream.close();}}}/*** 重命名文件* @param file* @param name* @return*/public static File renameFile(File file , String name ){String fileName = file.getParent() + File.separator + name ;File dest = new File(fileName);file.renameTo(dest) ;return dest ;}/*** 壓縮多個(gè)文件。* @param zipFileName 壓縮輸出文件名* @param files 需要壓縮的文件* @return* @throws Exception*/public static File createZip(String zipFileName, File... files) throws Exception {File outFile = new File(zipFileName) ;ZipOutputStream out = null;BufferedOutputStream bo = null;try {out = new ZipOutputStream(new FileOutputStream(outFile));bo = new BufferedOutputStream(out);for (File file : files) {zip(out, file, file.getName(), bo);}} catch (Exception e) {e.printStackTrace();}finally {try {bo.close();} finally {out.close(); // 輸出流關(guān)閉}}return outFile;}/**** @param zipFileName 壓縮輸出文件名* @param inputFile 需要壓縮的文件* @return* @throws Exception*/public static File createZip(String zipFileName, File inputFile) throws Exception {File outFile = new File(zipFileName) ;ZipOutputStream out = null;BufferedOutputStream bo = null;try {out = new ZipOutputStream(new FileOutputStream(outFile));bo = new BufferedOutputStream(out);zip(out, inputFile, inputFile.getName(), bo);} catch (Exception e) {e.printStackTrace();}finally {try {bo.close();} finally {out.close(); // 輸出流關(guān)閉}}return outFile;}private static void zip(ZipOutputStream out, File f, String base,BufferedOutputStream bo) throws Exception { // 方法重載if (f.isDirectory()) {File[] fl = f.listFiles();if ( fl == null || fl.length == 0) {out.putNextEntry(new ZipEntry(base + "/")); // 創(chuàng)建創(chuàng)建一個(gè)空的文件夾}else{for (int i = 0; i < fl.length; i++) {zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 遞歸遍歷子文件夾}}} else {out.putNextEntry(new ZipEntry(base)); // 創(chuàng)建zip壓縮進(jìn)入 base 文件System.out.println(base);BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f));try {write2Out(bi,out) ;} catch (IOException e) {//Ignore}finally {bi.close();// 輸入流關(guān)閉}}}private static void write2Out(InputStream input , OutputStream out) throws IOException {byte[] b = new byte[1024];int c = 0 ;while ( (c = input.read(b)) != -1 ) {out.write(b,0,c);out.flush();}out.flush();}
}
轉(zhuǎn)載于:https://www.cnblogs.com/qixidi/p/10213564.html
總結(jié)
以上是生活随笔為你收集整理的Java zip 压缩 文件夹删除,移动,重命名,复制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。