java解压、压缩、操作ZIP

1.文件的压缩

    压缩文件夹

	/**
	 * inputFileName 输入一个文件夹 zipFileName 输出一个压缩文件夹
	 */
	public static void zip(String inputFileName) throws Exception {
		String zipFileName = "c:\\test.zip"; // 打包后文件名字
		System.out.println(zipFileName);
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
				zipFileName));
		zip(out, new File(inputFileName), "");
		System.out.println("zip done");
		out.close();
	}

	private static void zip(ZipOutputStream out, File f, String base) throws Exception {
		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++) {
				zip(out, fl[i], base + fl[i].getName());
			}
		} else {
			out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
			FileInputStream in = new FileInputStream(f);
			int b;
			System.out.println(base);
			while ((b = in.read()) != -1) {
				out.write(b);
			}
			in.close();
		}
	}


    压缩文件

	public static void zip(ZipOutputStream out, File f) throws Exception {
  		String zipFileName = "c:\\test.zip"; // 打包后文件名字
  		System.out.println(zipFileName);
  		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
  		out.putNextEntry(new org.apache.tools.zip.ZipEntry(f.getName()));
  		FileInputStream in = new FileInputStream(f);
  		int b;
  		System.out.println(base);
  		while ((b = in.read()) != -1) {
  		 out.write(b);
  		}
  		in.close();
	}

 

压缩指定内容(比如流、文本)

	ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(path),1024));
	//写入文本文件
	StringBuffer xml = new StringBuffer("123");
	zout.putNextEntry(new ZipEntry("1.txt"));
	byte[] txtbuffer = xml.toString().getBytes("GBK");
	zout.write(buffer, 0, txtbuffer.length);
	//关闭压缩Entry对象
	zout.closeEntry();
	//关闭压缩输出流
	zout.close();

 

解压

	/**
	 * FunName: unzip Description: 文件解压缩
	 * 
	 * @param zipFilePath
	 *            源文件路径
	 * @param unzipDirectory
	 *            解压后的路径
	 * @author jiale
	 * @create Date 2014-1-2 13:33:39
	 */
	public static void unzip(String zipFilePath, String unzipDirectory)
			throws Exception {
		// 创建文件对象
		File file = new File(zipFilePath);
		// 创建zip文件对象
		ZipFile zipFile = new ZipFile(file);
		// 创建本zip文件解压目录
		File unzipFile = new File(unzipDirectory + "/"
				+ getSuffixName(file.getName()));
		if (unzipFile.exists())
			unzipFile.delete();
		unzipFile.mkdir();
		// 得到zip文件条目枚举对象
		Enumeration zipEnum = zipFile.getEntries();
		// 定义输入输出流对象
		InputStream input = null;
		OutputStream output = null;
		// 定义对象
		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 = unzipFile.getAbsolutePath();
			for (int v = 0; v < length; v++) {
				if (v < length - 1) { // 最后一个目录之前的目录
					path += "/" + names[v] + "/";
					createDir(path);
				} else { // 最后一个
					if (entryName.endsWith("/")) // 为目录,则创建文件夹
						createDir(unzipFile.getAbsolutePath() + "/" + entryName);
					else { // 为文件,则输出到文件
						input = zipFile.getInputStream(entry);
						output = new FileOutputStream(new File(unzipFile
								.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);
						// 关闭流
						input.close();
						output.flush();
						output.close();
					}
				}
			}
		}
	}


 


操作ZIP

	public String importCardInfo(String filepath,String user) throws Exception {
		// 日志信息
		String loginfo = "";
		// 照片集合
		Map<String, InputStream> photos = new HashMap<String, InputStream>();
		// 人员信息集合
		List<Person4> persons = new ArrayList<Person4>();
		List<Person4> problem = new ArrayList<Person4>();
		List temp = null;
		Person4 p = null;
		// 解析ZIP
		ZipFile zf = new ZipFile(filepath);
		Enumeration e = zf.getEntries();
		ZipEntry ze;
		try {

			while (e.hasMoreElements()) {
				ze = (ZipEntry) e.nextElement();
				if (ze.isDirectory()) {
				} else {
					// System.err.println("file - " + ze.getName() + " : "
					// + ze.getSize() + " bytes");
					long size = ze.getSize();
					// 解析文本
					if (size > 0 && ze.getName().endsWith(".txt")) {
						System.out.println("解析文本信息" + filepath);

						Person4 person = null;
						BufferedReader br = new BufferedReader(
								new InputStreamReader(zf.getInputStream(ze)));
						String line;
						int ii = 0;// 解析文本控制
						while ((line = br.readLine()) != null) {
							if (ii++ > 0) {
								temp = analyze(line);
								if ((Boolean) temp.get(0)) {
									persons.add((Person4) temp.get(2));
								} else {
									p = (Person4) temp.get(2);
									p.setProblem(temp.get(1) + "");
									problem.add(p);
								}
							}
						}
						br.close();
						int perCount = comparePCDAO.importCardInfo(persons);
						int pperCount = comparePCDAO.importCardProblemInfo(problem);
						System.out.println("信息成功添加数量" + perCount);
						loginfo += "信息添加成功数量" + perCount + " ";
						loginfo += "信息添加失败数量" + pperCount + " ";
						//记录上传信息
						comparePCDAO.scpcinfo(ze.getName().substring(0,ze.getName().length()-4), perCount+pperCount+"", perCount+"", pperCount+"", user);
					}

					// 解析照片
					if (size > 0 && ze.getName().endsWith(".jpg")) {
						InputStream ins = zf.getInputStream(ze);
						photos.put(ze.getName(), ins);
					}
					System.out.println();
				}
			}
			 System.out.println("照片数量:" + photos.size());
			 int phoCount = comparePCDAO.importPhotoInfo(photos);
			 System.out.println("照片成功添加数量:" + phoCount);
			 loginfo += "照片成功添加数量:" + phoCount;
			zf.close();
		} catch (Exception e1) {
			e1.printStackTrace();
			zf.close();
			System.out.println("*******************************************");
			throw new Exception("记录日志");
			
		}
		return loginfo;
	}


 

 

 

 


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值