|
2 | 2 |
|
3 | 3 | import java.io.*; |
4 | 4 | import java.io.FileOutputStream; |
| 5 | +import java.nio.ByteBuffer; |
| 6 | +import java.util.Enumeration; |
5 | 7 | import java.util.zip.*; |
| 8 | +import java.util.zip.ZipFile; |
6 | 9 |
|
7 | 10 | public class ZipHandler { |
8 | 11 | public static void ZipInputStreamSafe(InputStream inputStream) throws IOException { |
@@ -127,4 +130,49 @@ public static void InflaterInputStreamUnsafe(InputStream inputStream) throws jav |
127 | 130 | dest.close(); |
128 | 131 | } |
129 | 132 | } |
| 133 | + |
| 134 | + public static void InflaterUnsafe(byte[] inputBytes) throws DataFormatException, IOException { |
| 135 | + Inflater inflater = new Inflater(); |
| 136 | + inflater.setInput(inputBytes); |
| 137 | + try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(inputBytes.length)) { |
| 138 | + byte[] buffer = new byte[1024]; |
| 139 | + while (!inflater.finished()) { |
| 140 | + final int count = inflater.inflate(buffer); |
| 141 | + outputStream.write(buffer, 0, count); |
| 142 | + } |
| 143 | + outputStream.toByteArray(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public static void ZipFile1(String zipFilePath) throws DataFormatException, IOException { |
| 148 | + try { |
| 149 | + System.out.println("zipFilePath = " + zipFilePath); |
| 150 | + ZipFile zipFile = new ZipFile(zipFilePath); |
| 151 | + Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
| 152 | + while (entries.hasMoreElements()) { |
| 153 | + ZipEntry entry = entries.nextElement(); |
| 154 | + if (entry.isDirectory()) { |
| 155 | + System.out.print("dir : " + entry.getName()); |
| 156 | + String destPath = "tmp" + File.separator + entry.getName(); |
| 157 | + System.out.println(" => " + destPath); |
| 158 | + File file = new File(destPath); |
| 159 | + file.mkdirs(); |
| 160 | + } else { |
| 161 | + String destPath = "tmp" + File.separator + entry.getName(); |
| 162 | + |
| 163 | + try (InputStream inputStream = zipFile.getInputStream(entry); |
| 164 | + FileOutputStream outputStream = new FileOutputStream(destPath); ) { |
| 165 | + int data = inputStream.read(); |
| 166 | + while (data != -1) { |
| 167 | + outputStream.write(data); |
| 168 | + data = inputStream.read(); |
| 169 | + } |
| 170 | + } |
| 171 | + System.out.println("file : " + entry.getName() + " => " + destPath); |
| 172 | + } |
| 173 | + } |
| 174 | + } catch (IOException e) { |
| 175 | + throw new RuntimeException("Error unzipping file " + zipFilePath, e); |
| 176 | + } |
| 177 | + } |
130 | 178 | } |
0 commit comments