xref: /AOO41X/main/l10ntools/source/help/HelpIndexerTool.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package com.sun.star.help;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.FileInputStream;
31*cdf0e10cSrcweir import java.io.FileOutputStream;
32*cdf0e10cSrcweir import java.util.Arrays;
33*cdf0e10cSrcweir import java.util.HashSet;
34*cdf0e10cSrcweir import java.util.List;
35*cdf0e10cSrcweir import java.util.zip.ZipEntry;
36*cdf0e10cSrcweir import java.util.zip.ZipOutputStream;
37*cdf0e10cSrcweir import java.util.zip.CRC32;
38*cdf0e10cSrcweir import org.apache.lucene.analysis.standard.StandardAnalyzer;
39*cdf0e10cSrcweir import org.apache.lucene.analysis.cjk.CJKAnalyzer;
40*cdf0e10cSrcweir import org.apache.lucene.analysis.Analyzer;
41*cdf0e10cSrcweir import org.apache.lucene.index.IndexWriter;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir import java.io.File;
44*cdf0e10cSrcweir import java.io.FileNotFoundException;
45*cdf0e10cSrcweir import java.io.IOException;
46*cdf0e10cSrcweir import java.util.Date;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir /**
50*cdf0e10cSrcweir    When this tool is used with long path names on Windows, that is paths which start
51*cdf0e10cSrcweir    with \\?\, then the caller must make sure that the path is unique. This is achieved
52*cdf0e10cSrcweir    by removing '.' and '..' from the path. Paths which are created by
53*cdf0e10cSrcweir    osl_getSystemPathFromFileURL fulfill this requirement. This is necessary because
54*cdf0e10cSrcweir    lucene is patched to not use File.getCanonicalPath. See long_path.patch in the lucene
55*cdf0e10cSrcweir    module.
56*cdf0e10cSrcweir  */
57*cdf0e10cSrcweir public class HelpIndexerTool
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     public HelpIndexerTool()
60*cdf0e10cSrcweir 	{
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     /**
65*cdf0e10cSrcweir      * @param args the command line arguments
66*cdf0e10cSrcweir      */
67*cdf0e10cSrcweir     public static void main( String[] args )
68*cdf0e10cSrcweir 	{
69*cdf0e10cSrcweir 		boolean bExtensionMode = false;
70*cdf0e10cSrcweir 		mainImpl( args, bExtensionMode );
71*cdf0e10cSrcweir 	}
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     public static void mainImpl( String[] args, boolean bExtensionMode )
74*cdf0e10cSrcweir 	{
75*cdf0e10cSrcweir         String aDirToZipStr = "";
76*cdf0e10cSrcweir         String aSrcDirStr = "";
77*cdf0e10cSrcweir         String aLanguageStr = "";
78*cdf0e10cSrcweir         String aModule = "";
79*cdf0e10cSrcweir         String aTargetZipFileStr = "";
80*cdf0e10cSrcweir         String aCfsName = "";
81*cdf0e10cSrcweir         String aSegmentName = "";
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         // Scan arguments
84*cdf0e10cSrcweir         //If this tool is invoked in the build process for extensions help,
85*cdf0e10cSrcweir         //then -extension must be set.
86*cdf0e10cSrcweir         boolean bExtension = false;
87*cdf0e10cSrcweir         boolean bLang = false;
88*cdf0e10cSrcweir         boolean bMod = false;
89*cdf0e10cSrcweir         boolean bZipDir = false;
90*cdf0e10cSrcweir         boolean bSrcDir = false;
91*cdf0e10cSrcweir         boolean bOutput = false;
92*cdf0e10cSrcweir         boolean bCfsName = false;
93*cdf0e10cSrcweir         boolean bSegmentName = false;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         int nArgCount = args.length;
96*cdf0e10cSrcweir         for( int i = 0 ; i < nArgCount ; i++ )
97*cdf0e10cSrcweir 		{
98*cdf0e10cSrcweir             if( "-extension".equals(args[i]) )
99*cdf0e10cSrcweir 			{
100*cdf0e10cSrcweir                 bExtension = true;
101*cdf0e10cSrcweir             }
102*cdf0e10cSrcweir             else if( "-lang".equals(args[i]) )
103*cdf0e10cSrcweir 			{
104*cdf0e10cSrcweir                 if( i + 1 < nArgCount )
105*cdf0e10cSrcweir 				{
106*cdf0e10cSrcweir                     aLanguageStr = args[i + 1];
107*cdf0e10cSrcweir                     bLang = true;
108*cdf0e10cSrcweir                 }
109*cdf0e10cSrcweir                 i++;
110*cdf0e10cSrcweir             }
111*cdf0e10cSrcweir 			else if( "-mod".equals(args[i]) )
112*cdf0e10cSrcweir 			{
113*cdf0e10cSrcweir                 if( i + 1 < nArgCount )
114*cdf0e10cSrcweir 				{
115*cdf0e10cSrcweir                     aModule = args[i + 1];
116*cdf0e10cSrcweir                     bMod = true;
117*cdf0e10cSrcweir                 }
118*cdf0e10cSrcweir                 i++;
119*cdf0e10cSrcweir             }
120*cdf0e10cSrcweir 			else if( "-zipdir".equals(args[i]) )
121*cdf0e10cSrcweir 			{
122*cdf0e10cSrcweir                 if( i + 1 < nArgCount )
123*cdf0e10cSrcweir 				{
124*cdf0e10cSrcweir                     aDirToZipStr = args[i + 1];
125*cdf0e10cSrcweir                     bZipDir = true;
126*cdf0e10cSrcweir                 }
127*cdf0e10cSrcweir                 i++;
128*cdf0e10cSrcweir             }
129*cdf0e10cSrcweir 			else if( "-srcdir".equals(args[i]) )
130*cdf0e10cSrcweir 			{
131*cdf0e10cSrcweir                 if( i + 1 < nArgCount )
132*cdf0e10cSrcweir 				{
133*cdf0e10cSrcweir                     aSrcDirStr = args[i + 1];
134*cdf0e10cSrcweir                     bSrcDir = true;
135*cdf0e10cSrcweir                 }
136*cdf0e10cSrcweir                 i++;
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir 			else if( "-o".equals(args[i]) )
139*cdf0e10cSrcweir 			{
140*cdf0e10cSrcweir                 if( i + 1 < nArgCount )
141*cdf0e10cSrcweir 				{
142*cdf0e10cSrcweir                     aTargetZipFileStr = args[i + 1];
143*cdf0e10cSrcweir                     bOutput = true;
144*cdf0e10cSrcweir                 }
145*cdf0e10cSrcweir                 i++;
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir 			else if( "-checkcfsandsegname".equals(args[i]) )
148*cdf0e10cSrcweir 			{
149*cdf0e10cSrcweir                 if( i + 1 < nArgCount )
150*cdf0e10cSrcweir 				{
151*cdf0e10cSrcweir                     aCfsName = args[i + 1] + ".cfs";
152*cdf0e10cSrcweir                     bCfsName = true;
153*cdf0e10cSrcweir                 }
154*cdf0e10cSrcweir                 i++;
155*cdf0e10cSrcweir                 if( i + 1 < nArgCount )
156*cdf0e10cSrcweir 				{
157*cdf0e10cSrcweir                     aSegmentName = "segments" + args[i + 1];
158*cdf0e10cSrcweir                     bSegmentName = true;
159*cdf0e10cSrcweir                 }
160*cdf0e10cSrcweir                 i++;
161*cdf0e10cSrcweir                 if (!(bCfsName && bSegmentName))
162*cdf0e10cSrcweir                 {
163*cdf0e10cSrcweir                     System.out.println("Usage: HelpIndexer -checkcfsandsegname _0 _3 (2 arguments needed)");
164*cdf0e10cSrcweir                     System.exit( -1 );
165*cdf0e10cSrcweir                 }
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir         }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         if( !bLang || !bMod || !bZipDir || (!bOutput && !bExtensionMode && !bExtension) )
170*cdf0e10cSrcweir 		{
171*cdf0e10cSrcweir 			if( bExtensionMode )
172*cdf0e10cSrcweir 				return;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 			System.out.println("Usage: HelpIndexer -lang ISOLangCode -mod HelpModule -zipdir TempZipDir -o OutputZipFile");
175*cdf0e10cSrcweir             System.out.println("Usage: HelpIndexer -extension -lang ISOLangCode -mod HelpModule -zipdir PathToLangDir");
176*cdf0e10cSrcweir 			System.exit( -1 );
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         String aIndexDirName = aModule + ".idxl";
180*cdf0e10cSrcweir         File aIndexDir = new File( aDirToZipStr + File.separator + aIndexDirName );
181*cdf0e10cSrcweir 		if( !bSrcDir )
182*cdf0e10cSrcweir 			aSrcDirStr = aDirToZipStr;
183*cdf0e10cSrcweir         File aCaptionFilesDir = new File( aSrcDirStr + File.separator + "caption" );
184*cdf0e10cSrcweir         File aContentFilesDir = new File( aSrcDirStr + File.separator + "content" );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         try
187*cdf0e10cSrcweir 		{
188*cdf0e10cSrcweir             Date start = new Date();
189*cdf0e10cSrcweir             Analyzer analyzer = aLanguageStr.equals("ja") ? (Analyzer)new CJKAnalyzer() : (Analyzer)new StandardAnalyzer();
190*cdf0e10cSrcweir             IndexWriter writer = new IndexWriter( aIndexDir, analyzer, true );
191*cdf0e10cSrcweir 			if( !bExtensionMode )
192*cdf0e10cSrcweir 	            System.out.println( "Lucene: Indexing to directory '" + aIndexDir + "'..." );
193*cdf0e10cSrcweir             int nRet = indexDocs( writer, aModule, bExtensionMode, aCaptionFilesDir, aContentFilesDir );
194*cdf0e10cSrcweir             if( nRet != -1 )
195*cdf0e10cSrcweir 			{
196*cdf0e10cSrcweir 				if( !bExtensionMode )
197*cdf0e10cSrcweir 				{
198*cdf0e10cSrcweir 					System.out.println();
199*cdf0e10cSrcweir 					System.out.println( "Optimizing ..." );
200*cdf0e10cSrcweir 				}
201*cdf0e10cSrcweir                 writer.optimize();
202*cdf0e10cSrcweir             }
203*cdf0e10cSrcweir             writer.close();
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 			boolean bCfsFileOk = true;
206*cdf0e10cSrcweir 			boolean bSegmentFileOk = true;
207*cdf0e10cSrcweir 			if( bCfsName && bSegmentName && !bExtensionMode && nRet != -1 )
208*cdf0e10cSrcweir 			{
209*cdf0e10cSrcweir 				String aCompleteCfsFileName = aDirToZipStr + File.separator + aIndexDirName + File.separator + aCfsName;
210*cdf0e10cSrcweir 				String aCompleteSegmentFileName = aDirToZipStr + File.separator + aIndexDirName + File.separator + aSegmentName;
211*cdf0e10cSrcweir 				File aCfsFile = new File( aCompleteCfsFileName );
212*cdf0e10cSrcweir 				File aSegmentFile = new File( aCompleteSegmentFileName );
213*cdf0e10cSrcweir 				bCfsFileOk = aCfsFile.exists();
214*cdf0e10cSrcweir 				bSegmentFileOk = aSegmentFile.exists();
215*cdf0e10cSrcweir 				System.out.println( "Checking cfs file " + aCfsName+ ": " + (bCfsFileOk ? "Found" : "Not found") );
216*cdf0e10cSrcweir 				System.out.println( "Checking segment file " + aSegmentName+ ": " + (bSegmentFileOk ? "Found" : "Not found") );
217*cdf0e10cSrcweir 			}
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 			if( bExtensionMode || bExtension)
220*cdf0e10cSrcweir 			{
221*cdf0e10cSrcweir 				if( !bSrcDir )
222*cdf0e10cSrcweir 				{
223*cdf0e10cSrcweir 					deleteRecursively( aCaptionFilesDir );
224*cdf0e10cSrcweir 					deleteRecursively( aContentFilesDir );
225*cdf0e10cSrcweir 				}
226*cdf0e10cSrcweir 			}
227*cdf0e10cSrcweir 			else
228*cdf0e10cSrcweir 			{
229*cdf0e10cSrcweir 				if( nRet == -1 )
230*cdf0e10cSrcweir 					deleteRecursively( aIndexDir );
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 				if( bCfsFileOk && bSegmentFileOk )
233*cdf0e10cSrcweir 					System.out.println( "Zipping ..." );
234*cdf0e10cSrcweir 				File aDirToZipFile = new File( aDirToZipStr );
235*cdf0e10cSrcweir 				createZipFile( aDirToZipFile, aTargetZipFileStr );
236*cdf0e10cSrcweir 				deleteRecursively( aDirToZipFile );
237*cdf0e10cSrcweir 			}
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 			if( !bCfsFileOk )
240*cdf0e10cSrcweir 			{
241*cdf0e10cSrcweir 				System.out.println( "cfs file check failed, terminating..." );
242*cdf0e10cSrcweir 				System.exit( -1 );
243*cdf0e10cSrcweir 			}
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 			if( !bSegmentFileOk )
246*cdf0e10cSrcweir 			{
247*cdf0e10cSrcweir 				System.out.println( "segment file check failed, terminating..." );
248*cdf0e10cSrcweir 				System.exit( -1 );
249*cdf0e10cSrcweir 			}
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 			Date end = new Date();
252*cdf0e10cSrcweir 			if( !bExtensionMode )
253*cdf0e10cSrcweir 				System.out.println(end.getTime() - start.getTime() + " total milliseconds");
254*cdf0e10cSrcweir         }
255*cdf0e10cSrcweir 		catch (IOException e)
256*cdf0e10cSrcweir 		{
257*cdf0e10cSrcweir 			if( bExtensionMode )
258*cdf0e10cSrcweir 				return;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 			System.out.println(" caught a " + e.getClass() +
261*cdf0e10cSrcweir 				"\n with message: " + e.getMessage());
262*cdf0e10cSrcweir 			System.exit( -1 );
263*cdf0e10cSrcweir         }
264*cdf0e10cSrcweir     }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 	private static int indexDocs(IndexWriter writer, String aModule, boolean bExtensionMode,
267*cdf0e10cSrcweir 		File aCaptionFilesDir, File aContentFilesDir) throws IOException
268*cdf0e10cSrcweir 	{
269*cdf0e10cSrcweir         if( !aCaptionFilesDir.canRead() || !aCaptionFilesDir.isDirectory() )
270*cdf0e10cSrcweir 		{
271*cdf0e10cSrcweir 			if( !bExtensionMode )
272*cdf0e10cSrcweir 	            System.out.println( "Not found: " + aCaptionFilesDir );
273*cdf0e10cSrcweir             return -1;
274*cdf0e10cSrcweir         }
275*cdf0e10cSrcweir         if( !aContentFilesDir.canRead() || !aContentFilesDir.isDirectory() )
276*cdf0e10cSrcweir 		{
277*cdf0e10cSrcweir 			if( !bExtensionMode )
278*cdf0e10cSrcweir 	            System.out.println( "Not found: " + aContentFilesDir );
279*cdf0e10cSrcweir             return -1;
280*cdf0e10cSrcweir         }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir         String[] aCaptionFiles = aCaptionFilesDir.list();
283*cdf0e10cSrcweir         List aCaptionFilesList = Arrays.asList( aCaptionFiles );
284*cdf0e10cSrcweir         HashSet aCaptionFilesHashSet = new HashSet( aCaptionFilesList );
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir         String[] aContentFiles = aContentFilesDir.list();
287*cdf0e10cSrcweir         List aContentFilesList = Arrays.asList( aContentFiles );
288*cdf0e10cSrcweir         HashSet aContentFilesHashSet = new HashSet( aContentFilesList );
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir         // Loop over caption files and find corresponding content file
291*cdf0e10cSrcweir 		if( !bExtensionMode )
292*cdf0e10cSrcweir 	        System.out.println( "Indexing, adding files" );
293*cdf0e10cSrcweir         int nCaptionFilesLen = aCaptionFiles.length;
294*cdf0e10cSrcweir         for( int i = 0 ; i < nCaptionFilesLen ; i++ )
295*cdf0e10cSrcweir 		{
296*cdf0e10cSrcweir             String aCaptionFileStr = aCaptionFiles[i];
297*cdf0e10cSrcweir             File aCaptionFile = new File( aCaptionFilesDir, aCaptionFileStr );
298*cdf0e10cSrcweir             File aContentFile = null;
299*cdf0e10cSrcweir             if( aContentFilesHashSet.contains( aCaptionFileStr ) )
300*cdf0e10cSrcweir                 aContentFile = new File( aContentFilesDir, aCaptionFileStr );
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 			if( !bExtensionMode )
303*cdf0e10cSrcweir 				System.out.print( "." );
304*cdf0e10cSrcweir             writer.addDocument( HelpFileDocument.Document( aModule, aCaptionFile, aContentFile ) );
305*cdf0e10cSrcweir         }
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir         // Loop over content files to find remaining files not mapped to caption files
308*cdf0e10cSrcweir         int nContentFilesLen = aContentFiles.length;
309*cdf0e10cSrcweir         for( int i = 0 ; i < nContentFilesLen ; i++ )
310*cdf0e10cSrcweir 		{
311*cdf0e10cSrcweir             String aContentFileStr = aContentFiles[i];
312*cdf0e10cSrcweir             if( !aCaptionFilesHashSet.contains( aContentFileStr ) )
313*cdf0e10cSrcweir 			{
314*cdf0e10cSrcweir                 // Not already handled in caption files loop
315*cdf0e10cSrcweir                 File aCaptionFile = null;
316*cdf0e10cSrcweir                 File aContentFile = new File( aContentFilesDir, aContentFileStr );
317*cdf0e10cSrcweir 				if( !bExtensionMode )
318*cdf0e10cSrcweir 					System.out.print( "." );
319*cdf0e10cSrcweir                 writer.addDocument( HelpFileDocument.Document( aModule, aCaptionFile, aContentFile ) );
320*cdf0e10cSrcweir             }
321*cdf0e10cSrcweir         }
322*cdf0e10cSrcweir         return 0;
323*cdf0e10cSrcweir     }
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir     public static void createZipFile( File aDirToZip, String aTargetZipFileStr )
326*cdf0e10cSrcweir             throws FileNotFoundException, IOException
327*cdf0e10cSrcweir 	{
328*cdf0e10cSrcweir         FileOutputStream fos = new FileOutputStream( aTargetZipFileStr );
329*cdf0e10cSrcweir         ZipOutputStream zos = new ZipOutputStream( fos );
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir         File[] aChildrenFiles = aDirToZip.listFiles();
332*cdf0e10cSrcweir         int nFileCount = aChildrenFiles.length;
333*cdf0e10cSrcweir         for( int i = 0 ; i < nFileCount ; i++ )
334*cdf0e10cSrcweir             addToZipRecursively( zos, aChildrenFiles[i], null );
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir         zos.close();
337*cdf0e10cSrcweir     }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir     public static void addToZipRecursively( ZipOutputStream zos, File aFile, String aBasePath )
340*cdf0e10cSrcweir             throws FileNotFoundException, IOException
341*cdf0e10cSrcweir 	{
342*cdf0e10cSrcweir         if( aFile.isDirectory() )
343*cdf0e10cSrcweir 		{
344*cdf0e10cSrcweir             String aDirName = aFile.getName();
345*cdf0e10cSrcweir             if( aDirName.equalsIgnoreCase( "caption" ) || aDirName.equalsIgnoreCase( "content" ) )
346*cdf0e10cSrcweir                 return;
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir             File[] aChildrenFiles = aFile.listFiles();
349*cdf0e10cSrcweir             String aNewBasePath = "";
350*cdf0e10cSrcweir             if( aBasePath != null )
351*cdf0e10cSrcweir                 aNewBasePath += aBasePath + File.separator;
352*cdf0e10cSrcweir             aNewBasePath += aDirName;
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir             int nFileCount = aChildrenFiles.length;
355*cdf0e10cSrcweir             for( int i = 0 ; i < nFileCount ; i++ )
356*cdf0e10cSrcweir                 addToZipRecursively( zos, aChildrenFiles[i], aNewBasePath );
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir             return;
359*cdf0e10cSrcweir         }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir         // No directory
362*cdf0e10cSrcweir         // read contents of file we are going to put in the zip
363*cdf0e10cSrcweir         int fileLength = (int) aFile.length();
364*cdf0e10cSrcweir         FileInputStream fis = new FileInputStream( aFile );
365*cdf0e10cSrcweir         byte[] wholeFile = new byte[fileLength];
366*cdf0e10cSrcweir         int bytesRead = fis.read( wholeFile, 0, fileLength );
367*cdf0e10cSrcweir         fis.close();
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir         String aFileName = aFile.getName();
370*cdf0e10cSrcweir         String aEntryName = "";
371*cdf0e10cSrcweir         if( aBasePath != null )
372*cdf0e10cSrcweir             aEntryName += aBasePath + "/";
373*cdf0e10cSrcweir         aEntryName += aFileName;
374*cdf0e10cSrcweir         ZipEntry aZipEntry = new ZipEntry( aEntryName );
375*cdf0e10cSrcweir         aZipEntry.setTime( aFile.lastModified() );
376*cdf0e10cSrcweir         aZipEntry.setSize( fileLength );
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir         int nMethod = ( aFileName.toLowerCase().endsWith( ".jar" ) )
379*cdf0e10cSrcweir                 ? ZipEntry.STORED : ZipEntry.DEFLATED;
380*cdf0e10cSrcweir         aZipEntry.setMethod( nMethod );
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir         CRC32 tempCRC = new CRC32();
383*cdf0e10cSrcweir         tempCRC.update( wholeFile, 0, wholeFile.length );
384*cdf0e10cSrcweir         aZipEntry.setCrc( tempCRC.getValue() );
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir         // write the contents into the zip element
387*cdf0e10cSrcweir         zos.putNextEntry( aZipEntry );
388*cdf0e10cSrcweir         zos.write( wholeFile, 0, fileLength );
389*cdf0e10cSrcweir         zos.closeEntry();
390*cdf0e10cSrcweir     }
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir     static public boolean deleteRecursively( File aFile )
393*cdf0e10cSrcweir 	{
394*cdf0e10cSrcweir         if( aFile.isDirectory() )
395*cdf0e10cSrcweir 		{
396*cdf0e10cSrcweir             File[] aChildrenFiles = aFile.listFiles();
397*cdf0e10cSrcweir             int nFileCount = aChildrenFiles.length;
398*cdf0e10cSrcweir             for( int i = 0 ; i < nFileCount ; i++ )
399*cdf0e10cSrcweir 			{
400*cdf0e10cSrcweir                 File aChildrenFile = aChildrenFiles[i];
401*cdf0e10cSrcweir                 boolean bSuccess = deleteRecursively( aChildrenFile );
402*cdf0e10cSrcweir                 if( !bSuccess )
403*cdf0e10cSrcweir                     return false;
404*cdf0e10cSrcweir             }
405*cdf0e10cSrcweir         }
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir         return aFile.delete();
408*cdf0e10cSrcweir     }
409*cdf0e10cSrcweir }
410*cdf0e10cSrcweir 
411