- /**
- * 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;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/160420149324.html
来源: http://www.codesnippet.cn/detail/160420149324.html