1 package net.sourceforge.jpotpourri.util;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.zip.ZipEntry;
10 import java.util.zip.ZipException;
11 import java.util.zip.ZipFile;
12 import java.util.zip.ZipInputStream;
13 import java.util.zip.ZipOutputStream;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18
19
20
21
22 public final class ZipUtil {
23
24 private static final Log LOG = LogFactory.getLog(ZipUtil.class);
25
26 private static final int BUFFER_SIZE = 8192;
27
28
29 private ZipUtil() {
30
31 }
32
33 public static void unzip(final File file, final ZipFile zipFile,
34 final File targetDirectory) throws ZipUtilException {
35 LOG.info("Unzipping zip file '" + file.getAbsolutePath() + "' to directory " +
36 "'" + targetDirectory.getAbsolutePath() + "'.");
37 assert(file.exists() && file.isFile());
38
39 if(targetDirectory.exists() == false) {
40 LOG.debug("Creating target directory.");
41 if(targetDirectory.mkdirs() == false) {
42 throw new ZipUtilException("Could not create target directory at " +
43 "'" + targetDirectory.getAbsolutePath() + "'!");
44 }
45 }
46
47 ZipInputStream zipin = null;
48 try {
49
50
51 zipin = new ZipInputStream(new FileInputStream(file));
52 ZipEntry nextZipEntry = zipin.getNextEntry();
53 while (nextZipEntry != null) {
54 LOG.debug("Unzipping entry '" + nextZipEntry.getName() + "'.");
55
56 if(nextZipEntry.isDirectory()) {
57 LOG.debug("Skipping directory.");
58 continue;
59 }
60
61 final File targetFile = new File(targetDirectory, nextZipEntry.getName());
62 final File parentTargetFile = targetFile.getParentFile();
63 if(parentTargetFile.exists() == false) {
64 LOG.debug("Creating directory '" + parentTargetFile.getAbsolutePath() + "'.");
65 if(parentTargetFile.mkdirs() == false) {
66 throw new ZipUtilException("Could not create target directory at " +
67 "'" + parentTargetFile.getAbsolutePath() + "'!");
68 }
69 }
70
71 InputStream input = null;
72 FileOutputStream output = null;
73 try {
74 input = zipFile.getInputStream(nextZipEntry);
75 if(targetFile.createNewFile() == false) {
76 throw new ZipUtilException("Could not create target file " +
77 "'" + targetFile.getAbsolutePath() + "'!");
78 }
79 output = new FileOutputStream(targetFile);
80
81 byte[] buffer = new byte[BUFFER_SIZE];
82 int readBytes = input.read(buffer, 0, buffer.length);
83 while (readBytes > 0) {
84 output.write(buffer, 0, readBytes);
85 readBytes = input.read(buffer, 0, buffer.length);
86 }
87 } finally {
88 CloseUtil.close(input, output);
89 }
90
91 nextZipEntry = zipin.getNextEntry();
92 }
93 } catch (IOException e) {
94 throw new ZipUtilException("Could not unzip file '" + file.getAbsolutePath() + "'!", e);
95 } finally {
96 CloseUtil.close(zipin);
97 }
98 }
99
100 public static void zipDirectory(final File sourceDirectory, final File targetZipFile) throws ZipUtilException {
101 LOG.info("Zipping directory '" + sourceDirectory.getAbsolutePath() + "' to file " +
102 "'" + targetZipFile.getAbsolutePath() + "'.");
103 assert(sourceDirectory.exists() && sourceDirectory.isDirectory());
104 assert(targetZipFile.exists() == false);
105
106 ZipOutputStream zipout = null;
107 boolean finishedSuccessfully = false;
108 try {
109
110 zipout = new ZipOutputStream(new FileOutputStream(targetZipFile));
111 zipout.setLevel(9);
112 zipFiles(zipout, sourceDirectory, sourceDirectory);
113 zipout.finish();
114 finishedSuccessfully = true;
115
116 } catch (Exception e) {
117 throw new ZipUtilException("Could not zip directory '" + sourceDirectory.getAbsolutePath() + "'!", e);
118 } finally {
119 CloseUtil.close(zipout);
120
121 if(finishedSuccessfully == false) {
122 if(targetZipFile.exists()) {
123 if(targetZipFile.delete() == false) {
124 LOG.warn("Could not delete zip file '" + targetZipFile.getAbsolutePath() + "'!");
125 }
126 }
127 }
128 }
129 }
130
131 private static void zipFiles(final ZipOutputStream zipout, final File file,
132 final File sourceDirectory) throws IOException {
133 if(file.isDirectory()) {
134 for (File subFile : file.listFiles()) {
135 zipFiles(zipout, subFile, sourceDirectory);
136 }
137 } else {
138 final String entryName = getZipEntryName(file, sourceDirectory);
139 LOG.debug("Zipping file '" + file.getAbsolutePath() + "' as entry '" + entryName + "'.");
140 final ZipEntry entry = new ZipEntry(entryName);
141
142 BufferedInputStream fileInput = null;
143 try {
144 fileInput = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
145
146 byte[] buffer = new byte[BUFFER_SIZE];
147 zipout.putNextEntry(entry);
148
149 int count = fileInput.read(buffer, 0, BUFFER_SIZE);
150 while (count != -1) {
151 zipout.write(buffer, 0, count);
152 count = fileInput.read(buffer, 0, BUFFER_SIZE);
153 }
154
155 zipout.closeEntry();
156 } finally {
157 CloseUtil.close(fileInput);
158 }
159 }
160 }
161
162 private static String getZipEntryName(final File file, final File sourceDirectory) {
163 final String filePath = file.getAbsolutePath();
164 return filePath.substring(sourceDirectory.getAbsolutePath().length() + 1, filePath.length());
165 }
166
167
168 public static void main(final String[] args) throws ZipUtilException {
169
170
171 File file = new File("/zip/asdf.script");
172 ZipFile zipFile;
173 try {
174 zipFile = new ZipFile(file);
175 } catch (ZipException e) {
176 System.out.println("invalid zip file");
177 return;
178 } catch (IOException e) {
179 System.out.println(e.getMessage());
180 return;
181 }
182 ZipUtil.unzip(file, zipFile, new File("/zip/unzippedCovers"));
183 }
184 }