文件解压,文件/目录更名,文件/目录删除,文件/目录移动

文件解压,文件/目录更名,文件/目录删除,文件/目录移动

 

 

 

 

 

将指定的src文件解压到dstDir目录

 

public static void unzip(File src, File dstDir) throws IOException
	{
		ZipFile zipFile = null;
		try
		{
			// 创建zip文件对象
			zipFile = new ZipFile(src);
			// 得到zip文件条目枚举对象
			Enumeration zipEnum = zipFile.getEntries();
			// 定义输入输出流对象
			// 定义对象
			ZipEntry entry = null;
			// 循环读取条目
			while (zipEnum.hasMoreElements())
			{
				// 得到当前条目
				entry = (ZipEntry) zipEnum.nextElement();
				String entryName = new String(entry.getName());
				// 用/分隔条目名称
				String names[] = entryName.split("\\/");
				int length = names.length;
				String path = dstDir.getAbsolutePath();
				for (int v = 0; v < length; v++)
				{
					if (v < length - 1)
					{ // 最后一个目录之前的目录
						path += "/" + names[v] + "/";
						createDir(path);
					}
					else
					{ // 最后一个
						if (entryName.endsWith("/")) // 为目录,则创建文件夹
							createDir(dstDir.getAbsolutePath() + "/" + entryName);
						else
						{

							InputStream input = null;
							OutputStream output = null;
							try
							{// 为文件,则输出到文件
								input = zipFile.getInputStream(entry);
								output = new FileOutputStream(new File(dstDir.getAbsolutePath() + "/" + entryName));
								byte[] buffer = new byte[1024 * 8];
								int readLen = 0;
								while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
									output.write(buffer, 0, readLen);
								// 关闭流
								output.flush();
							}
							finally
							{
								if (input != null)
									input.close();
								if (output != null)
									output.close();
							}
						}
					}
				}
			}
		}
		finally
		{
			if (zipFile != null)
				zipFile.close();
		}
	}

 

 

该目录下 所有文件 集合

 File[] subFiles = file.listFiles(new FileFilter() {
      @Override
	 public boolean accept(File pathname)
	 {
	 if (pathname.isFile())
		 return true;
	 return false;
	 }
 });

 

 

与文件相关的工具类库。

其内容包括文件/目录更名,文件/目录删除,文件/目录移动。

public class FileUtil
{
	protected static Logger logger = LoggerFactory.getLogger(FileUtil.class);

	public static boolean copyDirectory(String oldPath, String newPath) {
		File b = new File(newPath);
		File a = new File(oldPath);
		try {
			FileUtils.copyDirectoryToDirectory(a, b);
		}
		catch (IOException e) {
			logger.error("", e);
			return false;
		}
		return true;
	}

	public static boolean moveDirectory(String oldPath, String newPath) {
		File b = new File(newPath);
		File a = new File(oldPath);
		return a.renameTo(b);
	}

	/**
	 * 将文件oldFile移为newFile
	 * 
	 * @param oldFile
	 * @param newFile
	 * @return
	 */
	public static boolean moveFile(String oldFile, String newFile) {
		File a = new File(oldFile);
		File b = new File(newFile);
		return moveFile(a, b);
	}

	/**
	 * 将文件移至指定目录,文件名不变
	 * 
	 * @param file
	 * @param path
	 * @return
	 */
	public static boolean moveFile(File file, String path) {
		File tmp = new File(path);
		if (!tmp.exists())
			tmp.mkdirs();
		File b = new File(path + File.separatorChar + file.getName());
		return moveFile(file, b);
	}

	/**
	 * 将文件移至指定目录,文件名不变
	 * 
	 * @param file
	 * @param path
	 * @return 移动是否成功
	 */
	public static boolean moveFile(File srcFile, File dstFile) {
		return srcFile.renameTo(dstFile);
	}

	public static boolean deleteDirectory(String sPath)
	{
		// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
		if (!sPath.endsWith(File.separator)) {
			sPath = sPath + File.separator;
		}
		File dirFile = new File(sPath);
		// 如果dir对应的文件不存在,或者不是一个目录,则退出
		if (!dirFile.exists() || !dirFile.isDirectory())
			return false;
		return deleteDirectory(dirFile);
	}

	public static boolean copyFile(String oldPath, String newPath)
	{
		boolean bool = false;
		File newFile = new File(newPath);
		InputStream inStream = null;
		OutputStream outStream = null;
		try {
			// 复制文件(保留上传的原文件,新的文件名为id_name.zip)
			inStream = new FileInputStream(oldPath);
			newFile.createNewFile();
			outStream = new FileOutputStream(newPath);
			byte[] by = new byte[2048];
			while (inStream.available() > 0)
			{
				int i = inStream.read(by);
				outStream.write(by, 0, i);
			}
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
		}
		finally
		{
			if (null != inStream) {
				try {
					inStream.close();
				}
				catch (IOException e) { }
			}
			if (null != outStream) {
				try {
					outStream.flush();
				}
				catch (IOException e) {
					System.err.println(e.getCause());
				}
				try {
					outStream.close();
				}
				catch (IOException e) {
					System.err.println(e.getCause());
				}
				bool = true;
				System.out.println("复制完毕! ");
			}
		}
		return bool;
	}

	public static boolean deleteDirectory(File f)
	{
		boolean flag = true;
		// 删除文件夹下的所有文件(包括子目录)
		File[] files = f.listFiles();
		for (int i = 0; i < files.length; i++)
		{
			// 删除子文件
			if (files[i].isFile()) {
				flag = deleteFile(files[i].getAbsolutePath());
				if (!flag)
					break;
			} // 删除子目录
			else {
				flag = deleteDirectory(files[i].getAbsolutePath());
				if (!flag)
					break;
			}
		}
		if (!flag)
			return false;
		// 删除当前目录
		if (f.delete()) {
			return true;
		}
		else {
			return false;
		}
	}

	public static boolean deleteFile(String sPath)
	{
		File file = new File(sPath);
		if (file.isFile() && file.exists()) {
			return file.delete();
		}
		return false;
	}

	public static String getTomcatRootPath(HttpServletRequest req) {
		String s = req.getSession().getServletContext().getRealPath("/");
		s = new File(s).getParent() + File.separatorChar + "ROOT";
		return s;
	}

	/**
	 * 装输入流写入文件中
	 * 
	 * @param is
	 * @param f
	 * @return
	 */
	public static boolean writeFile(InputStream is, File f)
	{
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(f);
			byte[] buf = new byte[10240];
			int i;
			while ((i = is.read(buf)) > 0)
				fos.write(buf, 0, i);
		}
		catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		finally {
			if (is != null)
				try {
					is.close();
				} catch (IOException e) { }
			if (fos != null)
				try {
					fos.close(); 
				} catch (IOException e) { }
		}
		return true;
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值