001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.utils;
021
022 import org.apache.commons.io.FileUtils;
023 import org.apache.commons.io.IOUtils;
024
025 import java.io.*;
026 import java.util.Enumeration;
027 import java.util.zip.ZipEntry;
028 import java.util.zip.ZipFile;
029 import java.util.zip.ZipOutputStream;
030
031 /**
032 * @since 1.10
033 */
034 public final class ZipUtils {
035
036 private ZipUtils() {
037 }
038
039 public static void unzip(File zip, File toDir) throws IOException {
040 unzip(zip, toDir, new ZipEntryFilter() {
041 public boolean accept(ZipEntry entry) {
042 return true;
043 }
044 });
045 }
046
047 public static void unzip(File zip, File toDir, ZipEntryFilter filter) throws IOException {
048 if (!toDir.exists()) {
049 FileUtils.forceMkdir(toDir);
050 }
051
052 ZipFile zipFile = new ZipFile(zip);
053 try {
054 Enumeration<? extends ZipEntry> entries = zipFile.entries();
055 while (entries.hasMoreElements()) {
056 ZipEntry entry = entries.nextElement();
057 if (filter.accept(entry)) {
058 File to = new File(toDir, entry.getName());
059 if (entry.isDirectory()) {
060 if (!to.exists() && !to.mkdirs()) {
061 throw new IOException("Error creating directory: " + to);
062 }
063 } else {
064 File parent = to.getParentFile();
065 if (parent != null && !parent.exists() && !parent.mkdirs()) {
066 throw new IOException("Error creating directory: " + parent);
067 }
068
069 FileOutputStream fos = new FileOutputStream(to);
070 try {
071 IOUtils.copy(zipFile.getInputStream(entry), fos);
072 } finally {
073 IOUtils.closeQuietly(fos);
074 }
075 }
076 }
077 }
078 } finally {
079 zipFile.close();
080 }
081 }
082
083 public static void zipDir(File dir, File zip) throws IOException {
084 OutputStream out = null;
085 ZipOutputStream zout = null;
086 try {
087 out = FileUtils.openOutputStream(zip);
088 zout = new ZipOutputStream(out);
089 zip(dir, zout, null);
090
091 } finally {
092 IOUtils.closeQuietly(zout);
093 IOUtils.closeQuietly(out);
094 }
095 }
096
097
098 private static void _zip(String entryName, InputStream in, ZipOutputStream out) throws IOException {
099 ZipEntry zentry = new ZipEntry(entryName);
100 out.putNextEntry(zentry);
101 IOUtils.copy(in, out);
102 out.closeEntry();
103 }
104
105 private static void _zip(String entryName, File file, ZipOutputStream out) throws IOException {
106 if (file.isDirectory()) {
107 entryName += '/';
108 ZipEntry zentry = new ZipEntry(entryName);
109 out.putNextEntry(zentry);
110 out.closeEntry();
111 File[] files = file.listFiles();
112 for (int i = 0, len = files.length; i < len; i++) {
113 _zip(entryName + files[i].getName(), files[i], out);
114 }
115
116 } else {
117 InputStream in = null;
118 try {
119 in = new BufferedInputStream(new FileInputStream(file));
120 _zip(entryName, in, out);
121 } finally {
122 IOUtils.closeQuietly(in);
123 }
124 }
125 }
126
127 private static void zip(File file, ZipOutputStream out, String prefix) throws IOException {
128 if (prefix != null) {
129 int len = prefix.length();
130 if (len == 0) {
131 prefix = null;
132 } else if (prefix.charAt(len - 1) != '/') {
133 prefix += '/';
134 }
135 }
136 for (File child : file.listFiles()) {
137 String name = prefix != null ? prefix + child.getName() : child.getName();
138 _zip(name, child, out);
139 }
140 }
141
142 public static interface ZipEntryFilter {
143 boolean accept(ZipEntry entry);
144 }
145
146 }
147