Commit 188aa77b0311d3a9048cc2d5025939824d479094

Authored by 李强
1 parent 1b1d237b

李强

src/main/java/com/bsth/util/PackTarGZUtils.java 0 → 100644
  1 +package com.bsth.util;
  2 +
  3 +import java.io.File;
  4 +import java.io.FileInputStream;
  5 +import java.io.FileNotFoundException;
  6 +import java.io.FileOutputStream;
  7 +import java.io.IOException;
  8 +import java.util.zip.GZIPOutputStream;
  9 +
  10 +import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  11 +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
  12 +import org.apache.commons.compress.utils.IOUtils;
  13 +
  14 +public class PackTarGZUtils {
  15 +
  16 +
  17 + /**
  18 + * @Title: pack
  19 + *
  20 + * @Description: 将一组文件打成tar包
  21 + *
  22 + * @param sources 要打包的原文件数组
  23 + *
  24 + * @param target 打包后的文件
  25 + *
  26 + * @return File 返回打包后的文件
  27 + *
  28 + * @throws
  29 + *
  30 + */
  31 + public static File pack(File[] sources, File target){
  32 +
  33 + FileOutputStream out = null;
  34 +
  35 + try {
  36 +
  37 + out = new FileOutputStream(target);
  38 +
  39 + } catch (FileNotFoundException e1) {
  40 +
  41 + e1.printStackTrace();
  42 +
  43 + }
  44 +
  45 + TarArchiveOutputStream os = new TarArchiveOutputStream(out);
  46 +
  47 + for (File file : sources) {
  48 +
  49 + try {
  50 +
  51 + os.putArchiveEntry(new TarArchiveEntry(file));
  52 +
  53 + IOUtils.copy(new FileInputStream(file), os);
  54 +
  55 + os.closeArchiveEntry();
  56 +
  57 + } catch (FileNotFoundException e) {
  58 +
  59 + e.printStackTrace();
  60 +
  61 + } catch (IOException e) {
  62 +
  63 + e.printStackTrace();
  64 +
  65 + }
  66 + }
  67 + if(os != null) {
  68 +
  69 + try {
  70 +
  71 + os.flush();
  72 +
  73 + os.close();
  74 +
  75 + } catch (IOException e) {
  76 +
  77 + e.printStackTrace();
  78 + }
  79 + }
  80 +
  81 + return target;
  82 +
  83 + }
  84 +
  85 + /**
  86 + *
  87 + * @Title: compress
  88 + *
  89 + * @Description: 将文件用gzip压缩
  90 + *
  91 + * @param source 需要压缩的文件
  92 + *
  93 + * @return File 返回压缩后的文件
  94 + *
  95 + * @throws
  96 + *
  97 + */
  98 + public static File compress(File source) {
  99 +
  100 + File target = new File(source.getName() + ".gz");
  101 +
  102 + FileInputStream in = null;
  103 +
  104 + GZIPOutputStream out = null;
  105 +
  106 + try {
  107 +
  108 + in = new FileInputStream(source);
  109 +
  110 + out = new GZIPOutputStream(new FileOutputStream(target));
  111 +
  112 + byte[] array = new byte[1024];
  113 +
  114 + int number = -1;
  115 +
  116 + while((number = in.read(array, 0, array.length)) != -1) {
  117 +
  118 + out.write(array, 0, number);
  119 + }
  120 + } catch (FileNotFoundException e) {
  121 +
  122 + e.printStackTrace();
  123 +
  124 + return null;
  125 +
  126 + } catch (IOException e) {
  127 +
  128 + e.printStackTrace();
  129 +
  130 + return null;
  131 +
  132 + } finally {
  133 +
  134 + if(in != null) {
  135 +
  136 + try {
  137 +
  138 + in.close();
  139 +
  140 + } catch (IOException e) {
  141 +
  142 + e.printStackTrace();
  143 +
  144 + return null;
  145 +
  146 + }
  147 +
  148 + }
  149 +
  150 + if(out != null) {
  151 +
  152 + try {
  153 +
  154 + out.close();
  155 +
  156 + } catch (IOException e) {
  157 +
  158 + e.printStackTrace();
  159 + return null;
  160 + }
  161 + }
  162 + }
  163 +
  164 + return target;
  165 +
  166 + }
  167 +}
... ...