使用ant.jar 解压文件 把asset文件夹中的内容复制到SD卡上

本文介绍了一个用于压缩文件夹到ZIP文件,并使用MD5算法进行加密的方法。该方法通过递归调用实现了文件夹及其子文件夹内所有文件的压缩,并提供了ZIP文件的MD5校验功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.packtest.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

import android.os.Handler;

public class ZipUtil {
	/**
	 * 压缩文件夹
	 * 
	 * @param zipPath
	 *            生成的zip文件路径
	 * @param filePath
	 *            需要压缩的文件夹路径
	 * @throws Exception
	 */
	public void zipFolder(String zipPath, String filePath) throws Exception {
		ZipOutputStream out = null;
		try {
			out = new ZipOutputStream(new FileOutputStream(zipPath));
			File f = new File(filePath);
			zipFiles(out, f, "");
		} finally {
			if (out != null)
				out.close();
		}
	}

	/**
	 * 递归调用,压缩文件夹和子文件夹的所有文件
	 * 
	 * @param out
	 * @param f
	 * @param base
	 * @throws Exception
	 */
	private void zipFiles(ZipOutputStream out, File f, String base) throws Exception {
		FileInputStream in = null;
		try {
			if (f.isDirectory()) {
				File[] fl = f.listFiles();
				out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
				base = base.length() == 0 ? "" : base + "/";
				for (int i = 0; i < fl.length; i++) {
					zipFiles(out, fl[i], base + fl[i].getName());// 递归压缩子文件夹
				}
			} else {
				out.putNextEntry(new ZipEntry(base));
				in = new FileInputStream(f);
				int b;
				while ((b = in.read()) != -1) {
					out.write(b);
				}
			}
		} finally {
			if (in != null)
				in.close();
		}

	}

	/**
	 * 压缩文件夹和子文件夹的所有文件
	 * 
	 * @param out
	 * @param f
	 * @param base
	 * @throws Exception
	 */
	public static void zipFiles(String srcPath, String targetPath) {
		try {
			// zip.zipFolder(zipPath, filePath);//压缩文件夹
			File srcdir = new File(srcPath);
			if (!srcdir.exists()) {
				throw new RuntimeException(srcPath + "不存在!");
			}
			File targetdir = new File(targetPath);
			if (targetdir.exists()) {
				targetdir.delete();
			}
			Project prj = new Project();
			Zip zip = new Zip();
			zip.setProject(prj);
			zip.setDestFile(targetdir);
			FileSet fileSet = new FileSet();
			fileSet.setProject(prj);
			fileSet.setDir(srcdir);
			// fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹
			// eg:zip.setIncludes("*.java");
			// fileSet.setExcludes(...); 排除哪些文件或文件夹
			zip.addFileset(fileSet);
			zip.execute();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * MD5加密zip文件
	 * 
	 * @param out
	 * @param f
	 * @param base
	 * @throws Exception
	 */
	public static String zipToMd5(String path) {
		StringBuffer md5 = new StringBuffer();
		FileInputStream fis = null;
		try {
			File f = new File(path);
			if (!f.exists()) {
				throw new RuntimeException(path + "不存在!");
			}
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] str = new byte[2048];
			int length = -1;
			fis = new FileInputStream(f);
			try {
				while ((length = fis.read(str)) != -1) {
					md.update(str, 0, length);
				}
				byte[] result = md.digest();
				for (int i = 0; i < result.length; i++) {
					md5.append(result[i]);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {// jsy 2012-05-12 add
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}// end
		return md5.toString();
	}

	public static void CreateZipFile(String filePath, String zipFilePath) {
		FileOutputStream fos = null;
		ZipOutputStream zos = null;
		try {
			fos = new FileOutputStream(zipFilePath);
			zos = new ZipOutputStream(fos);
			writeZipFile(new File(filePath), zos, "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (zos != null)
					zos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private static void writeZipFile(File f, ZipOutputStream zos, String hiberarchy) {
		if (f.exists()) {
			if (f.isDirectory()) {
				hiberarchy += f.getName() + "/";
				File[] fif = f.listFiles();
				for (int i = 0; i < fif.length; i++) {
					writeZipFile(fif[i], zos, hiberarchy);
				}

			} else {
				FileInputStream fis = null;
				try {
					fis = new FileInputStream(f);
					ZipEntry ze = new ZipEntry(hiberarchy + f.getName());
					zos.putNextEntry(ze);
					byte[] b = new byte[1024];
					while (fis.read(b) != -1) {
						zos.write(b);
						b = new byte[1024];
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						if (fis != null)
							fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
		}

	}

	public static boolean Ectract(String sZipPathFile, String sDestPath) {
		boolean flag = false;
		try {
			// 先指定压缩档的位置和档名,建立FileInputStream对象
			FileInputStream fins = new FileInputStream(sZipPathFile);
			// 将fins传入ZipInputStream中
			ZipInputStream zins = new ZipInputStream(fins);
			ZipEntry ze = null;
			byte ch[] = new byte[8192];
			while ((ze = zins.getNextEntry()) != null) {
				File zfile = new File(sDestPath + ze.getName());
				File fpath = new File(zfile.getParentFile().getPath());
				if (ze.isDirectory()) {
					if (!zfile.exists())
						zfile.mkdirs();
					zins.closeEntry();
				} else {
					if (!fpath.exists())
						fpath.mkdirs();
					FileOutputStream fouts = new FileOutputStream(zfile);
					int i;
					while ((i = zins.read(ch)) != -1)
						fouts.write(ch, 0, i);
					zins.closeEntry();
					fouts.close();
				}
			}
			fins.close();
			zins.close();
			flag = true;
			File file = new File(sZipPathFile);
			file.delete();
		} catch (Exception e) {
			flag = false;
		} finally {
//			if (!flag) {
//				String[] namesArray = new String[] { "detailicon", "listicon", "optionicon", "quickscanicon", "secclassicon", "smallicon" };
//				for (int i = 0; i < namesArray.length; i++) {
//					File file = new File(sDestPath + namesArray[i]);
//					if (file.exists())
//						DownLoadPic.deleteDir(file);
//				}
//
//			}
		}
		return flag;
	}

	public static boolean zipToFile(String sZipPathFile, String sDestPath) throws IOException {
		boolean flag = false;
		FileInputStream fins = null;
		ZipInputStream zins = null;
		FileOutputStream fouts = null;
		try {
			fins = new FileInputStream(sZipPathFile);
			zins = new ZipInputStream(fins);
			ZipEntry ze = null;
			byte ch[] = new byte[2048];
			while ((ze = zins.getNextEntry()) != null) {
				File zfile = new File(sDestPath + "//" + ze.getName());
				File fpath = new File(zfile.getParentFile().getPath());
				if (ze.isDirectory()) {
					if (!zfile.exists())
						zfile.mkdirs();
					zins.closeEntry();
				} else {
					if (!fpath.exists())
						fpath.mkdirs();
					fouts = new FileOutputStream(zfile);
					int i;
					String zfilePath = zfile.getAbsolutePath();
					while ((i = zins.read(ch)) != -1)
						fouts.write(ch, 0, i);
					zins.closeEntry();

					if (zfilePath.endsWith(".zip")) {
						zipToFile(zfilePath, zfilePath.substring(0, zfilePath.lastIndexOf(".zip")));
					}
				}
			}

			// 如果需要解压完后删除ZIP文件可以
			File file = new File(sZipPathFile);
			file.delete();
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		} finally {
			if (fouts != null)
				fouts.close();
			if (zins != null)
				zins.close();
			if (fins != null)
				fins.close();
		}
		return flag;
	}
	
	
	public static boolean zipToFile(InputStream inputStream, String sDestPath) throws IOException {
		boolean flag = false;
		ZipInputStream zins = null;
		FileOutputStream fouts = null;
		try {
			zins = new ZipInputStream(inputStream);
			ZipEntry ze = null;
			byte ch[] = new byte[2048];
			while ((ze = zins.getNextEntry()) != null) {
				File zfile = new File(sDestPath + "//" + ze.getName());
				File fpath = new File(zfile.getParentFile().getPath());
				if (ze.isDirectory()) {
					if (!zfile.exists())
						zfile.mkdirs();
					zins.closeEntry();
				} else {
					if (!fpath.exists())
						fpath.mkdirs();
					fouts = new FileOutputStream(zfile);
					int i;
					String zfilePath = zfile.getAbsolutePath();
					while ((i = zins.read(ch)) != -1)
						fouts.write(ch, 0, i);
					zins.closeEntry();

					if (zfilePath.endsWith(".zip")) {
						zipToFile(zfilePath, zfilePath.substring(0, zfilePath.lastIndexOf(".zip")));
					}
				}
			}

			// 如果需要解压完后删除ZIP文件可以
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		} finally {
			if (fouts != null)
				fouts.close();
			if (zins != null)
				zins.close();
		}
		
		return flag;
	}

	/**
	 * @param 删除Date之前的压缩包
	 * @param f
	 * @param base
	 * @throws Exception
	 */
//	public static void delZip(Date date, String path) {
//		try {
//			long d = date.getTime();
//			String targetPath = ConfigUtil.getPropValue(ConstantUtil.BASIC_PROP, "zip");
//			File f = new File(path + targetPath);
//			File[] files = f.listFiles();
//			for (int i = 0; i < files.length; i++) {
//				if (files[i].isFile()) {
//					long f_date = files[i].lastModified();
//					if (f_date < d) {
//						files[i].delete();
//					}
//				}
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	// 复制文件夹
	public static void copy(String fromPath, String toPath) throws IOException {
		PrintWriter pfp = null;
		FileReader fr = null;
		BufferedReader br = null;
		try {
			File f1 = new File(fromPath);
			if (f1.isDirectory()) {

			} else {
				// 读
				fr = new FileReader(fromPath);
				br = new BufferedReader(fr);
				String temp = "";
				String line = null;
				while ((line = br.readLine()) != null) {
					temp += line;
				}
				// 写
				File f2 = new File(toPath);
				f2.mkdir();
				File f3 = new File(f2, f1.getName());

				pfp = new PrintWriter(f3);
				pfp.print(temp);

			}
		} catch (Exception e) {
			e.getStackTrace();
		} finally {
			if (pfp != null)
				pfp.close();
			if (br != null)
				br.close();
			if (fr != null)
				fr.close();
		}
	}

	public static void main(String args[]) throws Exception {
		zipToFile("H:\\work\\TFFServer\\WebRoot\\dish_pic\\zip\\010-2-20120411164316139.zip", "\\H:\\work\\TFFServer\\WebRoot\\dish_pic\\offical_pic");

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值