用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java 压缩文件 解压缩文件

2013-01-08 作者: 海大软件1102班举报

[java]代码库

	/**
	 * compress file
	 * 
	 * @param in
	 * @param out
	 * @param compressionAlgorithm
	 * @param calucateCompressedMd5
	 * @param listener
	 * @return
	 * @throws TemplateServiceException
	 */
	public final static String compress(File in, File out,
			String compressionAlgorithm, boolean calucateCompressedMd5,
			FileHandleListener listener) throws TemplateServiceException {

		try {
			FileInputStream fis = new FileInputStream(in);
			FileOutputStream fos = new FileOutputStream(out);
			return compress(fis, fos, compressionAlgorithm,
					calucateCompressedMd5, listener);
		} catch (FileNotFoundException e) {
			throw new TemplateServiceException(
					TemplateServiceException.FILE_NOT_EXIST_ERROR,
					new String[] { in.getPath() });
		}

	}

	public final static String compress(InputStream in, OutputStream out,
			String compressionAlgorithm, boolean calucateCompressedMd5,
			FileHandleListener listener) throws TemplateServiceException {
		try {
			MessageDigest md = null;
			if (calucateCompressedMd5) {
				md = MessageDigest.getInstance("md5");
				out = new DigestOutputStream(out, md);
			}

			CompressorStreamFactory csf = new CompressorStreamFactory();
			out = csf.createCompressorOutputStream(
					toCompressorAlgorithm(compressionAlgorithm), out);

			if (listener != null) {
				out = new FileHandleOutputStream(out, listener);
			}

			FileCopyUtils.copy(in, out, null);

			if (calucateCompressedMd5) {
				byte[] md5Bytes = md.digest();
				String md5 = OutputFormatter.binaryToHex(md5Bytes);
				return md5;
			}

			return null;
		} catch (NoSuchAlgorithmException e) {
			throw new TemplateServiceException(
					TemplateServiceException.UNKNOWN_ERROR);
		} catch (CompressorException e) {
			throw new TemplateServiceException(
					TemplateServiceException.UNKNOWN_ERROR);
		}

	}

	/**
	 * uncompress file
	 * 
	 * @param in
	 * @param out
	 * @param compressionAlgorithm
	 * @param calucateUncompressedMd5
	 * @param listener
	 * @return
	 * @throws TemplateServiceException
	 */
	public final static String uncompress(File in, File out,
			String compressionAlgorithm, boolean calucateUncompressedMd5,
			FileHandleListener listener) throws TemplateServiceException {
		try {
			FileInputStream fis = new FileInputStream(in);
			FileOutputStream fos = new FileOutputStream(out);
			return uncompress(fis, fos, compressionAlgorithm,
					calucateUncompressedMd5, listener);
		} catch (FileNotFoundException e) {
			throw new TemplateServiceException(
					TemplateServiceException.FILE_NOT_EXIST_ERROR,
					new String[] { in.getPath() });
		}
	}

	public final static String uncompress(InputStream in, OutputStream out,
			String compressionAlgorithm, boolean calucateUncompressedMd5,
			FileHandleListener listener) throws TemplateServiceException {
		try {
			MessageDigest md = null;
			if (calucateUncompressedMd5) {
				md = MessageDigest.getInstance("md5");
				out = new DigestOutputStream(out, md);
			}

			CompressorStreamFactory csf = new CompressorStreamFactory();
			in = csf.createCompressorInputStream(
					toCompressorAlgorithm(compressionAlgorithm), in);

			if (listener != null) {
				out = new FileHandleOutputStream(out, listener);
			}

			FileCopyUtils.copy(in, out, null);

			if (calucateUncompressedMd5) {
				byte[] md5Bytes = md.digest();
				String md5 = OutputFormatter.binaryToHex(md5Bytes);
				return md5;
			}

			return null;
		} catch (NoSuchAlgorithmException e) {
			throw new TemplateServiceException(
					TemplateServiceException.UNKNOWN_ERROR);
		} catch (CompressorException e) {
			throw new TemplateServiceException(
					TemplateServiceException.UNKNOWN_ERROR);
		}
	}

	public final static String toCompressorAlgorithm(String compressionAlgorithm) {
		if ("bz2".equalsIgnoreCase(compressionAlgorithm)
				|| "bzip2".equalsIgnoreCase(compressionAlgorithm)) {
			return CompressorStreamFactory.BZIP2;
		} else if ("gz".equalsIgnoreCase(compressionAlgorithm)
				|| "gzip".equalsIgnoreCase(compressionAlgorithm)) {
			return CompressorStreamFactory.GZIP;
		} else if ("xz".equalsIgnoreCase(compressionAlgorithm)) {
			return CompressorStreamFactory.XZ;
		} else if ("pack200".equalsIgnoreCase(compressionAlgorithm)) {
			return CompressorStreamFactory.PACK200;
		} else {
			return compressionAlgorithm;
		}
	}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...