001: /*
002: * Created on Apr 22, 2006
003: */
004: package com.openedit.util;
005:
006: import java.io.File;
007: import java.io.FileInputStream;
008: import java.io.FileOutputStream;
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.OutputStream;
012: import java.util.ArrayList;
013: import java.util.Date;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.zip.ZipEntry;
017: import java.util.zip.ZipInputStream;
018: import java.util.zip.ZipOutputStream;
019:
020: import com.openedit.OpenEditException;
021:
022: public class ZipUtil {
023: protected List fieldExcludes;
024: protected OutputFiller filler = new OutputFiller();
025: protected String fieldFindFileName;
026: protected boolean fieldExitOnFirstFind;
027: protected File fieldRoot;
028:
029: protected List getExcludes() {
030: if (fieldExcludes == null) {
031: fieldExcludes = new ArrayList();
032: }
033:
034: return fieldExcludes;
035: }
036:
037: public void addExclude(String inPath) {
038: //dont unzip or zip containing this path
039: getExcludes().add(inPath);
040: }
041:
042: //TODO: Should these use the getRoot()
043: public void unzip(String inPage, String inDest) throws Exception {
044: unzip(new File(inPage), new File(inDest));
045: }
046:
047: public void unzip(File inPage, File inDest) throws IOException {
048: InputStream in = null;
049: try {
050: in = new FileInputStream(inPage);
051: unzip(in, inDest);
052: } finally {
053: FileUtils.safeClose(in);
054: }
055: }
056:
057: public void unzip(InputStream inZip, File inDest)
058: throws IOException {
059: ZipInputStream unzip = new ZipInputStream(inZip);
060: ZipEntry entry = unzip.getNextEntry();
061:
062: while (entry != null) {
063: if (fieldFindFileName != null) {
064: if (!PathUtilities.match(entry.getName(),
065: fieldFindFileName)) //TODO: Handle leading /
066: {
067: entry = unzip.getNextEntry();
068: continue;
069: }
070: }
071:
072: //int zipeSize = (int) entry.getSize();
073: if (!entry.isDirectory()) {
074: File ufile = new File(inDest, entry.getName());
075: ufile.getParentFile().mkdirs();
076: FileOutputStream tout = new FileOutputStream(ufile);
077: try {
078: filler.fill(unzip, tout);
079: } finally {
080: FileUtils.safeClose(tout);
081: }
082: ufile.setLastModified(entry.getTime());
083: if (isExitOnFirstFind()) {
084: return;
085: }
086: }
087: entry = unzip.getNextEntry();
088: }
089:
090: }
091:
092: /*
093: public void zipFilesIn( File inDir,OutputStream inToBrowser ) throws IOException, OpenEditException
094: {
095: zipFilesIn(inDir,"/", inToBrowser);
096: }
097: */
098: /*
099: public void zipFilesIn( File inDir,String inIncludePath, OutputStream inToBrowser ) throws IOException, OpenEditException
100: {
101:
102: String startingdir = inDir.getAbsolutePath();
103: if ( !startingdir.endsWith(File.separator))
104: {
105: startingdir = startingdir + File.separator;
106: }
107: if( inIncludePath.length() > 1)
108: {
109: inDir = new File( inDir, inIncludePath);
110: }
111:
112: ZipOutputStream finalZip = new ZipOutputStream(inToBrowser);
113:
114: File[] children = inDir.listFiles();
115: if ( children != null)
116: {
117: for (int i = 0; i < children.length; i++)
118: {
119: addToZip( children[i], startingdir, finalZip);
120: }
121: }
122: finalZip.close();
123: }
124: */
125: public void addTozip(String inContent, String inName,
126: ZipOutputStream finalZip) throws IOException {
127: ZipEntry entry = new ZipEntry(inName);
128: entry.setSize(inContent.length());
129: entry.setTime(new Date().getTime());
130: finalZip.putNextEntry(entry);
131: finalZip.write(inContent.getBytes("UTF-8"));
132: finalZip.closeEntry();
133:
134: }
135:
136: public void zipFile(String inOpenEditPath, OutputStream inToBrowser)
137: throws IOException, OpenEditException {
138: ZipOutputStream finalZip = new ZipOutputStream(inToBrowser);
139: if (inOpenEditPath.equals("/")) {
140: addToZip(getRoot(), finalZip);
141: } else {
142: File file = new File(getRoot(), inOpenEditPath);
143: addToZip(file, finalZip);
144: }
145: finalZip.close();
146: }
147:
148: public void zipFiles(List inPaths, OutputStream inToBrowser)
149: throws IOException, OpenEditException {
150: ZipOutputStream finalZip = new ZipOutputStream(inToBrowser);
151: for (Iterator iterator = inPaths.iterator(); iterator.hasNext();) {
152: String path = (String) iterator.next();
153: File file = new File(getRoot(), path);
154: addToZip(file, finalZip);
155: }
156: finalZip.close();
157: }
158:
159: protected void addToZip(File inPath, ZipOutputStream inFinalZip)
160: throws IOException, OpenEditException {
161:
162: //This is the name as it's entered in the zip file
163: String zipname = inPath.getAbsolutePath().substring(
164: getRoot().getAbsolutePath().length());
165:
166: String openeditpath = zipname.replace('\\', '/'); //Windows replace
167:
168: //The zip entry can't begin with any slashes
169: //but because of the logic we use (and the listFiles method), there could be two.
170: while (zipname.startsWith("/")) {
171: zipname = zipname.substring(1, zipname.length());
172: }
173:
174: if (inPath.isDirectory() && !openeditpath.endsWith("/")) {
175: openeditpath = openeditpath + "/"; //Make sure we leave the / on there for directories
176: }
177:
178: //See if we should skip this file
179: if (!isValidEntry(openeditpath)) {
180: return;
181: }
182:
183: //Loop over files
184: if (inPath.isDirectory()) {
185: File[] files = inPath.listFiles();
186: if (files != null) {
187: for (int i = 0; i < files.length; i++) {
188: addToZip(files[i], inFinalZip);
189: }
190: }
191: } else {
192: ZipEntry entry = new ZipEntry(zipname);
193: entry.setSize(inPath.length());
194: entry.setTime(inPath.lastModified());
195: FileInputStream fis = new FileInputStream(inPath);
196: inFinalZip.putNextEntry(entry);
197: try {
198: new OutputFiller().fill(fis, inFinalZip);
199: } finally {
200: fis.close();
201: }
202: inFinalZip.closeEntry();
203: }
204: }
205:
206: protected boolean isValidEntry(String inPath)
207: throws OpenEditException {
208: if (exclude(inPath)) {
209: return false;
210: }
211: return true;
212: }
213:
214: protected boolean exclude(String inOpenEditPath) {
215: if (fieldExcludes != null) {
216: for (Iterator iter = getExcludes().iterator(); iter
217: .hasNext();) {
218: String wild = (String) iter.next();
219: if (PathUtilities.match(inOpenEditPath, wild)) {
220: return true;
221: }
222: }
223: }
224: return false;
225: }
226:
227: public String getFindFileName() {
228: return fieldFindFileName;
229: }
230:
231: public void setFindFileName(String inFindFileName) {
232: fieldFindFileName = inFindFileName;
233: }
234:
235: public boolean isExitOnFirstFind() {
236: return fieldExitOnFirstFind;
237: }
238:
239: public void setExitOnFirstFind(boolean inExitOnFirstFind) {
240: fieldExitOnFirstFind = inExitOnFirstFind;
241: }
242:
243: public File getRoot() {
244: return fieldRoot;
245: }
246:
247: public void setRoot(File inRoot) {
248: fieldRoot = inRoot;
249: }
250: }
|