java 用在把一个文件夹下的所有文件都放到一个文件夹下,删除多层文件夹

时间:2021-04-26 09:56:03

        java 用在把一个文件夹下的所有文件都放到一个文件夹下,删除多层文件夹

     该小程序是一个完整的功能,可以直接使用

package com.yangjf.service.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.log4j.Logger;


public class FolderFileToPack {

	public static List<String> listFile = new ArrayList<String>();
	
	public static List<String> listFileName = new ArrayList<String>();

	private  final Logger logger = Logger.getLogger(getClass());
	
	/**
	 * 将存放在sourceFilePath目录下的源文件夹下的所有文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
	 * (用在把一个文件夹下的所有文件都放到一个文件夹下,删除多层文件夹)
	 * @param sourceFilePath
	 *            :待压缩的文件路径
	 * @param zipFilePath
	 *            :压缩后存放路径
	 * @param fileName
	 *            :压缩后文件的名称
	 * @return
	 */
	public static boolean fileToZip(String sourceFilePath,String zipFilePath, String fileName) {
		boolean flag = false;
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		ZipOutputStream zos = null;
		getFile(sourceFilePath);
		try {
			File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
			if (zipFile.exists()) {
				System.out.println(zipFilePath + "目录下存在名字为:" + fileName
						+ ".zip" + "的打包文件.");
			} else { 
				fos = new FileOutputStream(zipFile);
				zos = new ZipOutputStream(new BufferedOutputStream(fos));
				byte[] bufs = new byte[1024 * 1024];
				for (int i = 0; i < listFile.size(); i++) {
					try {
						//把文件路径按“\”划分,获取最后一个也就是文件名
						String[] listFileSome = listFile.get(i).split("\\\\");
						int listLength = listFileSome.length;
						String oneFileName  = listFileSome[listLength-1];
						//查重文件名,重复加数字
						oneFileName = checkFileName(oneFileName,0);
						//创建ZIP实体,并添加进压缩包
						ZipEntry zipEntry = new ZipEntry(oneFileName);
						zos.putNextEntry(zipEntry);
						// 读取待压缩的文件并写进压缩包里
						fis = new FileInputStream(listFile.get(i));
						bis = new BufferedInputStream(fis, 1024 * 1024);
						int read = 0;
						while ((read = bis.read(bufs, 0, 1024 * 1024)) != -1) {
							zos.write(bufs, 0, read);
						}
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						//logger.error("");
					}
					
				}
				flag = true;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			// 关闭流
			try {
				if (null != bis)
					bis.close();
				if (null != zos)
					zos.close();
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return flag;
	}

	//查重文件名,最后添加进去
	public static String checkFileName(String oneFileName,int i){
		
		if(listFileName.contains(oneFileName)){
			String[] partName = oneFileName.split("\\.");
			oneFileName = partName[0]+ ++i+"."+partName[1];
			checkFileName(oneFileName,i);
		}else{
			listFileName.add(oneFileName);
		}
		return oneFileName;
		
	}
	
	public static void getFile(String path) {
		File file = new File(path);
		File[] tempList = file.listFiles();
		for (File f : tempList) {
			if (f.isFile()) {
				listFile.add(f.getPath());
				System.out.println(f.getPath());
				continue;
			}
			if (f.isDirectory()) {
				getFile(f.getPath());
			}
		}

	}

	public static void main(String[] args) {
		String sourceFilePath = "F:\\uploadT";
		String zipFilePath = "F:\\log";
		String fileName = "manyFileType02";
		fileToZip(sourceFilePath,zipFilePath, fileName);

	}

}