Source Code Cross Referenced for ZipUtil.java in  » Content-Management-System » openedit » com » openedit » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » openedit » com.openedit.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.