Guest User

Untitled

a guest
Nov 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.util.ArrayDeque;
  6. import java.util.ArrayList;
  7. import java.util.Deque;
  8. import java.util.HashMap;
  9. import java.util.LinkedHashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.SortedMap;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipInputStream;
  15.  
  16. import org.apache.commons.lang3.StringUtils;
  17.  
  18. public class ZipHandler1 {
  19.  
  20. private static final int BUFFER_SIZE = 2048;
  21.  
  22. private static final String ZIP_EXTENSION = ".zip";
  23. public static final Integer FOLDER = 1;
  24. public static final Integer ZIP = 2;
  25. public static final Integer FILE = 3;
  26.  
  27.  
  28. public static Deque<Map<Integer, Object[]>> unzip(ByteArrayOutputStream zippedOutputFile) {
  29.  
  30. try {
  31.  
  32. ZipInputStream inputStream = new ZipInputStream(
  33. new BufferedInputStream(new ByteArrayInputStream(
  34. zippedOutputFile.toByteArray())));
  35.  
  36. ZipEntry entry;
  37.  
  38. Deque<Map<Integer, Object[]>> result = new ArrayDeque<Map<Integer, Object[]>>();
  39.  
  40. while ((entry = inputStream.getNextEntry()) != null) {
  41.  
  42. LinkedHashMap<Integer, Object[]> map = new LinkedHashMap<Integer, Object[]>();
  43. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  44. System.out.println("tExtracting entry: " + entry);
  45. int count;
  46. byte[] data = new byte[BUFFER_SIZE];
  47. if (!entry.isDirectory()) {
  48. BufferedOutputStream out = new BufferedOutputStream(
  49. outputStream, BUFFER_SIZE);
  50.  
  51. while ((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {
  52. out.write(data, 0, count);
  53. }
  54.  
  55. out.flush();
  56. out.close();
  57.  
  58. // recursively unzip files
  59. if (entry.getName().toUpperCase().endsWith(ZIP_EXTENSION.toUpperCase())) {
  60. map.put(ZIP, new Object[] {entry.getName(), unzip(outputStream)});
  61. result.add(map);
  62.  
  63. } else {
  64. map.put(FILE, new Object[] {entry.getName(), outputStream});
  65. result.add(map);
  66. }
  67. } else {
  68. map.put(FOLDER, new Object[] {entry.getName(),
  69. unzip(outputStream)});
  70. result.add(map);
  71. }
  72. }
  73.  
  74. inputStream.close();
  75.  
  76. return result;
  77.  
  78. } catch (Exception e) {
  79. throw new RuntimeException(e);
  80. }
  81. }
  82. }
  83.  
  84. package course.hernan;
  85.  
  86. import java.io.BufferedInputStream;
  87. import java.io.BufferedOutputStream;
  88. import java.io.ByteArrayInputStream;
  89. import java.io.ByteArrayOutputStream;
  90. import java.io.File;
  91. import java.io.FileInputStream;
  92. import java.io.FileNotFoundException;
  93. import java.io.FileOutputStream;
  94. import java.io.IOException;
  95. import java.util.ArrayDeque;
  96. import java.util.Deque;
  97. import java.util.LinkedHashMap;
  98. import java.util.List;
  99. import java.util.Map;
  100. import java.util.zip.ZipEntry;
  101. import java.util.zip.ZipInputStream;
  102. import java.util.zip.ZipOutputStream;
  103.  
  104. import org.apache.commons.io.IOUtils;
  105.  
  106. public class FileReader {
  107.  
  108. private static final int BUFFER_SIZE = 2048;
  109.  
  110. public static void main(String[] args) {
  111.  
  112. try {
  113. File f = new File("DIR/inputs.zip");
  114. FileInputStream fis = new FileInputStream(f);
  115. BufferedInputStream bis = new BufferedInputStream(fis);
  116. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  117. BufferedOutputStream bos = new BufferedOutputStream(baos);
  118. byte[] buffer = new byte[BUFFER_SIZE];
  119.  
  120. while (bis.read(buffer, 0, BUFFER_SIZE) != -1) {
  121. bos.write(buffer);
  122. }
  123.  
  124. bos.flush();
  125. bos.close();
  126. bis.close();
  127.  
  128. Deque<Map<Integer, Object[]>> outputDataStack = ZipHandler1.unzip(baos);
  129.  
  130. //Output file
  131. File fout = new File("DIR/inputs2.zip");
  132.  
  133. ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(fout)));
  134.  
  135. processZip(outputDataStack, zipOutput);
  136. zipOutput.close();
  137.  
  138. } catch (FileNotFoundException e) {
  139. // TODO Auto-generated catch block
  140. e.printStackTrace();
  141. } catch (IOException e) {
  142. // TODO Auto-generated catch block
  143. e.printStackTrace();
  144. }
  145. }
  146.  
  147. private static final void processZip(Deque<Map<Integer, Object[]>> outputDataStack,
  148. ZipOutputStream zipOutput) throws IOException {
  149.  
  150. while (!outputDataStack.isEmpty()) {
  151. Map<Integer, Object[]> map = outputDataStack.pop();
  152.  
  153. for (Map.Entry<Integer, Object[]> entry : map.entrySet()) {
  154. System.out.println("KEY:" + entry.getKey());
  155. Object[] values = entry.getValue();
  156. String entryName = (String)values[0];
  157.  
  158. if (entry.getKey().equals(ZipHandler1.FILE)) {
  159. System.out.println("..........................");
  160. System.out.println("type: FILE");
  161. System.out.println("Name: " + entryName);
  162.  
  163. zipOutput.putNextEntry(new ZipEntry(entryName));
  164. byte[] outputByteArray = ((ByteArrayOutputStream)values[1]).toByteArray();
  165.  
  166. IOUtils.write(outputByteArray, zipOutput);
  167. zipOutput.closeEntry();
  168. ((ByteArrayOutputStream)values[1]).close();
  169.  
  170. } else if (entry.getKey().equals(ZipHandler1.FOLDER)) {
  171. System.out.println("..........................");
  172. System.out.println("type: FOLDER");
  173. System.out.println("Name: " + entryName);
  174.  
  175. zipOutput.putNextEntry(new ZipEntry(entryName));
  176. System.out.println("..........................");
  177. zipOutput.closeEntry();
  178.  
  179. } else if (entry.getKey().equals(ZipHandler1.ZIP)) {
  180. System.out.println("..........................");
  181. System.out.println("type: ZIP");
  182. System.out.println("Name: " + entryName);
  183.  
  184. zipOutput.putNextEntry(new ZipEntry(entryName));
  185.  
  186. ByteArrayOutputStream innerZipByteArray = new ByteArrayOutputStream(BUFFER_SIZE);
  187. ZipOutputStream innerZipOutput = new ZipOutputStream(
  188. new BufferedOutputStream(innerZipByteArray));
  189.  
  190. processZip((Deque<Map<Integer,Object[]>>)values[1], innerZipOutput);
  191. innerZipOutput.flush();
  192. IOUtils.write(zzzz.toByteArray(), zipOutput);
  193. innerZipOutput.close();
  194. zipOutput.closeEntry();
  195. System.out.println("..........................");
  196. }
  197. System.out.println("..........................");
  198. zipOutput.closeEntry();
  199. }
  200. }
  201. }
Add Comment
Please, Sign In to add comment