[Java] java 压缩文件 解压缩文件 →→→→→进入此内容的聊天室

来自 , 2021-01-23, 写在 Java, 查看 129 次.
URL http://www.code666.cn/view/71a3cb15
  1.         /**
  2.          * compress file
  3.          *
  4.          * @param in
  5.          * @param out
  6.          * @param compressionAlgorithm
  7.          * @param calucateCompressedMd5
  8.          * @param listener
  9.          * @return
  10.          * @throws TemplateServiceException
  11.          */
  12.         public final static String compress(File in, File out,
  13.                         String compressionAlgorithm, boolean calucateCompressedMd5,
  14.                         FileHandleListener listener) throws TemplateServiceException {
  15.  
  16.                 try {
  17.                         FileInputStream fis = new FileInputStream(in);
  18.                         FileOutputStream fos = new FileOutputStream(out);
  19.                         return compress(fis, fos, compressionAlgorithm,
  20.                                         calucateCompressedMd5, listener);
  21.                 } catch (FileNotFoundException e) {
  22.                         throw new TemplateServiceException(
  23.                                         TemplateServiceException.FILE_NOT_EXIST_ERROR,
  24.                                         new String[] { in.getPath() });
  25.                 }
  26.  
  27.         }
  28.  
  29.         public final static String compress(InputStream in, OutputStream out,
  30.                         String compressionAlgorithm, boolean calucateCompressedMd5,
  31.                         FileHandleListener listener) throws TemplateServiceException {
  32.                 try {
  33.                         MessageDigest md = null;
  34.                         if (calucateCompressedMd5) {
  35.                                 md = MessageDigest.getInstance("md5");
  36.                                 out = new DigestOutputStream(out, md);
  37.                         }
  38.  
  39.                         CompressorStreamFactory csf = new CompressorStreamFactory();
  40.                         out = csf.createCompressorOutputStream(
  41.                                         toCompressorAlgorithm(compressionAlgorithm), out);
  42.  
  43.                         if (listener != null) {
  44.                                 out = new FileHandleOutputStream(out, listener);
  45.                         }
  46.  
  47.                         FileCopyUtils.copy(in, out, null);
  48.  
  49.                         if (calucateCompressedMd5) {
  50.                                 byte[] md5Bytes = md.digest();
  51.                                 String md5 = OutputFormatter.binaryToHex(md5Bytes);
  52.                                 return md5;
  53.                         }
  54.  
  55.                         return null;
  56.                 } catch (NoSuchAlgorithmException e) {
  57.                         throw new TemplateServiceException(
  58.                                         TemplateServiceException.UNKNOWN_ERROR);
  59.                 } catch (CompressorException e) {
  60.                         throw new TemplateServiceException(
  61.                                         TemplateServiceException.UNKNOWN_ERROR);
  62.                 }
  63.  
  64.         }
  65.  
  66.         /**
  67.          * uncompress file
  68.          *
  69.          * @param in
  70.          * @param out
  71.          * @param compressionAlgorithm
  72.          * @param calucateUncompressedMd5
  73.          * @param listener
  74.          * @return
  75.          * @throws TemplateServiceException
  76.          */
  77.         public final static String uncompress(File in, File out,
  78.                         String compressionAlgorithm, boolean calucateUncompressedMd5,
  79.                         FileHandleListener listener) throws TemplateServiceException {
  80.                 try {
  81.                         FileInputStream fis = new FileInputStream(in);
  82.                         FileOutputStream fos = new FileOutputStream(out);
  83.                         return uncompress(fis, fos, compressionAlgorithm,
  84.                                         calucateUncompressedMd5, listener);
  85.                 } catch (FileNotFoundException e) {
  86.                         throw new TemplateServiceException(
  87.                                         TemplateServiceException.FILE_NOT_EXIST_ERROR,
  88.                                         new String[] { in.getPath() });
  89.                 }
  90.         }
  91.  
  92.         public final static String uncompress(InputStream in, OutputStream out,
  93.                         String compressionAlgorithm, boolean calucateUncompressedMd5,
  94.                         FileHandleListener listener) throws TemplateServiceException {
  95.                 try {
  96.                         MessageDigest md = null;
  97.                         if (calucateUncompressedMd5) {
  98.                                 md = MessageDigest.getInstance("md5");
  99.                                 out = new DigestOutputStream(out, md);
  100.                         }
  101.  
  102.                         CompressorStreamFactory csf = new CompressorStreamFactory();
  103.                         in = csf.createCompressorInputStream(
  104.                                         toCompressorAlgorithm(compressionAlgorithm), in);
  105.  
  106.                         if (listener != null) {
  107.                                 out = new FileHandleOutputStream(out, listener);
  108.                         }
  109.  
  110.                         FileCopyUtils.copy(in, out, null);
  111.  
  112.                         if (calucateUncompressedMd5) {
  113.                                 byte[] md5Bytes = md.digest();
  114.                                 String md5 = OutputFormatter.binaryToHex(md5Bytes);
  115.                                 return md5;
  116.                         }
  117.  
  118.                         return null;
  119.                 } catch (NoSuchAlgorithmException e) {
  120.                         throw new TemplateServiceException(
  121.                                         TemplateServiceException.UNKNOWN_ERROR);
  122.                 } catch (CompressorException e) {
  123.                         throw new TemplateServiceException(
  124.                                         TemplateServiceException.UNKNOWN_ERROR);
  125.                 }
  126.         }
  127.  
  128.         public final static String toCompressorAlgorithm(String compressionAlgorithm) {
  129.                 if ("bz2".equalsIgnoreCase(compressionAlgorithm)
  130.                                 || "bzip2".equalsIgnoreCase(compressionAlgorithm)) {
  131.                         return CompressorStreamFactory.BZIP2;
  132.                 } else if ("gz".equalsIgnoreCase(compressionAlgorithm)
  133.                                 || "gzip".equalsIgnoreCase(compressionAlgorithm)) {
  134.                         return CompressorStreamFactory.GZIP;
  135.                 } else if ("xz".equalsIgnoreCase(compressionAlgorithm)) {
  136.                         return CompressorStreamFactory.XZ;
  137.                 } else if ("pack200".equalsIgnoreCase(compressionAlgorithm)) {
  138.                         return CompressorStreamFactory.PACK200;
  139.                 } else {
  140.                         return compressionAlgorithm;
  141.                 }
  142.         }

回复 "java 压缩文件 解压缩文件"

这儿你可以回复上面这条便签

captcha