KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > controllers > kernel > impl > simple > DigitalAssetDeliveryController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.controllers.kernel.impl.simple;
25
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.BufferedOutputStream JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileDescriptor JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.FileWriter JavaDoc;
33 import java.io.FilenameFilter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.nio.channels.FileChannel JavaDoc;
39 import java.nio.channels.FileLock JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.Vector JavaDoc;
45 import java.util.zip.ZipEntry JavaDoc;
46 import java.util.zip.ZipFile JavaDoc;
47
48 import org.apache.log4j.Logger;
49 import org.infoglue.cms.controllers.kernel.impl.simple.DigitalAssetController;
50 import org.infoglue.cms.entities.content.DigitalAsset;
51 import org.infoglue.cms.entities.content.DigitalAssetVO;
52 import org.infoglue.cms.entities.management.Repository;
53 import org.infoglue.cms.exception.SystemException;
54 import org.infoglue.cms.util.CmsPropertyHandler;
55 import org.infoglue.cms.util.graphics.ThumbnailGenerator;
56 import org.infoglue.deliver.applications.databeans.DeliveryContext;
57 import org.infoglue.deliver.controllers.kernel.URLComposer;
58 import org.infoglue.deliver.util.HttpHelper;
59 import org.infoglue.deliver.util.Timer;
60
61
62 public class DigitalAssetDeliveryController extends BaseDeliveryController
63 {
64     private final static Logger logger = Logger.getLogger(DigitalAssetDeliveryController.class.getName());
65
66     class FilenameFilterImpl implements FilenameFilter JavaDoc
67     {
68         private String JavaDoc filter = ".";
69         
70         public FilenameFilterImpl(String JavaDoc aFilter)
71         {
72             filter = aFilter;
73         }
74         
75         public boolean accept(File JavaDoc dir, String JavaDoc name)
76         {
77             return name.startsWith(filter);
78         }
79     };
80
81
82     /**
83      * Private constructor to enforce factory-use
84      */

85     
86     private DigitalAssetDeliveryController()
87     {
88     }
89     
90     /**
91      * Factory method
92      */

93     
94     public static DigitalAssetDeliveryController getDigitalAssetDeliveryController()
95     {
96         return new DigitalAssetDeliveryController();
97     }
98     
99     
100     /**
101      * This is the basic way of getting an asset-url for a digital asset.
102      * If the asset is cached on disk it returns that path imediately it's ok - otherwise it dumps it fresh.
103      */

104
105     public String JavaDoc getAssetUrl(DigitalAsset digitalAsset, Repository repository, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
106     {
107         String JavaDoc assetUrl = "";
108         
109         if(digitalAsset != null)
110         {
111             String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
112             //String filePath = CmsPropertyHandler.getDigitalAssetPath();
113

114             int i = 0;
115             String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
116             //System.out.println("filePath:" + filePath);
117
while(filePath != null)
118             {
119                 try
120                 {
121                     DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
122                 }
123                 catch(Exception JavaDoc e)
124                 {
125                     logger.warn("An file could not be written:" + e.getMessage(), e);
126                 }
127
128                 i++;
129                 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
130                 //System.out.println("filePath:" + filePath);
131
}
132
133             //DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
134

135             String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
136             if(repository != null && repository.getDnsName() != null && !repository.getDnsName().equals(""))
137                 dnsName = repository.getDnsName();
138                 
139             assetUrl = URLComposer.getURLComposer().composeDigitalAssetUrl(dnsName, fileName, deliveryContext);
140         }
141
142         return assetUrl;
143     }
144
145
146     /**
147      * This is the basic way of getting an asset-url for a digital asset.
148      * If the asset is cached on disk it returns that path imediately it's ok - otherwise it dumps it fresh.
149      */

150
151     public String JavaDoc getAssetThumbnailUrl(DigitalAsset digitalAsset, Repository repository, int width, int height, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
152     {
153         String JavaDoc assetUrl = "";
154         
155         if(digitalAsset != null)
156         {
157             String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
158             String JavaDoc thumbnailFileName = "thumbnail_" + width + "_" + height + "_" + fileName;
159
160             int i = 0;
161             String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
162             while(filePath != null)
163             {
164                 try
165                 {
166                     DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
167                     DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(fileName, thumbnailFileName, filePath, width, height);
168                 }
169                 catch(Exception JavaDoc e)
170                 {
171                     logger.warn("An file could not be written:" + e.getMessage(), e);
172                 }
173                 
174                 i++;
175                 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
176             }
177
178             //String filePath = CmsPropertyHandler.getDigitalAssetPath();
179
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
180
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(digitalAsset, fileName, thumbnailFileName, filePath, width, height);
181

182             String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
183             if(repository != null && repository.getDnsName() != null && !repository.getDnsName().equals(""))
184                 dnsName = repository.getDnsName();
185                 
186             assetUrl = URLComposer.getURLComposer().composeDigitalAssetUrl(dnsName, thumbnailFileName, deliveryContext);
187         }
188
189         return assetUrl;
190     }
191
192     
193     /**
194      * This method checks if the given file exists on disk. If it does it's ignored because
195      * that means that the file is allready cached on the server. If not we dump
196      * the text on it.
197      */

198     
199     public void dumpAttributeToFile(String JavaDoc attributeValue, String JavaDoc fileName, String JavaDoc filePath) throws Exception JavaDoc
200     {
201         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
202         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(outputFile));
203         pw.println(attributeValue);
204         pw.close();
205     }
206     
207     /**
208      * This method checks if the given file exists on disk. If it does it's ignored because
209      * that means that the file is allready cached on the server. If not we dump
210      * the given url on it.
211      */

212     
213     public File JavaDoc dumpUrlToFile(String JavaDoc fileName, String JavaDoc filePath, String JavaDoc pageContent) throws Exception JavaDoc
214     {
215         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
216         logger.info("outputFile:" + outputFile.getAbsolutePath());
217         if(!outputFile.exists() || outputFile.length() == 0)
218         {
219             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(outputFile));
220             pw.println(pageContent);
221             pw.close();
222         }
223         
224         return outputFile;
225     }
226
227     /**
228      * This method checks if the given file exists on disk. If it does it's ignored because
229      * that means that the file is allready cached on the server. If not we dump
230      * the given url on it.
231      */

232     /*
233     public String getUrlToFile(String url, Map headers, String fileName, String filePath) throws Exception
234     {
235         String pageContent = null;
236         
237         File outputFile = new File(filePath + File.separator + fileName);
238         System.out.println("outputFile:" + outputFile.getAbsolutePath());
239         if(!outputFile.exists() || outputFile.length() == 0)
240         {
241             HttpHelper helper = new HttpHelper();
242             pageContent = helper.getUrlContent(url, headers);
243             System.out.println("pageContent:" + pageContent);
244             if(pageContent == null || pageContent.equals(""))
245                 return null;
246
247             PrintWriter pw = new PrintWriter(new FileWriter(outputFile));
248             pw.println(pageContent);
249             pw.close();
250         }
251         
252         return pageContent;
253     }
254     */

255     
256
257     /**
258      * This method checks if the given file exists on disk. If it does it's ignored because
259      * that means that the file is allready cached on the server. If not we take out the stream from the
260      * digitalAsset-object and dumps it.
261      */

262 /*
263     public File dumpDigitalAsset(DigitalAsset digitalAsset, String fileName, String filePath) throws Exception
264     {
265         long timer = System.currentTimeMillis();
266
267         File outputFile = outputFile = new File(filePath, fileName);
268
269         // Check existing files
270         if (outputFile.exists() && outputFile.createNewFile() == false)
271         {
272             return outputFile;
273         }
274
275         FileOutputStream fileOutputStream = null;
276         BufferedOutputStream bufferedOutputStream = null;
277         BufferedInputStream bufferedInputStream = null;
278         FileDescriptor fileDescriptor = null;
279         FileChannel fileChannel = null;
280         FileLock fileLock = null;
281         try
282         {
283
284             fileOutputStream = new FileOutputStream(outputFile);
285             fileDescriptor = fileOutputStream.getFD();
286             fileChannel = fileOutputStream.getChannel();
287             fileLock = fileChannel.tryLock();
288             if (fileLock == null)
289             {
290                 // if lock is taken return existing file
291                 return outputFile;
292             }
293
294             bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
295             bufferedInputStream = new BufferedInputStream(digitalAsset.getAssetBlob());
296
297             byte[] buf = new byte[4096];
298             int c = 0;
299             while ((c = bufferedInputStream.read(buf)) != -1)
300             {
301                 bufferedOutputStream.write(buf, 0, c);
302             }
303         }
304         catch (Exception exception)
305         {
306             throw exception;
307         }
308         finally
309         {
310             if (bufferedOutputStream != null)
311             {
312                 bufferedOutputStream.flush();
313             }
314             if (fileDescriptor != null)
315             {
316                 fileDescriptor.sync();
317             }
318             if (fileLock != null)
319             {
320                 fileLock.release();
321             }
322             if (fileChannel != null)
323             {
324                 fileChannel.close();
325             }
326             if (bufferedInputStream != null)
327             {
328                 bufferedInputStream.close();
329             }
330             if (fileOutputStream != null)
331             {
332                 fileOutputStream.close();
333             }
334             if (bufferedOutputStream != null)
335             {
336                 bufferedOutputStream.close();
337             }
338             fileLock = null;
339             fileChannel = null;
340             bufferedInputStream = null;
341             fileOutputStream = null;
342             bufferedOutputStream = null;
343             fileDescriptor = null;
344         }
345
346         logger.warn("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer));
347
348         return outputFile;
349     }
350 */

351     
352     public File JavaDoc dumpDigitalAsset(DigitalAsset digitalAsset, String JavaDoc fileName, String JavaDoc filePath) throws Exception JavaDoc
353     {
354         Timer timer = new Timer();
355         File JavaDoc tmpOutputFile = new File JavaDoc(filePath + File.separator + Thread.currentThread().getId() + "_tmp_" + fileName);
356         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
357         //logger.warn("outputFile:" + filePath + File.separator + fileName + ":" + outputFile.length());
358
if(outputFile.exists())
359         {
360             //logger.warn("The file allready exists so we don't need to dump it again..");
361
return outputFile;
362         }
363
364         try
365         {
366             //System.out.println("tmpOutputFile:" + tmpOutputFile.getAbsolutePath());
367
//System.out.println("outputFile:" + outputFile.getAbsolutePath());
368

369             //System.out.println("Dumping asset " + Thread.currentThread().getId() + ":" + fileName);
370
//Thread.sleep(2000);
371
InputStream JavaDoc inputStream = digitalAsset.getAssetBlob();
372             logger.info("inputStream:" + inputStream + ":" + inputStream.getClass().getName() + ":" + digitalAsset);
373             synchronized(inputStream)
374             {
375                 logger.info("reading inputStream and writing to disk....");
376                 
377                 FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(tmpOutputFile);
378                 BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fos);
379                 BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(inputStream);
380                 
381                 int character;
382                 int i=0;
383                 while ((character = bis.read()) != -1)
384                 {
385                     bos.write(character);
386                     i++;
387                 }
388                 
389                 if(i == 0)
390                     logger.info("Wrote " + i + " chars to " + fileName);
391                 
392                 bos.flush();
393                 fos.close();
394                 bos.close();
395                     
396                 bis.close();
397
398                 logger.info("done reading inputStream and writing to disk....");
399             }
400             
401             logger.info("Time for dumping file " + fileName + ":" + timer.getElapsedTime());
402
403             if(tmpOutputFile.length() == 0 || outputFile.exists())
404             {
405                 logger.info("written file:" + tmpOutputFile.length() + " - removing temp and not renaming it...");
406                 tmpOutputFile.delete();
407                 logger.info("Time for deleting file " + timer.getElapsedTime());
408             }
409             else
410             {
411                 logger.info("written file:" + tmpOutputFile.length() + " - renaming it to " + outputFile.getAbsolutePath());
412                 tmpOutputFile.renameTo(outputFile);
413                 logger.info("Time for renaming file " + timer.getElapsedTime());
414             }
415         }
416         catch (IOException JavaDoc e)
417         {
418             throw new Exception JavaDoc("Could not write file " + outputFile.getAbsolutePath() + " - error reported:" + e.getMessage(), e);
419         }
420         
421         return outputFile;
422     }
423
424
425     /**
426      * This method checks if the given file exists on disk. If it does it's ignored because
427      * that means that the file is allready cached on the server. If not we take out the stream from the
428      * digitalAsset-object and dumps it.
429      */

430     
431     public File JavaDoc dumpDigitalAsset(File JavaDoc masterFile, String JavaDoc fileName, String JavaDoc filePath) throws Exception JavaDoc
432     {
433         long timer = System.currentTimeMillis();
434         
435         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
436         //System.out.println("outputFile:" + filePath + File.separator + fileName + ":" + outputFile.length());
437
if(outputFile.exists() && outputFile.length() > 0)
438         {
439             //logger.info("The file allready exists so we don't need to dump it again..");
440
return outputFile;
441         }
442         
443         try
444         {
445             //System.out.println("outputFile:" + filePath + File.separator + fileName);
446
outputFile.createNewFile();
447             
448             FileOutputStream JavaDoc fis = new FileOutputStream JavaDoc(outputFile);
449             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fis);
450     
451             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(masterFile));
452             
453             int character;
454             while ((character = bis.read()) != -1)
455             {
456                 bos.write(character);
457             }
458             bos.flush();
459             
460             bis.close();
461             fis.close();
462             bos.close();
463             //logger.warn("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer));
464

465             //FileDescriptor fd = fis.getFD();
466
//fd.sync();
467
}
468         catch (IOException JavaDoc e)
469         {
470             throw new Exception JavaDoc("Could not write file " + outputFile.getAbsolutePath() + " - error reported:" + e.getMessage(), e);
471         }
472         
473         return outputFile;
474     }
475
476     /**
477      * This method checks if the given file exists on disk. If it does it's ignored because
478      * that means that the file is allready cached on the server. If not we take out the stream from the
479      * digitalAsset-object and dumps a thumbnail to it.
480      */

481     
482     public File JavaDoc dumpDigitalAssetThumbnail(String JavaDoc fileName, String JavaDoc thumbnailFile, String JavaDoc filePath, int width, int height) throws Exception JavaDoc
483     {
484         long timer = System.currentTimeMillis();
485         logger.info("fileName:" + fileName);
486         logger.info("thumbnailFile:" + thumbnailFile);
487         
488         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + thumbnailFile);
489         if(outputFile.exists())
490         {
491             logger.info("The file allready exists so we don't need to dump it again..");
492             return outputFile;
493         }
494         
495         ThumbnailGenerator tg = new ThumbnailGenerator();
496         tg.transform(filePath + File.separator + fileName, filePath + File.separator + thumbnailFile, width, height, 100);
497         
498         logger.info("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer));
499         
500         return outputFile;
501     }
502     
503     
504     /**
505      * This method dumps the digitalAsset to file and unzips it. If it does it's ignored because
506      * that means that the file is allready cached on the server. If not we take out the stream from the
507      * digitalAsset-object and dumps it.
508      */

509     
510     public File JavaDoc dumpAndUnzipDigitalAsset(DigitalAsset digitalAsset, String JavaDoc fileName, String JavaDoc filePath, File JavaDoc unzipDirectory) throws Exception JavaDoc
511     {
512         File JavaDoc zipFile = dumpDigitalAsset(digitalAsset, fileName, filePath);
513         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
514         unzipFile(outputFile, unzipDirectory);
515         return zipFile;
516     }
517
518     /**
519      * This method dumps the digitalAsset to file and unzips it. If it does it's ignored because
520      * that means that the file is allready cached on the server. If not we take out the stream from the
521      * digitalAsset-object and dumps it.
522      */

523     
524     public File JavaDoc dumpAndUnzipDigitalAsset(File JavaDoc masterFile, String JavaDoc fileName, String JavaDoc filePath, File JavaDoc unzipDirectory) throws Exception JavaDoc
525     {
526         File JavaDoc zipFile = dumpDigitalAsset(masterFile, fileName, filePath);
527         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
528         unzipFile(outputFile, unzipDirectory);
529         return zipFile;
530     }
531
532     public Vector JavaDoc dumpAndGetZipEntries(DigitalAsset digitalAsset, String JavaDoc fileName, String JavaDoc filePath, File JavaDoc unzipDirectory) throws Exception JavaDoc
533     {
534         dumpDigitalAsset(digitalAsset, fileName, filePath);
535         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
536         return getZipFileEntries(outputFile, unzipDirectory);
537     }
538     
539     public Vector JavaDoc dumpAndGetZipEntries(File JavaDoc masterFile, String JavaDoc fileName, String JavaDoc filePath, File JavaDoc unzipDirectory) throws Exception JavaDoc
540     {
541         dumpDigitalAsset(masterFile, fileName, filePath);
542         File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
543         return getZipFileEntries(outputFile, unzipDirectory);
544     }
545     
546
547     /**
548      * This method removes all images in the digitalAsset directory which belongs to a certain content version.
549      */

550
551     public void deleteContentVersionAssets(Integer JavaDoc contentVersionId) throws SystemException, Exception JavaDoc
552     {
553         try
554         {
555             List JavaDoc digitalAssetVOList = DigitalAssetController.getController().getDigitalAssetVOList(contentVersionId);
556             Iterator JavaDoc assetIterator = digitalAssetVOList.iterator();
557             while(assetIterator.hasNext())
558             {
559                 DigitalAssetVO digitalAssetVO = (DigitalAssetVO)assetIterator.next();
560                 this.deleteDigitalAssets(digitalAssetVO.getId());
561             }
562         }
563         catch(Exception JavaDoc e)
564         {
565             logger.error("Could not delete the assets for the contentVersion " + contentVersionId + ":" + e.getMessage(), e);
566         }
567     }
568
569     
570     
571     /**
572      * This method removes all images in the digitalAsset directory which belongs to a certain digital asset.
573      */

574     
575     public void deleteDigitalAssets(Integer JavaDoc digitalAssetId) throws SystemException, Exception JavaDoc
576     {
577         try
578         {
579             int i = 0;
580             String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
581             while(filePath != null)
582             {
583                 File JavaDoc assetDirectory = new File JavaDoc(filePath);
584                 File JavaDoc[] files = assetDirectory.listFiles(new FilenameFilterImpl(digitalAssetId.toString()));
585                 for(int j=0; j<files.length; j++)
586                 {
587                     File JavaDoc file = files[j];
588                     logger.info("Deleting file " + file.getPath());
589                     file.delete();
590                 }
591                 i++;
592                 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
593             }
594
595             //File assetDirectory = new File(CmsPropertyHandler.getDigitalAssetPath());
596
//File[] files = assetDirectory.listFiles(new FilenameFilterImpl(digitalAssetId.toString()));
597
//for(int i=0; i<files.length; i++)
598
//{
599
// File file = files[i];
600
// logger.info("Deleting file " + file.getPath());
601
// file.delete();
602
//}
603

604         }
605         catch(Exception JavaDoc e)
606         {
607             logger.error("Could not delete the assets for the digitalAsset " + digitalAssetId + ":" + e.getMessage(), e);
608         }
609     }
610     
611     /**
612      * This method unzips a zip-file recursively.
613      */

614     
615     private void unzipFile(File JavaDoc assetFile, File JavaDoc targetFolder) throws Exception JavaDoc
616     {
617         logger.info("Unzipping file " + assetFile.getPath() + " to " + targetFolder);
618         Enumeration JavaDoc entries;
619         ZipFile JavaDoc zipFile = new ZipFile JavaDoc(assetFile);
620         entries = zipFile.entries();
621
622         while(entries.hasMoreElements())
623         {
624             ZipEntry JavaDoc entry = (ZipEntry JavaDoc)entries.nextElement();
625
626             if(entry.isDirectory())
627             {
628                 // Assume directories are stored parents first then children.
629
//System.err.println("Extracting directory: " + targetFolder + File.separator + entry.getName());
630
// This is not robust, just for demonstration purposes.
631
(new File JavaDoc(targetFolder + File.separator + entry.getName())).mkdirs();
632                 continue;
633             }
634     
635             //System.err.println("Extracting file: " + targetFolder + File.separator + entry.getName());
636
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(targetFolder + File.separator + entry.getName())));
637         }
638     
639         zipFile.close();
640     }
641
642     private Vector JavaDoc getZipFileEntries(File JavaDoc assetFile, File JavaDoc targetFolder) throws Exception JavaDoc
643     {
644         logger.info("Getting entries from " + assetFile.getPath());
645         Enumeration JavaDoc entries;
646         Vector JavaDoc entryCopies = new Vector JavaDoc();
647         ZipFile JavaDoc zipFile = new ZipFile JavaDoc(assetFile);
648         entries = zipFile.entries();
649
650         while(entries.hasMoreElements())
651         {
652             ZipEntry JavaDoc entry = (ZipEntry JavaDoc)entries.nextElement();
653             ZipEntry JavaDoc entryCopy = (ZipEntry JavaDoc) entry.clone();
654             
655             entryCopies.add(entryCopy);
656         }
657     
658         zipFile.close();
659         return entryCopies;
660     }
661
662
663     /**
664      * Just copies the files...
665      */

666     
667     private void copyInputStream(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc
668     {
669         byte[] buffer = new byte[1024];
670         int len;
671
672         while((len = in.read(buffer)) >= 0)
673             out.write(buffer, 0, len);
674
675         in.close();
676         out.close();
677     }
678
679
680 }
Popular Tags