1 package com.atlassian.plugin.util.zip;
2
3 import org.apache.commons.io.IOUtils;
4 import org.apache.commons.lang3.StringUtils;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.zip.ZipEntry;
10 import java.util.zip.ZipInputStream;
11
12
13
14
15 public class StreamUnzipper extends AbstractUnzipper {
16 private ZipInputStream zis;
17
18
19
20
21
22
23
24 public StreamUnzipper(InputStream zipStream, File destDir) {
25 if (zipStream == null)
26 throw new IllegalArgumentException("zip stream cannot be null");
27 this.zis = new ZipInputStream(zipStream);
28 this.destDir = destDir;
29 }
30
31 public void unzip() throws IOException {
32 ZipEntry zipEntry = zis.getNextEntry();
33 try {
34 while (zipEntry != null) {
35 saveEntry(zis, zipEntry);
36 zis.closeEntry();
37 zipEntry = zis.getNextEntry();
38 }
39 } finally {
40 IOUtils.closeQuietly(zis);
41 }
42 }
43
44 public File unzipFileInArchive(String fileName) throws IOException {
45 File result = null;
46
47 try {
48 ZipEntry zipEntry = zis.getNextEntry();
49 while (zipEntry != null) {
50 String entryName = zipEntry.getName();
51
52
53 if (StringUtils.isNotEmpty(entryName) && entryName.startsWith("/"))
54 entryName = entryName.substring(1);
55
56 if (fileName.equals(entryName)) {
57 result = saveEntry(zis, zipEntry);
58 break;
59 }
60 zis.closeEntry();
61 zipEntry = zis.getNextEntry();
62 }
63 } finally {
64 IOUtils.closeQuietly(zis);
65 }
66
67 return result;
68 }
69
70 public ZipEntry[] entries() throws IOException {
71 return entries(zis);
72 }
73 }