| 1 | |
package com.aurea.maven.plugins.sonic.utils; |
| 2 | |
|
| 3 | |
import java.io.BufferedInputStream; |
| 4 | |
import java.io.File; |
| 5 | |
import java.io.FileInputStream; |
| 6 | |
import java.io.FileOutputStream; |
| 7 | |
import java.util.zip.ZipEntry; |
| 8 | |
import java.util.zip.ZipInputStream; |
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | 0 | public class ZipUtilities { |
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
public static void doUnzipAction(final String _filename, final String _unzipDir) { |
| 23 | |
|
| 24 | 0 | final int bufferSize = 1024; |
| 25 | |
|
| 26 | 0 | FileOutputStream dest = null; |
| 27 | |
|
| 28 | |
ZipInputStream zis; |
| 29 | |
|
| 30 | |
try { |
| 31 | 0 | zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(new File(_filename)))); |
| 32 | |
|
| 33 | |
ZipEntry entry; |
| 34 | |
|
| 35 | 0 | new File(_unzipDir).mkdirs(); |
| 36 | 0 | while ((entry = zis.getNextEntry()) != null) { |
| 37 | |
int count; |
| 38 | 0 | byte[] data = new byte[bufferSize]; |
| 39 | 0 | String targetFile = _unzipDir + "/" + entry.getName(); |
| 40 | |
|
| 41 | 0 | createSubDirectory(_unzipDir, entry); |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | if (!entry.isDirectory()) { |
| 46 | 0 | dest = new FileOutputStream(targetFile); |
| 47 | |
|
| 48 | 0 | while ((count = zis.read(data, 0, bufferSize)) != -1) { |
| 49 | 0 | dest.write(data, 0, count); |
| 50 | |
} |
| 51 | |
|
| 52 | 0 | dest.flush(); |
| 53 | 0 | dest.close(); |
| 54 | |
} |
| 55 | 0 | dest = null; |
| 56 | |
|
| 57 | 0 | } |
| 58 | |
|
| 59 | 0 | zis.close(); |
| 60 | |
|
| 61 | 0 | } catch (Exception e) { |
| 62 | |
|
| 63 | 0 | e.printStackTrace(); |
| 64 | 0 | throw new RuntimeException(e); |
| 65 | 0 | } |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
private static void createSubDirectory(final String _unzipDir, final ZipEntry _entry) { |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | 0 | String name = _entry.getName(); |
| 75 | |
String fileDir; |
| 76 | 0 | if (name.indexOf("\\") > 0) { |
| 77 | |
|
| 78 | 0 | fileDir = name.substring(0, name.lastIndexOf("\\")); |
| 79 | |
|
| 80 | 0 | } else if (name.indexOf("/") > 0) { |
| 81 | |
|
| 82 | 0 | fileDir = name.substring(0, name.lastIndexOf("/")); |
| 83 | |
|
| 84 | |
} else { |
| 85 | |
|
| 86 | 0 | return; |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | 0 | File newdir = new File(_unzipDir + File.separator + fileDir); |
| 91 | 0 | if (!newdir.exists()) { |
| 92 | 0 | newdir.mkdirs(); |
| 93 | |
} |
| 94 | 0 | } |
| 95 | |
} |