KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > util > ZipUtil


1 /*
2  * Created on Apr 22, 2006
3  */

4 package com.openedit.util;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.zip.ZipEntry JavaDoc;
17 import java.util.zip.ZipInputStream JavaDoc;
18 import java.util.zip.ZipOutputStream JavaDoc;
19
20 import com.openedit.OpenEditException;
21
22
23 public class ZipUtil
24 {
25     protected List JavaDoc fieldExcludes;
26     protected OutputFiller filler = new OutputFiller();
27     protected String JavaDoc fieldFindFileName;
28     protected boolean fieldExitOnFirstFind;
29     protected File JavaDoc fieldRoot;
30     
31     protected List JavaDoc getExcludes()
32     {
33         if (fieldExcludes == null)
34         {
35             fieldExcludes = new ArrayList JavaDoc();
36         }
37
38         return fieldExcludes;
39     }
40     public void addExclude(String JavaDoc inPath)
41     {
42         //dont unzip or zip containing this path
43
getExcludes().add(inPath);
44     }
45     public void unzip(String JavaDoc inPage, String JavaDoc inDest) throws Exception JavaDoc
46     {
47         unzip(new File JavaDoc( inPage), new File JavaDoc( inDest ));
48     }
49     public void unzip(File JavaDoc inPage, File JavaDoc inDest) throws IOException JavaDoc
50     {
51         InputStream JavaDoc in = null;
52         try
53         {
54             in = new FileInputStream JavaDoc(inPage);
55             ZipInputStream JavaDoc unzip = new ZipInputStream JavaDoc(in);
56             ZipEntry JavaDoc entry = unzip.getNextEntry();
57             
58             while( entry != null)
59             {
60                 if( fieldFindFileName != null )
61                 {
62                     if( ! PathUtilities.match(entry.getName(),fieldFindFileName)) //TODO: Handle leading /
63
{
64                         entry = unzip.getNextEntry();
65                         continue;
66                     }
67                 }
68
69                 //int zipeSize = (int) entry.getSize();
70
if( !entry.isDirectory() )
71                 {
72                     File JavaDoc ufile = new File JavaDoc( inDest, entry.getName());
73                     ufile.getParentFile().mkdirs();
74                     FileOutputStream JavaDoc tout = new FileOutputStream JavaDoc(ufile);
75                     try
76                     {
77                         filler.fill(unzip,tout);
78                     }
79                     finally
80                     {
81                         FileUtils.safeClose(tout);
82                     }
83                     ufile.setLastModified(entry.getTime());
84                     if( isExitOnFirstFind() )
85                     {
86                         return;
87                     }
88                 }
89                 entry = unzip.getNextEntry();
90             }
91         }
92         finally
93         {
94             FileUtils.safeClose(in);
95         }
96     }
97     /*
98     public void zipFilesIn( File inDir,OutputStream inToBrowser ) throws IOException, OpenEditException
99     {
100         zipFilesIn(inDir,"/", inToBrowser);
101     }
102     */

103     /*
104     public void zipFilesIn( File inDir,String inIncludePath, OutputStream inToBrowser ) throws IOException, OpenEditException
105     {
106
107         String startingdir = inDir.getAbsolutePath();
108         if ( !startingdir.endsWith(File.separator))
109         {
110             startingdir = startingdir + File.separator;
111         }
112         if( inIncludePath.length() > 1)
113         {
114             inDir = new File( inDir, inIncludePath);
115         }
116         
117         ZipOutputStream finalZip = new ZipOutputStream(inToBrowser);
118         
119         File[] children = inDir.listFiles();
120         if ( children != null)
121         {
122             for (int i = 0; i < children.length; i++)
123             {
124                 addToZip( children[i], startingdir, finalZip);
125             }
126         }
127         finalZip.close();
128     }
129     */

130     public void addTozip( String JavaDoc inContent, String JavaDoc inName, ZipOutputStream JavaDoc finalZip ) throws IOException JavaDoc
131     {
132         ZipEntry JavaDoc entry = new ZipEntry JavaDoc( inName );
133         entry.setSize( inContent.length() );
134         entry.setTime( new Date JavaDoc().getTime() );
135         finalZip.putNextEntry( entry );
136         finalZip.write(inContent.getBytes("UTF-8"));
137         finalZip.closeEntry();
138         
139     }
140
141     public void zipFile( String JavaDoc inOpenEditPath, OutputStream JavaDoc inToBrowser ) throws IOException JavaDoc, OpenEditException
142     {
143         ZipOutputStream JavaDoc finalZip = new ZipOutputStream JavaDoc(inToBrowser);
144         //String startingdir = inRoot.getAbsolutePath();
145
//String rootdirectory = inRoot.getAbsolutePath().substring(inOpenEditPath.getAbsolutePath().length()-1);
146

147 // if ( inRoot.isDirectory() )
148
// {
149
// if ( !startingdir.endsWith(File.separator))
150
// {
151
// startingdir = startingdir + File.separator;
152
// }
153
// }
154
// else
155
// {
156
// startingdir = startingdir.substring(0,startingdir.length() - inPath.getName().length() );
157
// }
158

159         File JavaDoc file = new File JavaDoc( getRoot(), inOpenEditPath);
160         addToZip(file, finalZip);
161
162         finalZip.close();
163     }
164     protected void addToZip(File JavaDoc inPath,ZipOutputStream JavaDoc inFinalZip) throws IOException JavaDoc, OpenEditException
165     {
166         
167         //This is the name as it's entered in the zip file
168
String JavaDoc zipname = inPath.getAbsolutePath().substring(getRoot().getAbsolutePath().length());
169
170         String JavaDoc openeditpath = zipname.replace('\\', '/'); //Windows replace
171

172         //The zip entry can't begin with any slashes
173
//but because of the logic we use (and the listFiles method), there could be two.
174
while (zipname.startsWith("/"))
175         {
176             zipname = zipname.substring(1, zipname.length());
177         }
178         
179         if( inPath.isDirectory() && !openeditpath.endsWith("/"))
180         {
181             openeditpath = openeditpath + "/"; //Make sure we leave the / on there for directories
182
}
183         
184         //See if we should skip this file
185
if( !isValidEntry( openeditpath ))
186         {
187             return;
188         }
189         
190         //Loop over files
191
if( inPath.isDirectory())
192         {
193             File JavaDoc[] files = inPath.listFiles();
194             if ( files != null)
195             {
196                 for (int i = 0; i < files.length; i++)
197                 {
198                     addToZip(files[i],inFinalZip);
199                 }
200             }
201         }
202         else
203         {
204             ZipEntry JavaDoc entry = new ZipEntry JavaDoc( zipname );
205             entry.setSize( inPath.length() );
206             entry.setTime( inPath.lastModified() );
207             FileInputStream JavaDoc fis = new FileInputStream JavaDoc( inPath );
208             inFinalZip.putNextEntry( entry );
209             try
210             {
211                 new OutputFiller().fill( fis, inFinalZip );
212             }
213             finally
214             {
215                 fis.close();
216             }
217             inFinalZip.closeEntry();
218         }
219     }
220     protected boolean isValidEntry(String JavaDoc inPath) throws OpenEditException
221     {
222         if( exclude( inPath ))
223         {
224             return false;
225         }
226         return true;
227     }
228     protected boolean exclude(String JavaDoc inOpenEditPath)
229     {
230         if( fieldExcludes != null)
231         {
232             for (Iterator JavaDoc iter = getExcludes().iterator(); iter.hasNext();)
233             {
234                 String JavaDoc wild = (String JavaDoc)iter.next();
235                 if ( PathUtilities.match(inOpenEditPath, wild ) )
236                 {
237                     return true;
238                 }
239             }
240         }
241         return false;
242     }
243     public String JavaDoc getFindFileName()
244     {
245         return fieldFindFileName;
246     }
247     public void setFindFileName(String JavaDoc inFindFileName)
248     {
249         fieldFindFileName = inFindFileName;
250     }
251     public boolean isExitOnFirstFind()
252     {
253         return fieldExitOnFirstFind;
254     }
255     public void setExitOnFirstFind(boolean inExitOnFirstFind)
256     {
257         fieldExitOnFirstFind = inExitOnFirstFind;
258     }
259     public File JavaDoc getRoot()
260     {
261         return fieldRoot;
262     }
263     public void setRoot(File JavaDoc inRoot)
264     {
265         fieldRoot = inRoot;
266     }
267 }
Popular Tags