ZipUtils.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.fourdage.dingding.util;
  2. import java.io.*;
  3. import java.util.Properties;
  4. import java.util.zip.GZIPInputStream;
  5. import org.apache.tools.tar.TarEntry;
  6. import org.apache.tools.tar.TarInputStream;
  7. public class ZipUtils {
  8. /**
  9. * 构建目录
  10. * @param outputDir
  11. * @param subDir
  12. */
  13. public static void createDirectory(String outputDir,String subDir){
  14. File file = new File(outputDir);
  15. if(!(subDir == null || subDir.trim().equals(""))){//子目录不为空
  16. file = new File(outputDir + "/" + subDir);
  17. }
  18. if(!file.exists()){
  19. if(!file.getParentFile().exists())
  20. file.getParentFile().mkdirs();
  21. file.mkdirs();
  22. }
  23. }
  24. //------------------------------------------------------------------------------------------------------
  25. /**
  26. * 解压tar.gz 文件
  27. * @param file 要解压的tar.gz文件对象
  28. * @param outputDir 要解压到某个指定的目录下
  29. * @throws IOException
  30. */
  31. public static void unTarGz(File file, String outputDir) throws IOException{
  32. TarInputStream tarIn = null;
  33. try{
  34. tarIn = new TarInputStream(new GZIPInputStream(
  35. new BufferedInputStream(new FileInputStream(file))),
  36. 1024 * 2);
  37. createDirectory(outputDir,null);//创建输出目录
  38. TarEntry entry = null;
  39. while( (entry = tarIn.getNextEntry()) != null ){
  40. if(entry.isDirectory()){//是目录
  41. entry.getName();
  42. createDirectory(outputDir,entry.getName());//创建空目录
  43. }else{//是文件
  44. File tmpFile = new File(outputDir + "/" + entry.getName());
  45. createDirectory(tmpFile.getParent() + "/",null);//创建输出目录
  46. OutputStream out = null;
  47. try{
  48. out = new FileOutputStream(tmpFile);
  49. int length = 0;
  50. byte[] b = new byte[2048];
  51. while((length = tarIn.read(b)) != -1){
  52. out.write(b, 0, length);
  53. }
  54. }catch(IOException ex){
  55. throw ex;
  56. }finally{
  57. if(out!=null)
  58. out.close();
  59. }
  60. }
  61. }
  62. }catch(IOException ex){
  63. throw new IOException("解压归档文件出现异常",ex);
  64. } finally{
  65. try{
  66. if(tarIn != null){
  67. tarIn.close();
  68. }
  69. }catch(IOException ex){
  70. throw new IOException("关闭tarFile出现异常",ex);
  71. }
  72. }
  73. }
  74. public static void main(String[] args) throws IOException, InterruptedException {
  75. String path = "/data/557662992913661952/";
  76. String fileName = "copy.tar.gz";
  77. File f = new File(path);
  78. Properties prop = System.getProperties();
  79. String os = prop.getProperty("os.name");
  80. if (os != null && os.toLowerCase().indexOf("linux") > -1) {
  81. // linux 执行合并文件命令
  82. StringBuffer sb = new StringBuffer(" cat ");
  83. for (int i = 0; i < f.listFiles().length; i++){
  84. sb.append(f.listFiles()[i].getAbsolutePath());
  85. }
  86. sb.append(" >> ").append(path).append(fileName);
  87. String[] cmd = new String[]{"/bin/sh", "-c", sb.toString()};
  88. Process process = Runtime.getRuntime().exec(cmd);
  89. int exitValue = process.waitFor();
  90. process.getOutputStream().close();
  91. if (0 == exitValue){
  92. System.out.println(path+fileName+":合并完成");
  93. unTarGz(new File(path + fileName), path);
  94. System.out.println(path+fileName+":解压完成");
  95. }
  96. } else {
  97. // windows 执行合并文件命令
  98. StringBuffer sb = new StringBuffer("cmd.exe /c copy /b ");
  99. for (int i = 0; i < f.listFiles().length; i++){
  100. sb.append(f.listFiles()[i].getAbsolutePath());
  101. if (i != f.listFiles().length -1){
  102. sb.append("+");
  103. }
  104. }
  105. sb.append(" ").append(path).append(fileName);
  106. System.out.println(sb.toString());
  107. Process process = Runtime.getRuntime().exec(sb.toString());
  108. int exitValue = process.waitFor();
  109. process.getOutputStream().close();
  110. if (0 == exitValue){
  111. System.out.println(path+fileName+":合并完成");
  112. unTarGz(new File(path + fileName), path);
  113. System.out.println(path+fileName+":解压完成");
  114. }
  115. }
  116. }
  117. }