工具类 GzipUtil .java


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GzipUtil {
 private static final Logger logger = LoggerFactory.getLogger(GzipUtil.class);

 /**
  * 解压tar.gz文件
  *
  * @param targzFile
  *            需要解压的文件路径
  * @param outputDir
  *            解压后的文件存放的文件夹
  *
  * @return 就绪文件列表
  */
 public static List<String> unzipGzipFile(String targzFile, String outputDir) {

  List<String> dataOkFiles = new ArrayList<String>();
  FileInputStream fileInputStream = null;
  ArchiveInputStream archiveInputStream = null;
  BufferedInputStream bufferedInputStream = null;
  BufferedOutputStream bufferedOutputStream = null;

  try {
   fileInputStream = new FileInputStream(targzFile);
   GZIPInputStream gzipInputStream = new GZIPInputStream(new BufferedInputStream(fileInputStream));
   archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInputStream);
   bufferedInputStream = new BufferedInputStream(archiveInputStream);
   TarArchiveEntry tarArchiveEntry = (TarArchiveEntry) archiveInputStream.getNextEntry();

   while (tarArchiveEntry != null) {
    String name = tarArchiveEntry.getName();
    String[] names = name.split("/");
    String fileName = outputDir;


    for (String str : names) {
     fileName = fileName + File.separator + str;
    }

    File file = mkFile(fileName);
    logger.info("解压出的文件为:{}", file.toString());
   
    if (fileName.endsWith("ok")) {
     dataOkFiles.add(fileName);
    }

    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));

    int b;
    while ((b = bufferedInputStream.read()) != -1) {
     bufferedOutputStream.write(b);
    }

    bufferedOutputStream.flush();
    bufferedOutputStream.close();

    tarArchiveEntry = (TarArchiveEntry) archiveInputStream.getNextEntry();
   }
  } catch (Exception e) {
   logger.error("error", e);
   return null;
  } finally {
   try {
    if (bufferedInputStream != null) {
     bufferedInputStream.close();
    }
   } catch (IOException e) {
    logger.error("error", e);
   }
  }

  return dataOkFiles;
 }

 private static File mkFile(String fileName) {
  File f = new File(fileName);
  try {
   if (!f.getParentFile().exists()) {
    f.getParentFile().mkdirs();
   }
   f.createNewFile();
  } catch (IOException e) {
   logger.error("error", e);
  }
  return f;
 }

 /**
  * 校验数据文件 .dat
  *
  * @param dataFileDir
  *            数据文件所在的文件夹路径
  * @param dataFileOk
  *            就绪文件名
  * @return 数据文件列表
  */
 public static List<String> checkDataFile(List<String> dataFileOks) {
  BufferedReader bufferedReader = null;
  List<String> dataFileList = new ArrayList<String>();

  try {
   for (String dataFileOk : dataFileOks) {

    File dataOkfile = new File(dataFileOk);
    FileInputStream fileInputStream = new FileInputStream(dataOkfile);

    bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
    String datFileName = bufferedReader.readLine();
    String datFileSize = bufferedReader.readLine();
    String datFileLine = bufferedReader.readLine();
    String datFileTag = bufferedReader.readLine();

    logger.info("数据就绪文件内容:数据文件名:{}\n 数据文件大小:{}\n 数据文件行数:{}\n 数据文件类型标识:{}", datFileName, datFileSize,
      datFileLine, datFileTag);

    if (bufferedReader != null) {
     bufferedReader.close();
    }

    String datFilePath = dataOkfile.getParent() + File.separator + datFileName;

    File datFile = new File(datFilePath);
    if (!datFile.exists()) {
     throw new FileNotFoundException(datFilePath + "文件不存在!");
    }

    if (!Long.toString(datFile.length()).equals(datFileSize.trim())) {
     throw new Exception(datFilePath + "文件大小错误!");
    }

    if (!datFileTag.equals("I") || !datFileName.endsWith("INC.dat")) {
     throw new Exception(datFilePath + "不是增量文件或文件名错误!");
    }

    dataFileList.add(datFilePath);

   }
  } catch (Exception e) {
   logger.error("error", e);
   return null;
  } finally {
   if (bufferedReader != null) {
    try {
     bufferedReader.close();

    } catch (IOException e) {
     logger.error("error", e);

    }
   }
  }

  return dataFileList;
 }

 /**
  * 校验压缩文件 .tar.gz
  *
  * @param zipFileOkPath
  *            包就绪文件的路径
  * @param zipFileOk
  *            包就绪文件名
  * @return true-校验通过,false-校验失败
  */
 public static boolean checkGzipFile(String zipFileOkPath, String zipFileOk) {
  BufferedReader bufferedReader = null;
  boolean isZipFileComplete = false;

  try {

   FileInputStream fileInputStream = new FileInputStream(zipFileOkPath + zipFileOk);
   bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
   String gzipFileName = bufferedReader.readLine();
   String gzipFileSize = bufferedReader.readLine();

   logger.info("包就绪文件内容:包文件名:{}\n包文件大小:{}", gzipFileName, gzipFileSize);

   String gzipFilePath = zipFileOkPath + gzipFileName;
   File gzipFile = new File(gzipFilePath);
   if (!gzipFile.exists()) {
    throw new FileNotFoundException(gzipFilePath + "文件不存在!");
   }

   if (!Long.toString(gzipFile.length()).equals(gzipFileSize.trim())) {
    throw new Exception(gzipFilePath + "文件大小错误!");
   }

   isZipFileComplete = true;
  } catch (Exception e) {
   logger.error("error", e);

  } finally {
   if (bufferedReader != null) {
    try {
     bufferedReader.close();

    } catch (IOException e) {
     logger.error("error", e);

    }
   }
  }
  return isZipFileComplete;
 }

 /**
  *
  * @Title: pack
  * @Description: 将一组文件打成tar包
  * @param sources
  *            要打包的原文件数组
  * @param target
  *            打包后的文件
  * @return File 返回打包后的文件
  */
 public static File pack(File[] sources, File target) {

  FileOutputStream out = null;

  try {
   out = new FileOutputStream(target);
  } catch (FileNotFoundException e1) {
   logger.error("将一组文件打成tar包的时候生成文件输出流时发生异常{}", e1);
  }

  TarArchiveOutputStream os = new TarArchiveOutputStream(out);
  FileInputStream in = null;
  for (File file : sources) {
   try {
    in = new FileInputStream(file);
    TarArchiveEntry entry = new TarArchiveEntry(file.getName());
    entry.setSize(file.length());
    os.putArchiveEntry(entry);
    IOUtils.copy(in, os);
   } catch (FileNotFoundException e) {
    logger.error("将一组文件打成tar包时发生FileNotFoundException异常{}", e);
   } catch (IOException e) {
    logger.error("将一组文件打成tar包时发生IOException异常{}", e);
   } finally {
    if (os != null) {
     try {
      os.closeArchiveEntry();
     } catch (IOException e) {
      logger.error("closeArchiveEntry时发生IOException异常{}", e);
     }
    }
    if (in != null) {
     try {
      in.close();
     } catch (IOException e) {
      logger.error("关闭FileInputStream时发生IOException异常{}", e);
     }
    }
   }
  }

  if (os != null) {
   try {
    os.flush();
    os.close();
   } catch (IOException e) {
    logger.error("关闭FileOutputStream时发生IOException异常{}", e);
   }
  }

  return target;
 }

 /**
  *
  * @Title: compress
  * @Description: 将文件用gzip压缩
  * @param source
  *            需要压缩的文件
  * @return File 返回压缩后的文件
  */
 public static File compress(File source) {

  File target = new File(source.getAbsolutePath() + ".gz");

  if (target.exists()) {

   target.delete();

  }

  FileInputStream in = null;
  GZIPOutputStream out = null;

  try {

   target.createNewFile();
   in = new FileInputStream(source);
   out = new GZIPOutputStream(new FileOutputStream(target));
   byte[] array = new byte[1024];
   int number = -1;

   while ((number = in.read(array, 0, array.length)) != -1) {

    out.write(array, 0, number);

   }

  } catch (FileNotFoundException e) {

   logger.error("将一组文件打成gz包时发生FileNotFoundException异常{}", e);
   return null;

  } catch (IOException e) {

   logger.error("将一组文件打成gz包时发生IOException异常{}", e);
   return null;

  } finally {

   if (in != null) {

    try {

     in.close();

    } catch (IOException e) {

     logger.error("将一组文件打成gz包关闭输入流时发生IOException异常{}", e);
     return null;
    }
   }

   if (out != null) {

    try {

     out.flush();
     out.close();

    } catch (IOException e) {

     logger.error("将一组文件打成gz包关闭输出流时发生IOException异常{}", e);
     return null;

    }
   }
  }

  return target;

 }


 private static void writeGzipOk() {
  BufferedWriter bw = null;
  try {
   File file = new File("E:\\sas\\zip\\FK_20170905.tar.gz");
   FileOutputStream fos = new FileOutputStream("E:\\sas\\zip\\FK_20170905.ok");
   OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
   bw = new BufferedWriter(osw);
   bw.write("FK_20170905.tar.gz\n");
   bw.write(Long.toString(file.length()));

  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (bw != null) {

    try {
     bw.flush();
     bw.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

 private static void writeDatOk() {
  BufferedWriter bw = null;
  try {
   File file = new File("E:\\sas\\zip\\FK_ACCT_20170905_01_INC.dat");
   FileOutputStream fos = new FileOutputStream("E:\\sas\\zip\\FK_ACCT_20170905_01_INC.ok");
   OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
   bw = new BufferedWriter(osw);
   bw.write("FK_ACCT_20170905_01_INC.dat\n");
   bw.write(Long.toString(file.length()) + "\n");
   bw.write(50000 + "\n");
   bw.write("I");

  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (bw != null) {

    try {
     bw.flush();
     bw.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

 private static void writeDat() {
  BufferedWriter bw = null;
  try {

   FileOutputStream fos = new FileOutputStream("E:\\sas\\zip\\FK_ACCT_20170905_01_INC.dat");
   OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
   bw = new BufferedWriter(osw);
   String str = "2031203120312031|^|2";
   for (int i = 0; i < 50000; i++) {
    if (i != 0) {
     bw.newLine();
    }
    bw.write(str);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (bw != null) {

    try {
     bw.flush();
     bw.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

 /**
  * 归档
  *
  * @param entry
  * @throws IOException
  * @author yutao
  * @return
  * @date 2017年5月27日下午1:48:23
  */
 private static String archive(String entry) throws IOException {
  File file = new File(entry);

  TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(file.getAbsolutePath() + ".tar"));
  String base = file.getName();
  if (file.isDirectory()) {
   archiveDir(file, tos, base);
  } else {
   archiveHandle(tos, file, base);
  }

  tos.close();
  return file.getAbsolutePath() + ".tar";
 }

 /**
  * 递归处理,准备好路径
  *
  * @param file
  * @param tos
  * @param base
  * @throws IOException
  * @author yutao
  * @date 2017年5月27日下午1:48:40
  */
 private static void archiveDir(File file, TarArchiveOutputStream tos, String basePath) throws IOException {
  File[] listFiles = file.listFiles();
  for (File fi : listFiles) {
   if (fi.isDirectory()) {
    archiveDir(fi, tos, basePath + File.separator + fi.getName());
   } else {
    archiveHandle(tos, fi, basePath);
   }
  }
 }

 /**
  * 具体归档处理(文件)
  *
  * @param tos
  * @param fi
  * @param base
  * @throws IOException
  * @author yutao
  * @date 2017年5月27日下午1:48:56
  */
 private static void archiveHandle(TarArchiveOutputStream tos, File fi, String basePath) throws IOException {
  TarArchiveEntry tEntry = new TarArchiveEntry(basePath + File.separator + fi.getName());
  tEntry.setSize(fi.length());

  tos.putArchiveEntry(tEntry);

  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fi));

  byte[] buffer = new byte[1024];
  int read = -1;
  while ((read = bis.read(buffer)) != -1) {
   tos.write(buffer, 0, read);
  }
  bis.close();
  tos.closeArchiveEntry();// 这里必须写,否则会失败
 }

 public static String compressArchive(String path) throws IOException {
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));

  GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(
    new BufferedOutputStream(new FileOutputStream(path + ".gz")));

  byte[] buffer = new byte[1024];
  int read = -1;
  while ((read = bis.read(buffer)) != -1) {
   gcos.write(buffer, 0, read);
  }
  gcos.close();
  bis.close();
  return path + ".gz";
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值