Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package com.alipay.datamatrix.common.util;
  2.  
  3. import java.io.*;
  4. import java.util.zip.ZipEntry;
  5. import java.util.zip.ZipInputStream;
  6.  
  7. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  8. import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
  9. import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
  10.  
  11. /**
  12. * FileExtractUtils
  13. */
  14. public class FileExtractUtils {
  15. /**
  16. * The constant BUFFER_SIZE.
  17. */
  18. private static final int BUFFER_SIZE = 4096;
  19.  
  20. /**
  21. * Extract zip.
  22. *
  23. * @param zipfile
  24. * the zipfile
  25. * @param outdir
  26. * the outdir
  27. */
  28. public static void extractZip(File zipfile, File outdir) {
  29. try {
  30. ZipInputStream is = new ZipInputStream(
  31. new BufferedInputStream(new FileInputStream(zipfile)));
  32. ZipEntry entry;
  33. while ((entry = is.getNextEntry()) != null) {
  34. String name = entry.getName();
  35. if (entry.isDirectory()) {
  36. mkDirs(outdir, name);
  37. } else {
  38. String dir = directoryPart(name);
  39. if (dir != null) {
  40. mkDirs(outdir, dir);
  41. }
  42. extractFile(is, outdir, name);
  43. }
  44. }
  45. is.close();
  46. } catch (IOException e) {
  47. throw new RuntimeException(e);
  48. }
  49. }
  50.  
  51. /**
  52. * Extract g zip.
  53. *
  54. * @param tgzFile
  55. * the tgz file
  56. * @param outDir
  57. * the out dir
  58. */
  59. public static void extractGZip(File tgzFile, File outDir) {
  60. try {
  61. TarArchiveInputStream tarIs = new TarArchiveInputStream(new GzipCompressorInputStream(
  62. new BufferedInputStream(new FileInputStream(tgzFile))));
  63. TarArchiveEntry entry;
  64. while ((entry = (TarArchiveEntry) tarIs.getNextEntry()) != null) {
  65. String name = entry.getName();
  66. if (entry.isDirectory()) {
  67. mkDirs(outDir, name);
  68. } else {
  69. String dir = directoryPart(name);
  70. if (dir != null) {
  71. mkDirs(outDir, dir);
  72. }
  73. extractFile(tarIs, outDir, name);
  74. }
  75. }
  76. tarIs.close();
  77. } catch (IOException e) {
  78. throw new RuntimeException(e);
  79. }
  80. }
  81.  
  82. /**
  83. * Extract file.
  84. *
  85. * @param inputStream
  86. * the input stream
  87. * @param outDir
  88. * the out dir
  89. * @param name
  90. * the name
  91. * @throws IOException
  92. * the io exception
  93. */
  94. private static void extractFile(InputStream inputStream, File outDir,
  95. String name) throws IOException {
  96. int count = -1;
  97. byte buffer[] = new byte[BUFFER_SIZE];
  98. BufferedOutputStream out = new BufferedOutputStream(
  99. new FileOutputStream(new File(outDir, name)), BUFFER_SIZE);
  100. while ((count = inputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
  101. out.write(buffer, 0, count);
  102. }
  103. out.close();
  104. }
  105.  
  106. /**
  107. * Mk dirs.
  108. *
  109. * @param outdir
  110. * the outdir
  111. * @param path
  112. * the path
  113. */
  114. private static void mkDirs(File outdir, String path) {
  115. File d = new File(outdir, path);
  116. if (!d.exists()) {
  117. d.mkdirs();
  118. }
  119. }
  120.  
  121. /**
  122. * Directory part string.
  123. *
  124. * @param name
  125. * the name
  126. * @return the string
  127. */
  128. private static String directoryPart(String name) {
  129. int s = name.lastIndexOf(File.separatorChar);
  130. return s == -1 ? null : name.substring(0, s);
  131. }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement