`
mengqingyu
  • 浏览: 328579 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

Java文件压缩(apache.tools.zip)

阅读更多
由于jdk自带的压缩功能存在编码问题,所以使用apache工具包。
	/**
	 * 
	 * @function:文件zip压缩
	 * @param zipPath
	 *            压缩目标路径
	 * @param srcPath
	 *            被压缩文件路径
	 * @throws BusinessException
	 * @author: mengqingyu 2013-8-22 上午10:59:37
	 */
	public static void zipFiles(String zipPath, String srcPath) throws BusinessException {
		File srcFile = new File(srcPath);
		ZipOutputStream zos = null;
		InputStream is = null;
		byte[] buf = null;
		try {
			zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath)));
			is = new BufferedInputStream(new FileInputStream(srcFile));
			buf = new byte[is.available()];
			is.read(buf);
		} catch (Exception e) {
			log.error(e);
		} finally {
			try {
				is.close();
			} catch (IOException e1) {
				log.error(e1);
			}
		}
		try {
			zos.putNextEntry(new ZipEntry(srcFile.getName()));
			zos.write(buf);
			zos.setEncoding("gbk");
		} catch (IOException e) {
			log.error(e);
		} finally {
			try {
				zos.closeEntry();
				zos.close();
			} catch (IOException e1) {
				log.error(e1);
			}
		}
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics